我创建了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
答案 0 :(得分:1)
您在语句中忘记了FROM子句,它是
DELETE FROM dbo.EmployeeDetails WHERE ....
答案 1 :(得分:1)
因为您的DELETE
声明不正确。正确的语法是
DELETE FROM <TableName> Where <Condition>
因此,请将您的查询更改为此,然后尝试
DELETE FROM [dbo].[EmployeeDetails] Where Pincode = @Pincode
答案 2 :(得分:0)
你必须使变量成为可选的,你没有在程序中传递:
使用此修改后的内容:
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
希望它有所帮助。 :)
答案 3 :(得分:0)
删除记录的存储过程 - 书籍详细信息示例如下:
Create procedure[dbo].[DeleteBookDetails_SP]
@BookId Int
As
Begin
Delete from BookDetails Where BookId=@BookId
End
这里&#34; DeleteBookDetails_SP&#34;是存储过程名称,&#34; BookDetails&#34;是表名和&#34; BookId&#34;是表中的列名。
答案 4 :(得分:0)
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
希望有帮助。 :)