Sql server 删除记录的存储过程

Sql server 删除记录的存储过程,sql-server,Sql Server,我创建了3个用于插入、更新和删除记录的存储过程 虽然插入和更新记录存储过程工作正常,但我无法通过删除存储过程删除记录 该存储过程中的代码如下所示: CREATE PROCEDURE usp_DeleteEmployeeRcord (@Name NVarchar(50), @FName Nvarchar(50), @Address NVarchar (50), @Email Nvarchar(50), @Mobile NVarchar(50),

我创建了3个用于插入、更新和删除记录的存储过程

虽然插入和更新记录存储过程工作正常,但我无法通过删除存储过程删除记录

该存储过程中的代码如下所示:

CREATE PROCEDURE usp_DeleteEmployeeRcord
    (@Name NVarchar(50),
     @FName Nvarchar(50),
     @Address NVarchar (50),
     @Email Nvarchar(50),
     @Mobile NVarchar(50),
     @Pincode NVarchar(50))
AS 
BEGIN
    DELETE [dbo].[EmployeeDetails]  
    WHERE Pincode = @Pincode
END

您必须使变量成为可选变量,但不在过程中传递:

使用此修改的:

CREATE PROCEDURE usp_DeleteEmployeeRcord
(
 @Name NVarchar(50)='',
 @FName Nvarchar(50)='', 
 @Address NVarchar (50)='', 
 @Email Nvarchar(50)='', 
 @Mobile NVarchar(50)='', 
 @Pincode NVArchar(50)
)

AS 
        BEGIN
            DELETE [dbo].[EmployeeDetails]  
            Where Pincode = @Pincode
        END

希望有帮助。:)

您必须使变量成为可选变量,而不在过程中传递这些变量:

使用此修改的:

CREATE PROCEDURE usp_DeleteEmployeeRcord
(
 @Name NVarchar(50)='',
 @FName Nvarchar(50)='', 
 @Address NVarchar (50)='', 
 @Email Nvarchar(50)='', 
 @Mobile NVarchar(50)='', 
 @Pincode NVArchar(50)
)

AS 
        BEGIN
            DELETE [dbo].[EmployeeDetails]  
            Where Pincode = @Pincode
        END

希望有帮助。:)

您在声明中忘记了FROM子句,它是

DELETE FROM dbo.EmployeeDetails WHERE ....

你在对账单中忘记了FROM子句,它是

DELETE FROM dbo.EmployeeDetails WHERE ....

因为您的
DELETE
语句不正确。正确的语法是

DELETE FROM <TableName> Where <Condition>

因为您的
DELETE
语句不正确。正确的语法是

DELETE FROM <TableName> Where <Condition>

删除记录的存储过程- 书籍详细信息示例如下:

    Create procedure[dbo].[DeleteBookDetails_SP] 
         @BookId Int
    As
    Begin   

         Delete from BookDetails Where BookId=@BookId

    End

这里“DeleteBookDetails\u SP”是存储过程名,“BookDetails”是表名,“BookId”是表中的列名。

删除记录的存储过程-
Use test
go

CREATE TABLE employee(  
id INTEGER NOT NULL PRIMARY KEY,  
first_name VARCHAR(10),  
last_name VARCHAR(10),  
salary DECIMAL(10,2),  
city VARCHAR(20), 
)  


INSERT INTO employee VALUES (2, 'Monu', 'Rathor',4789,'Agra');  
GO  

INSERT INTO employee VALUES (4, 'Rahul' , 'Saxena', 5567,'London');  
GO  

INSERT INTO employee VALUES (5, 'prabhat', 'kumar', 4467,'Bombay'); 
go  

INSERT INTO employee VALUES (6, 'ramu', 'kksingh', 3456, 'jk');  
go  

select * from employee 


Create procedure MasterInsertUpdateDelete
(
@ID integer,  --//Mandatory parameters
@Firstname varchar(50)=Null,  --//optional parameters//--
@Lastname varchar(50)=Null,
@Salary Decimal(10, 2)=Null,
@City varchar(20)=Null,
@Statementtype nvarchar(200) = '' 
)
as
Begin

    if @Statementtype = 'Insert'
    begin
        insert into employee(id, first_name, last_name, salary, city) values(@ID, @Firstname, @Lastname, @Salary, @City)
    end

    if @Statementtype = 'Select'
    begin
            select *
        from employee
    end
    if @Statementtype = 'Update'
    begin
        update employee set first_name = @Firstname, last_name=@Lastname, salary=@Salary, city=@City
        where id=@ID
    end

    if @Statementtype = 'Delete' 
    begin
    delete from  employee where id=@ID
    end
