单个存储过程可从任何表中删除记录 [英] single stored procedure to delete a record from any table
本文介绍了单个存储过程可从任何表中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我想编写一个存储过程,以便使用我尝试遵循的任何条件从任何表中删除记录,但未成功
Hi All,
I want to write a single stored procedure to delete record from any table using any codition i try following but didnt succeeded
create procedure deleteRecord
(
@tblName nvarchar(255),
@condition nvarchar(255)
)
as
begin
delete from @tblName @condition
end
帮帮我.
谢谢.
Help me.
Thanks in Adv.
推荐答案
一个大小适合所有删除操作的建议不正确,几乎不可能以任何可靠性或安全性实现
A one size fits all delete operation would be ill-advised and near impossible to implement with any reliability or security
将代码更改为以下内容:
Change the code to the following :
exec ('delete from ' + @tblName + ' ' + @condition)
这里是:
create procedure deleteRecord
(
@tblName nvarchar(255),
@condition nvarchar(255)
)
as
begin
declare @cmd as nvarchar(4000)
set @cmd = 'delete ' + @tblName + ' where ' + @condition
exec sp_executesql @cmd;
end
您应该这样称呼:
You should call it by this way :
exec deleteRecord 'Table1' , 'id=7'
希望对您有帮助.
Hope It Helps.
这篇关于单个存储过程可从任何表中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文