0

I created a SQL procedure that replaces all values in a field with Xs the same length as the original values.

Update  Table
Set     Name = Replicate('x', Len(Name))

I am trying to alter this procedure so that I can just pass a table and field name as a parameter on execute instead of editing the stored procedure every time I want to pass a new field. Is there a way to do this?

This is what I think the execute statements should look like when I want to x out the values in another field:

Exec MyProcedure ‘Users’, ‘Email’
Cat4Storm
  • 3
  • 1
  • 3
  • You can do this in a stored procedure with dynamic SQL but it is dangerous. You might want to explain what you are really trying to do here so that we can suggest some safer and more reliable options. – RBarryYoung Jul 19 '16 at 17:28
  • Thanks for your reply. You're right, a procedure like this is dangerous but it will only be executed in a test DB and I have a check built in that ensures this. How could I do this with dynamic SQL? – Cat4Storm Jul 19 '16 at 17:34

1 Answers1

0

You can use EXEC to execute sql statements like this: EXEC (@sqlCommand). SqlCommand would be the string composed by you based on the received parameters.

Also, another option would be to run the statement using sp_executesql.