end

--For insert the records
exec MasterInsertUpdateDelete 7, 'Kallesh', 'Yadav', 30000, 'Bangalore', 'Insert'


--For update the records
exec MasterInsertUpdateDelete 4,'Sneha', 'Kumari', 23000, 'Mumbai', 'Update'

select *
from employee

--For delete the records
exec MasterInsertUpdateDelete  @ID = 2,  @Statementtype = 'Delete' -->We should specify the parameters name while deleting the specific records
书籍详细信息示例如下:

    Create procedure[dbo].[DeleteBookDetails_SP] 
         @BookId Int
    As
    Begin   

         Delete from BookDetails Where BookId=@BookId

    End
这里“DeleteBookDetails_SP”是存储过程名,“BookDetails”是表名,“BookId”是表中的列名

Use test
go

CREATE TABLE employee(  
id INTEGER NOT NULL PRIMARY KEY,  
first_name VARCHAR(10),  
last_name VARCHAR(10),  
salary DECIMAL(10,2),  
city VARCHAR(20), 
)  


INSERT INTO employee VALUES (2, 'Monu', 'Rathor',4789,'Agra');  
GO  

INSERT INTO employee VALUES (4, 'Rahul' , 'Saxena', 5567,'London');  
GO  

INSERT INTO employee VALUES (5, 'prabhat', 'kumar', 4467,'Bombay'); 
go  

INSERT INTO employee VALUES (6, 'ramu', 'kksingh', 3456, 'jk');  
go  

select * from employee 


Create procedure MasterInsertUpdateDelete
(
@ID integer,  --//Mandatory parameters
@Firstname varchar(50)=Null,  --//optional parameters//--
@Lastname varchar(50)=Null,
@Salary Decimal(10, 2)=Null,
@City varchar(20)=Null,
@Statementtype nvarchar(200) = '' 
)
as
Begin

    if @Statementtype = 'Insert'
    begin
        insert into employee(id, first_name, last_name, salary, city) values(@ID, @Firstname, @Lastname, @Salary, @City)
    end

    if @Statementtype = 'Select'
    begin
            select *
        from employee
    end
    if @Statementtype = 'Update'
    begin
        update employee set first_name = @Firstname, last_name=@Lastname, salary=@Salary, city=@City
        where id=@ID
    end

    if @Statementtype = 'Delete' 
    begin
    delete from  employee where id=@ID
    end
end

--For insert the records
exec MasterInsertUpdateDelete 7, 'Kallesh', 'Yadav', 30000, 'Bangalore', 'Insert'


--For update the records
exec MasterInsertUpdateDelete 4,'Sneha', 'Kumari', 23000, 'Mumbai', 'Update'

select *
from employee

--For delete the records
exec MasterInsertUpdateDelete  @ID = 2,  @Statementtype = 'Delete' -->We should specify the parameters name while deleting the specific records
希望有帮助。:)


希望有帮助。:)

您的代码将给出一个语法错误。请仅在自己测试后发布代码。您会发现什么错误?您可能会发现错误,即对象usp_DeleteEmployeeRecord已经存在,因此您只需删除上一个或alter过程即可。是的,对于MS SQL Server,delete语句中的“FROM”是可选的。:)试试看,你的代码会出现语法错误。请仅在自己测试后发布代码。您会发现什么错误?您可能会发现错误,即对象usp_DeleteEmployeeRecord已经存在,因此您只需删除上一个或alter过程即可。是的,对于MS SQL Server,delete语句中的“FROM”是可选的。:)请告诉我们您遇到了什么错误,或者它是否简单而没有错误?告诉我们您遇到了什么错误,或者它是否简单而没有错误?Create语句必须是Create procedure[dbo]。[DeleteBookDetails\u SP](@BookId Int),而不是Create procedure[dbo]。[DeleteBookDetails\u SP]@BookId Int Create语句必须是Create procedure[dbo]。[DeleteBookDetails_SP](@BookId Int)而不是作为创建过程[dbo]。[DeleteBookDetails_SP]@BookId在此存储过程中可以插入、更新、检索和删除记录在此存储过程中可以插入、更新、检索和删除记录如果启动此过程,然后在执行过程中中途取消该过程,表会发生什么情况?在取消之前删除的任何行会保持删除状态还是服务器会滚动返回然后删除?如果启动此过程,然后在执行过程中中途取消它,表会发生什么情况?在取消之前删除的任何行会保持删除状态还是服务器会回滚然后删除?