sql server - sp_msforeachtable performing actions on variables -
i trying figure out how use sp_msforeachtable perform action on tables , variables match variable/table names stored in table
ie
i have table has 3 columns : table, variable, action
and trying use sp_msforeachtable see tables , variables match, , if match, perform action on table.
how call variable names in sp_msforeachtable statement? know use ? table name, not sure how if variable name=variable name x
is there way without using undocumented sp?
ill try explain better:
i trying clean personal info bunch of tables... have table looks (not sure how format table, imagine each entry seperate row, first row name, a, , set '')
variable
name
phone number
name
table
a
a
b
action
set ''
set '555-555-5555'
set ''
etc.
i have database full of tables....on table a, want code set rows of variable 'name'
'' (blank)
, , phone number '555-555-5555'
etc.and move on table b , same , on
i use cursor , dynamic sql:
--set test: create table #datatable (column1 nvarchar(128) not null, column2 int not null); --create global temp table can accessed dynamic sql. create table ##actiontable ([table] nvarchar(128) not null, variable nvarchar(max) not null, [action] nvarchar(max) not null); insert ##actiontable ([table], variable, [action]) values ('#datatable', '1', 'insert @table (column1, column2) values (''@variable_1'', @variable);'), ('#datatable', '2', 'insert @table (column1, column2) values (''@variable_1'', @variable);'), ('#datatable', '3', 'insert @table (column1, column2) values (''@variable_1'', @variable);'), ('#datatable', '4', 'insert @table (column1, column2) values (''@variable_1'', @variable);'); --code: declare @action nvarchar(max); declare @table nvarchar(128); declare @variable nvarchar(max); declare rowcurser cursor select [table], variable, [action] ##actiontable; open rowcurser; fetch rowcurser @table, @variable, @action while @@fetch_status = 0 begin --execute code (pick 1 of two. option 2 safer , can cached (faster), not work example because parameters left variables). -- option 1: set @action = replace(replace(@action, '@table', @table), '@variable', @variable); execute(@action); -- option 2: execute sp_executesql @stmt = n'insert #datatable (column1, column2) values (cast(@variable nvarchar(128)) + n''_2'', @variable);', @params = n'@variable nvarchar(max)', @variable = @variable; --setup next iteration fetch rowcurser @table, @variable, @action end close rowcurser; deallocate rowcurser; --check , cleanup test select * #datatable; drop table #datatable; drop table ##actiontable;
note: there security concerns trying do, since can add table have same access account runs script. reduce these concerns defining actions in table can edited administrator, referencing action in existing table.
note: best have data types of @action, @table, , @variable match source columns. variables data type in database (as long not local temp type). notice there 2 places in code above types defined, first variables declared @ top, , second arguments sp_executesql defined in string near bottom.
note: if @stmt , @params assigned constant instead of variable, make sure prefix constant n read unicode string.
Comments
Post a Comment