I am trying to figure out how to use sp_msforeachtable to perform an action on all tables and variables that match variable/table names stored in another table
IE
I have a table that has 3 columns : table, variable, action
and I am trying to use sp_MSforeachtable to see which tables and variables match, and if match, perform that action on the table.
How do you call variable names in the sp_MSforeachtable statement? I know to use ? for the table name, but not sure how I would say if variable name=variable name then do X
Is there another way to do this without using this undocumented SP?
Ill try to explain better:
I am trying to clean personal info from a bunch of tables... I have a table that looks like this (not sure how to format a table, so imagine each entry is a seperate row, so the first row is Name, A, and set to '')
Variable
Name
Phone Number
Name
Table
A
A
B
Action
Set to ''
Set to '555-555-5555'
Set to ''
etc.
I then have a database full of tables....on table A, I would want my code to set all rows of variable 'Name'
to '' (blank)
, and Phone Number to '555-555-5555'
etc.and then move on to table B and do the same and so on
I would use a cursor and dynamic SQL:
Note: There are security concerns with what you are trying to do, since anyone who can add to your table will have the same access as the account which runs the script. You could reduce these concerns by defining the actions in another table which can only be edited by the administrator, then referencing the action in your existing table.
Note: It is best to have the data types of @action, @table, and @variable match their source columns. The variables an be any data type in your database (as long as it is not a local temp type). You will notice that there are two places in the code above where the types are defined, first where the variables are declared at the top, and second where the arguments for sp_executesql are defined in the string near the bottom.
Note: if @stmt and @params are assigned with a constant instead of a variable, make sure to prefix the constant with N so it will be read as a Unicode string.