Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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
SQL
create procedure deleteRecord
(
 @tblName nvarchar(255),
 @condition nvarchar(255)
)
as 
  begin
     delete from @tblName @condition
  end

Help me.
Thanks in Adv.
Posted
Updated 22-Nov-11 3:43am
v2

A one size fits all delete operation would be ill-advised and near impossible to implement with any reliability or security
 
Share this answer
 
Comments
Mehdi Gholam 22-Nov-11 9:52am    
Agreed a terrible idea. 5'ed - but if the OP is asking for rope... :)
[no name] 22-Nov-11 10:05am    
The one who loads the gun is just as responsible as the one who pulls the trigger
fjdiewornncalwe 22-Nov-11 10:46am    
Agreed. A silly idea just asking for abuse.
Change the code to the following :

SQL
exec ('delete from ' + @tblName + ' ' + @condition)
 
Share this answer
 
Comments
Nilesh Patil Kolhapur 22-Nov-11 10:29am    
Thanks.
Amir Mahfoozi 22-Nov-11 11:11am    
+5ed to neutralize malicious +1er ! ;)
fjdiewornncalwe 22-Nov-11 11:29am    
extremely dangerous. In the example used, if the user puts in exec deleteRecord 'Table1', '' then you will simply delete everything. Where is the validation, how can it be validated, how do you protect your data from abuse.
Mehdi Gholam 22-Nov-11 11:47am    
The presumption albeit a far fetched one is the OP knows what he is doing. I just supply the rope :)
Here it is :
SQL
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 :

SQL
exec deleteRecord 'Table1' , 'id=7'



Hope It Helps.
 
Share this answer
 
v2
Comments
Nilesh Patil Kolhapur 22-Nov-11 10:42am    
Thanks Amir
Amir Mahfoozi 22-Nov-11 11:08am    
You're welcome ;)
fjdiewornncalwe 22-Nov-11 11:29am    
extremely dangerous. In the example used, if the user puts in exec deleteRecord 'Table1', '' then you will simply delete everything. Where is the validation, how can it be validated, how do you protect your data from abuse.
Amir Mahfoozi 22-Nov-11 11:34am    
I have tested what you claimed and got his error : Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'where'.
Also you should know that I just answered his question, he didn't asked "Is it the correct way or not?" to discuss about it.
fjdiewornncalwe 22-Nov-11 13:18pm    
You did, and I'm just adding my two cents worth on the danger of doing it this way so that inexperienced devs who may view this as an easy quick win will think twice about doing it this way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900