Dynamic query with table variable to loop through all tables to update in a database2019 Community Moderator ElectionWhat is the advantage of using FAST_FORWARD for defining a cursor?How do I get list of all tables in a database using TSQL?How can I list all foreign keys referencing a given table in SQL Server?How to assign a select result to a variable?SQL update query using joinsUpdate a table using JOIN in SQL Server?SQL Server: How to Join to first rowFind all tables containing column with specified name - MS SQL ServerGet size of all tables in databaseLocal Variable in UPDATE() T-SQL Trigger FunctionQuery for updating a column in same table from same table
Can other pieces capture a threatening piece and prevent a checkmate?
How are passwords stolen from companies if they only store hashes?
HP P840 HDD RAID 5 many strange drive failures
gerund and noun applications
Why is there so much iron?
What is the significance behind "40 days" that often appears in the Bible?
Are dual Irish/British citizens bound by the 90/180 day rule when travelling in the EU after Brexit?
How do hiring committees for research positions view getting "scooped"?
Light propagating through a sound wave
Knife as defense against stray dogs
Pronounciation of the combination "st" in spanish accents
Do US professors/group leaders only get a salary, but no group budget?
Suggestions on how to spend Shaabath (constructively) alone
Wrapping homogeneous Python objects
Existence of a celestial body big enough for early civilization to be thought of as a second moon
Is there a hypothetical scenario that would make Earth uninhabitable for humans, but not for (the majority of) other animals?
Tikz: place node leftmost of two nodes of different widths
Do I need to be arrogant to get ahead?
Relation between independence and correlation of uniform random variables
How might a highly intelligent aquatic species (mermaids) communicate underwater?
Can you move over difficult terrain with only 5 feet of movement?
Describing a chess game in a novel
What are substitutions for coconut in curry?
How to terminate ping <dest> &
Dynamic query with table variable to loop through all tables to update in a database
2019 Community Moderator ElectionWhat is the advantage of using FAST_FORWARD for defining a cursor?How do I get list of all tables in a database using TSQL?How can I list all foreign keys referencing a given table in SQL Server?How to assign a select result to a variable?SQL update query using joinsUpdate a table using JOIN in SQL Server?SQL Server: How to Join to first rowFind all tables containing column with specified name - MS SQL ServerGet size of all tables in databaseLocal Variable in UPDATE() T-SQL Trigger FunctionQuery for updating a column in same table from same table
I working on a SQL query to update a list of tables and columns in a database. Any suggestions are welcome.
I am using information_schema.tables and information_schema.columns, lookup table, and a table variable. However, I am having an issue updating.
I started with the join to ensure I am matching the right table and column needed to the lookup table.
I need to loop through and update tables where matched to the objectname if table.column.
i was trying to use a cursor but did some research and thought table variable may work better for optimization and performance.
I declared my variables and create a table variable but it is still not updating. I started with the below query after I created and populated my table variable.
I tried different ways with a cursor and table variable but I am not getting it right to complete the update.
'Select l.objectname, l.fieldname, l.newpvalue, l.oldpvalue from lookup_table 1 inner join information_schema.tables t
on l.objectname = t.table_name
inner join information_schema.columns c
on l.fieldname = c.column_name and l.objectname
= c.table_name'
Here is the new update that I am attempting to parameterized
SELECT 'UPDATE' + I.TABLE_NAME + 'SET C.COLUMN_NAME =
CASE
WHEN TP.[listValue] = TP.[OldValue]
WHEN TP.[listValue] != TP.[OldValue]
THEN SET TP.NOTES = C.COLUMN_NAME '-' TP.[OldValue]
AND SET C.COLUMN_NAME = NULL
END
FROM
INFORMATION_SCHEMA.TABLES I INNER JOIN [dbo].[tpRef] tp
ON I.TABLE_NAME = tp.ObjectName
INNER JOIN INFORMATION_SCHEMA.COLUMNS C
ON tp.[FieldName] = C.COLUMN_NAME
AND tp.[ObjectName] = C.TABLE_NAME
LEFT JOIN INFORMATION_SCHEMA.COLUMNS C2
ON tp.[FieldName] = C2.COLUMN_NAME
AND C2.COLUMN_NAME = 'NOTES'
WHERE TABLE_TYPE = 'base table'
AND tp.ObjectName = 'table name'
AND tp.FieldName = 'table field'
AND tp.[listValue] = 'List'
tsql
add a comment |
I working on a SQL query to update a list of tables and columns in a database. Any suggestions are welcome.
I am using information_schema.tables and information_schema.columns, lookup table, and a table variable. However, I am having an issue updating.
I started with the join to ensure I am matching the right table and column needed to the lookup table.
I need to loop through and update tables where matched to the objectname if table.column.
i was trying to use a cursor but did some research and thought table variable may work better for optimization and performance.
I declared my variables and create a table variable but it is still not updating. I started with the below query after I created and populated my table variable.
I tried different ways with a cursor and table variable but I am not getting it right to complete the update.
'Select l.objectname, l.fieldname, l.newpvalue, l.oldpvalue from lookup_table 1 inner join information_schema.tables t
on l.objectname = t.table_name
inner join information_schema.columns c
on l.fieldname = c.column_name and l.objectname
= c.table_name'
Here is the new update that I am attempting to parameterized
SELECT 'UPDATE' + I.TABLE_NAME + 'SET C.COLUMN_NAME =
CASE
WHEN TP.[listValue] = TP.[OldValue]
WHEN TP.[listValue] != TP.[OldValue]
THEN SET TP.NOTES = C.COLUMN_NAME '-' TP.[OldValue]
AND SET C.COLUMN_NAME = NULL
END
FROM
INFORMATION_SCHEMA.TABLES I INNER JOIN [dbo].[tpRef] tp
ON I.TABLE_NAME = tp.ObjectName
INNER JOIN INFORMATION_SCHEMA.COLUMNS C
ON tp.[FieldName] = C.COLUMN_NAME
AND tp.[ObjectName] = C.TABLE_NAME
LEFT JOIN INFORMATION_SCHEMA.COLUMNS C2
ON tp.[FieldName] = C2.COLUMN_NAME
AND C2.COLUMN_NAME = 'NOTES'
WHERE TABLE_TYPE = 'base table'
AND tp.ObjectName = 'table name'
AND tp.FieldName = 'table field'
AND tp.[listValue] = 'List'
tsql
are you indicating that i should use a cursor with table variable?
– Sonya
Mar 7 at 19:30
add a comment |
I working on a SQL query to update a list of tables and columns in a database. Any suggestions are welcome.
I am using information_schema.tables and information_schema.columns, lookup table, and a table variable. However, I am having an issue updating.
I started with the join to ensure I am matching the right table and column needed to the lookup table.
I need to loop through and update tables where matched to the objectname if table.column.
i was trying to use a cursor but did some research and thought table variable may work better for optimization and performance.
I declared my variables and create a table variable but it is still not updating. I started with the below query after I created and populated my table variable.
I tried different ways with a cursor and table variable but I am not getting it right to complete the update.
'Select l.objectname, l.fieldname, l.newpvalue, l.oldpvalue from lookup_table 1 inner join information_schema.tables t
on l.objectname = t.table_name
inner join information_schema.columns c
on l.fieldname = c.column_name and l.objectname
= c.table_name'
Here is the new update that I am attempting to parameterized
SELECT 'UPDATE' + I.TABLE_NAME + 'SET C.COLUMN_NAME =
CASE
WHEN TP.[listValue] = TP.[OldValue]
WHEN TP.[listValue] != TP.[OldValue]
THEN SET TP.NOTES = C.COLUMN_NAME '-' TP.[OldValue]
AND SET C.COLUMN_NAME = NULL
END
FROM
INFORMATION_SCHEMA.TABLES I INNER JOIN [dbo].[tpRef] tp
ON I.TABLE_NAME = tp.ObjectName
INNER JOIN INFORMATION_SCHEMA.COLUMNS C
ON tp.[FieldName] = C.COLUMN_NAME
AND tp.[ObjectName] = C.TABLE_NAME
LEFT JOIN INFORMATION_SCHEMA.COLUMNS C2
ON tp.[FieldName] = C2.COLUMN_NAME
AND C2.COLUMN_NAME = 'NOTES'
WHERE TABLE_TYPE = 'base table'
AND tp.ObjectName = 'table name'
AND tp.FieldName = 'table field'
AND tp.[listValue] = 'List'
tsql
I working on a SQL query to update a list of tables and columns in a database. Any suggestions are welcome.
I am using information_schema.tables and information_schema.columns, lookup table, and a table variable. However, I am having an issue updating.
I started with the join to ensure I am matching the right table and column needed to the lookup table.
I need to loop through and update tables where matched to the objectname if table.column.
i was trying to use a cursor but did some research and thought table variable may work better for optimization and performance.
I declared my variables and create a table variable but it is still not updating. I started with the below query after I created and populated my table variable.
I tried different ways with a cursor and table variable but I am not getting it right to complete the update.
'Select l.objectname, l.fieldname, l.newpvalue, l.oldpvalue from lookup_table 1 inner join information_schema.tables t
on l.objectname = t.table_name
inner join information_schema.columns c
on l.fieldname = c.column_name and l.objectname
= c.table_name'
Here is the new update that I am attempting to parameterized
SELECT 'UPDATE' + I.TABLE_NAME + 'SET C.COLUMN_NAME =
CASE
WHEN TP.[listValue] = TP.[OldValue]
WHEN TP.[listValue] != TP.[OldValue]
THEN SET TP.NOTES = C.COLUMN_NAME '-' TP.[OldValue]
AND SET C.COLUMN_NAME = NULL
END
FROM
INFORMATION_SCHEMA.TABLES I INNER JOIN [dbo].[tpRef] tp
ON I.TABLE_NAME = tp.ObjectName
INNER JOIN INFORMATION_SCHEMA.COLUMNS C
ON tp.[FieldName] = C.COLUMN_NAME
AND tp.[ObjectName] = C.TABLE_NAME
LEFT JOIN INFORMATION_SCHEMA.COLUMNS C2
ON tp.[FieldName] = C2.COLUMN_NAME
AND C2.COLUMN_NAME = 'NOTES'
WHERE TABLE_TYPE = 'base table'
AND tp.ObjectName = 'table name'
AND tp.FieldName = 'table field'
AND tp.[listValue] = 'List'
tsql
tsql
edited Mar 7 at 17:48
Sonya
asked Mar 7 at 17:13
SonyaSonya
62
62
are you indicating that i should use a cursor with table variable?
– Sonya
Mar 7 at 19:30
add a comment |
are you indicating that i should use a cursor with table variable?
– Sonya
Mar 7 at 19:30
are you indicating that i should use a cursor with table variable?
– Sonya
Mar 7 at 19:30
are you indicating that i should use a cursor with table variable?
– Sonya
Mar 7 at 19:30
add a comment |
2 Answers
2
active
oldest
votes
not a working cursor, needs the query to fill out the needed columns to populate the variables.
declare
@table_name varchar(128)
,@schema_name varchar(128) -- dbo is the default schema, if custom make sure query returns the correct schema
,@update_column varchar(128)
,@update_data varchar(1024)
,@where_column varchar(128)
,@where_data varchar(1024)
,@s varchar(max);
declare x cursor local fast_forward
for
/*
insert your query returning desired values to be looped through.
*/
select 1,2,3,4,5,6 --6 variables to populate. delete this line when you get your query written above :)
open x;
fetch next from x into @table_name,@update_column,@schema_name,@update_data,@where_column,@where_data;
while @@fetch_status = 0
begin
set @s = '
update ' + quotename(@schema_name) + '.' + quotename(@table_name) + '
set ' + quotename(@update_column) + ' = ''' + @update_data + ''' where ' + quotename(@where_column) + ' = ''' + @where_data + ''';
'
begin try
--to execute it, you can use exec()
exec(@s);
--to print out the string so you can read and or copy/paste to text execution use print()
print(@s);
/*
I would recommend print(@s); until you have all your bugs worked out.
process the updates manually from the printed statements to verify syntax correctness and results
*/
end try
begin catch
print error_message()
end catch;
-- this list of variables must match the first "fetch next" and all be declared and returned in the "for" query of the cursor.
-- those that are not must be set inside the cursor by queries, calculations or hardcoded.
fetch next from x into @table_name,@update_column,@schema_name,@update_data,@where_column,@where_data;
end
close x;
deallocate x;
Cursor used is
Fast_forward(SO answer) = (read_only and forward_only)
Local(cursor performance breakdown) = local in scope, only valid within scope.
I still can't bring myself to use the information_schema views...
thank you but i need update the table and column data value. I am using a lookup to pass the table name, column name and value that i need to compare to determine value update. I will remove the information you are pulling that i do not need and try using this one. I am getting an syntax with the dynamic sql i am using.
– Sonya
Mar 7 at 21:59
the issue I have is executing an update statement in the set query where I need to pass tablename and columnname
– Sonya
Mar 7 at 22:48
modified the answer so that it's closer to what your final result needs, still need to populate the cursor with a query returning the needed data for the updates.
– JBJ
Mar 7 at 23:45
thank i actually did what you modified to on yesterday. It was updating but the value was not correct. So I am testing again this morning but it is not updating at all now so tracing through to see what changed.
– Sonya
Mar 8 at 15:04
add a comment |
I was attempting to use table variable but are you indicating to use both a table variable and cursor?
When I run the below is takes a long time.
----UPDATE TABLE AND COLUMN FOUND
SELECT @STRUPDSQL =
'UPDATE' + 'I.TABLE_NAME' + 'SET C.COLUMN_NAME =
CASE
WHEN TP.[PicklistValue] = TP.[OldValue]
WHEN TP.[PicklistValue] != TP.[OldValue]
THEN SET TP.NOTES = C.COLUMN_NAME - TP.[OldValue]
AND SET C.COLUMN_NAME = NULL
END
FROM
INFORMATION_SCHEMA.TABLES I INNER JOIN @TBLS T
ON I.TABLE_NAME = T.v_TABLENAME
WHERE T.v_TABLENAME =@l_TBLNM AND T.v_COLUMNNAME =@l_COLNNM
'
SET @I = @I + 1
END
I have it working but the value that I want to update with is being read as a column name and not the column data value.
IF quotename(@COLNAME) IS NOT NULL
BEGIN
SET @SQL = 'UPDATE ' + quotename(@TABLENAME) + ' SET ' + quotename(@COLNAME) + ' = ' +'"' + @TVALUE + '"' + ' ' + 'WHERE ' + quotename(@COLNAME) + '= AccountSource'
END
i realized i need to go back to include a temp table or table variable like i had in my original code to successfully update the value in the table that I am passing.
– Sonya
Mar 11 at 14:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049429%2fdynamic-query-with-table-variable-to-loop-through-all-tables-to-update-in-a-data%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
not a working cursor, needs the query to fill out the needed columns to populate the variables.
declare
@table_name varchar(128)
,@schema_name varchar(128) -- dbo is the default schema, if custom make sure query returns the correct schema
,@update_column varchar(128)
,@update_data varchar(1024)
,@where_column varchar(128)
,@where_data varchar(1024)
,@s varchar(max);
declare x cursor local fast_forward
for
/*
insert your query returning desired values to be looped through.
*/
select 1,2,3,4,5,6 --6 variables to populate. delete this line when you get your query written above :)
open x;
fetch next from x into @table_name,@update_column,@schema_name,@update_data,@where_column,@where_data;
while @@fetch_status = 0
begin
set @s = '
update ' + quotename(@schema_name) + '.' + quotename(@table_name) + '
set ' + quotename(@update_column) + ' = ''' + @update_data + ''' where ' + quotename(@where_column) + ' = ''' + @where_data + ''';
'
begin try
--to execute it, you can use exec()
exec(@s);
--to print out the string so you can read and or copy/paste to text execution use print()
print(@s);
/*
I would recommend print(@s); until you have all your bugs worked out.
process the updates manually from the printed statements to verify syntax correctness and results
*/
end try
begin catch
print error_message()
end catch;
-- this list of variables must match the first "fetch next" and all be declared and returned in the "for" query of the cursor.
-- those that are not must be set inside the cursor by queries, calculations or hardcoded.
fetch next from x into @table_name,@update_column,@schema_name,@update_data,@where_column,@where_data;
end
close x;
deallocate x;
Cursor used is
Fast_forward(SO answer) = (read_only and forward_only)
Local(cursor performance breakdown) = local in scope, only valid within scope.
I still can't bring myself to use the information_schema views...
thank you but i need update the table and column data value. I am using a lookup to pass the table name, column name and value that i need to compare to determine value update. I will remove the information you are pulling that i do not need and try using this one. I am getting an syntax with the dynamic sql i am using.
– Sonya
Mar 7 at 21:59
the issue I have is executing an update statement in the set query where I need to pass tablename and columnname
– Sonya
Mar 7 at 22:48
modified the answer so that it's closer to what your final result needs, still need to populate the cursor with a query returning the needed data for the updates.
– JBJ
Mar 7 at 23:45
thank i actually did what you modified to on yesterday. It was updating but the value was not correct. So I am testing again this morning but it is not updating at all now so tracing through to see what changed.
– Sonya
Mar 8 at 15:04
add a comment |
not a working cursor, needs the query to fill out the needed columns to populate the variables.
declare
@table_name varchar(128)
,@schema_name varchar(128) -- dbo is the default schema, if custom make sure query returns the correct schema
,@update_column varchar(128)
,@update_data varchar(1024)
,@where_column varchar(128)
,@where_data varchar(1024)
,@s varchar(max);
declare x cursor local fast_forward
for
/*
insert your query returning desired values to be looped through.
*/
select 1,2,3,4,5,6 --6 variables to populate. delete this line when you get your query written above :)
open x;
fetch next from x into @table_name,@update_column,@schema_name,@update_data,@where_column,@where_data;
while @@fetch_status = 0
begin
set @s = '
update ' + quotename(@schema_name) + '.' + quotename(@table_name) + '
set ' + quotename(@update_column) + ' = ''' + @update_data + ''' where ' + quotename(@where_column) + ' = ''' + @where_data + ''';
'
begin try
--to execute it, you can use exec()
exec(@s);
--to print out the string so you can read and or copy/paste to text execution use print()
print(@s);
/*
I would recommend print(@s); until you have all your bugs worked out.
process the updates manually from the printed statements to verify syntax correctness and results
*/
end try
begin catch
print error_message()
end catch;
-- this list of variables must match the first "fetch next" and all be declared and returned in the "for" query of the cursor.
-- those that are not must be set inside the cursor by queries, calculations or hardcoded.
fetch next from x into @table_name,@update_column,@schema_name,@update_data,@where_column,@where_data;
end
close x;
deallocate x;
Cursor used is
Fast_forward(SO answer) = (read_only and forward_only)
Local(cursor performance breakdown) = local in scope, only valid within scope.
I still can't bring myself to use the information_schema views...
thank you but i need update the table and column data value. I am using a lookup to pass the table name, column name and value that i need to compare to determine value update. I will remove the information you are pulling that i do not need and try using this one. I am getting an syntax with the dynamic sql i am using.
– Sonya
Mar 7 at 21:59
the issue I have is executing an update statement in the set query where I need to pass tablename and columnname
– Sonya
Mar 7 at 22:48
modified the answer so that it's closer to what your final result needs, still need to populate the cursor with a query returning the needed data for the updates.
– JBJ
Mar 7 at 23:45
thank i actually did what you modified to on yesterday. It was updating but the value was not correct. So I am testing again this morning but it is not updating at all now so tracing through to see what changed.
– Sonya
Mar 8 at 15:04
add a comment |
not a working cursor, needs the query to fill out the needed columns to populate the variables.
declare
@table_name varchar(128)
,@schema_name varchar(128) -- dbo is the default schema, if custom make sure query returns the correct schema
,@update_column varchar(128)
,@update_data varchar(1024)
,@where_column varchar(128)
,@where_data varchar(1024)
,@s varchar(max);
declare x cursor local fast_forward
for
/*
insert your query returning desired values to be looped through.
*/
select 1,2,3,4,5,6 --6 variables to populate. delete this line when you get your query written above :)
open x;
fetch next from x into @table_name,@update_column,@schema_name,@update_data,@where_column,@where_data;
while @@fetch_status = 0
begin
set @s = '
update ' + quotename(@schema_name) + '.' + quotename(@table_name) + '
set ' + quotename(@update_column) + ' = ''' + @update_data + ''' where ' + quotename(@where_column) + ' = ''' + @where_data + ''';
'
begin try
--to execute it, you can use exec()
exec(@s);
--to print out the string so you can read and or copy/paste to text execution use print()
print(@s);
/*
I would recommend print(@s); until you have all your bugs worked out.
process the updates manually from the printed statements to verify syntax correctness and results
*/
end try
begin catch
print error_message()
end catch;
-- this list of variables must match the first "fetch next" and all be declared and returned in the "for" query of the cursor.
-- those that are not must be set inside the cursor by queries, calculations or hardcoded.
fetch next from x into @table_name,@update_column,@schema_name,@update_data,@where_column,@where_data;
end
close x;
deallocate x;
Cursor used is
Fast_forward(SO answer) = (read_only and forward_only)
Local(cursor performance breakdown) = local in scope, only valid within scope.
I still can't bring myself to use the information_schema views...
not a working cursor, needs the query to fill out the needed columns to populate the variables.
declare
@table_name varchar(128)
,@schema_name varchar(128) -- dbo is the default schema, if custom make sure query returns the correct schema
,@update_column varchar(128)
,@update_data varchar(1024)
,@where_column varchar(128)
,@where_data varchar(1024)
,@s varchar(max);
declare x cursor local fast_forward
for
/*
insert your query returning desired values to be looped through.
*/
select 1,2,3,4,5,6 --6 variables to populate. delete this line when you get your query written above :)
open x;
fetch next from x into @table_name,@update_column,@schema_name,@update_data,@where_column,@where_data;
while @@fetch_status = 0
begin
set @s = '
update ' + quotename(@schema_name) + '.' + quotename(@table_name) + '
set ' + quotename(@update_column) + ' = ''' + @update_data + ''' where ' + quotename(@where_column) + ' = ''' + @where_data + ''';
'
begin try
--to execute it, you can use exec()
exec(@s);
--to print out the string so you can read and or copy/paste to text execution use print()
print(@s);
/*
I would recommend print(@s); until you have all your bugs worked out.
process the updates manually from the printed statements to verify syntax correctness and results
*/
end try
begin catch
print error_message()
end catch;
-- this list of variables must match the first "fetch next" and all be declared and returned in the "for" query of the cursor.
-- those that are not must be set inside the cursor by queries, calculations or hardcoded.
fetch next from x into @table_name,@update_column,@schema_name,@update_data,@where_column,@where_data;
end
close x;
deallocate x;
Cursor used is
Fast_forward(SO answer) = (read_only and forward_only)
Local(cursor performance breakdown) = local in scope, only valid within scope.
I still can't bring myself to use the information_schema views...
edited Mar 7 at 23:44
answered Mar 7 at 20:08
JBJJBJ
16616
16616
thank you but i need update the table and column data value. I am using a lookup to pass the table name, column name and value that i need to compare to determine value update. I will remove the information you are pulling that i do not need and try using this one. I am getting an syntax with the dynamic sql i am using.
– Sonya
Mar 7 at 21:59
the issue I have is executing an update statement in the set query where I need to pass tablename and columnname
– Sonya
Mar 7 at 22:48
modified the answer so that it's closer to what your final result needs, still need to populate the cursor with a query returning the needed data for the updates.
– JBJ
Mar 7 at 23:45
thank i actually did what you modified to on yesterday. It was updating but the value was not correct. So I am testing again this morning but it is not updating at all now so tracing through to see what changed.
– Sonya
Mar 8 at 15:04
add a comment |
thank you but i need update the table and column data value. I am using a lookup to pass the table name, column name and value that i need to compare to determine value update. I will remove the information you are pulling that i do not need and try using this one. I am getting an syntax with the dynamic sql i am using.
– Sonya
Mar 7 at 21:59
the issue I have is executing an update statement in the set query where I need to pass tablename and columnname
– Sonya
Mar 7 at 22:48
modified the answer so that it's closer to what your final result needs, still need to populate the cursor with a query returning the needed data for the updates.
– JBJ
Mar 7 at 23:45
thank i actually did what you modified to on yesterday. It was updating but the value was not correct. So I am testing again this morning but it is not updating at all now so tracing through to see what changed.
– Sonya
Mar 8 at 15:04
thank you but i need update the table and column data value. I am using a lookup to pass the table name, column name and value that i need to compare to determine value update. I will remove the information you are pulling that i do not need and try using this one. I am getting an syntax with the dynamic sql i am using.
– Sonya
Mar 7 at 21:59
thank you but i need update the table and column data value. I am using a lookup to pass the table name, column name and value that i need to compare to determine value update. I will remove the information you are pulling that i do not need and try using this one. I am getting an syntax with the dynamic sql i am using.
– Sonya
Mar 7 at 21:59
the issue I have is executing an update statement in the set query where I need to pass tablename and columnname
– Sonya
Mar 7 at 22:48
the issue I have is executing an update statement in the set query where I need to pass tablename and columnname
– Sonya
Mar 7 at 22:48
modified the answer so that it's closer to what your final result needs, still need to populate the cursor with a query returning the needed data for the updates.
– JBJ
Mar 7 at 23:45
modified the answer so that it's closer to what your final result needs, still need to populate the cursor with a query returning the needed data for the updates.
– JBJ
Mar 7 at 23:45
thank i actually did what you modified to on yesterday. It was updating but the value was not correct. So I am testing again this morning but it is not updating at all now so tracing through to see what changed.
– Sonya
Mar 8 at 15:04
thank i actually did what you modified to on yesterday. It was updating but the value was not correct. So I am testing again this morning but it is not updating at all now so tracing through to see what changed.
– Sonya
Mar 8 at 15:04
add a comment |
I was attempting to use table variable but are you indicating to use both a table variable and cursor?
When I run the below is takes a long time.
----UPDATE TABLE AND COLUMN FOUND
SELECT @STRUPDSQL =
'UPDATE' + 'I.TABLE_NAME' + 'SET C.COLUMN_NAME =
CASE
WHEN TP.[PicklistValue] = TP.[OldValue]
WHEN TP.[PicklistValue] != TP.[OldValue]
THEN SET TP.NOTES = C.COLUMN_NAME - TP.[OldValue]
AND SET C.COLUMN_NAME = NULL
END
FROM
INFORMATION_SCHEMA.TABLES I INNER JOIN @TBLS T
ON I.TABLE_NAME = T.v_TABLENAME
WHERE T.v_TABLENAME =@l_TBLNM AND T.v_COLUMNNAME =@l_COLNNM
'
SET @I = @I + 1
END
I have it working but the value that I want to update with is being read as a column name and not the column data value.
IF quotename(@COLNAME) IS NOT NULL
BEGIN
SET @SQL = 'UPDATE ' + quotename(@TABLENAME) + ' SET ' + quotename(@COLNAME) + ' = ' +'"' + @TVALUE + '"' + ' ' + 'WHERE ' + quotename(@COLNAME) + '= AccountSource'
END
i realized i need to go back to include a temp table or table variable like i had in my original code to successfully update the value in the table that I am passing.
– Sonya
Mar 11 at 14:46
add a comment |
I was attempting to use table variable but are you indicating to use both a table variable and cursor?
When I run the below is takes a long time.
----UPDATE TABLE AND COLUMN FOUND
SELECT @STRUPDSQL =
'UPDATE' + 'I.TABLE_NAME' + 'SET C.COLUMN_NAME =
CASE
WHEN TP.[PicklistValue] = TP.[OldValue]
WHEN TP.[PicklistValue] != TP.[OldValue]
THEN SET TP.NOTES = C.COLUMN_NAME - TP.[OldValue]
AND SET C.COLUMN_NAME = NULL
END
FROM
INFORMATION_SCHEMA.TABLES I INNER JOIN @TBLS T
ON I.TABLE_NAME = T.v_TABLENAME
WHERE T.v_TABLENAME =@l_TBLNM AND T.v_COLUMNNAME =@l_COLNNM
'
SET @I = @I + 1
END
I have it working but the value that I want to update with is being read as a column name and not the column data value.
IF quotename(@COLNAME) IS NOT NULL
BEGIN
SET @SQL = 'UPDATE ' + quotename(@TABLENAME) + ' SET ' + quotename(@COLNAME) + ' = ' +'"' + @TVALUE + '"' + ' ' + 'WHERE ' + quotename(@COLNAME) + '= AccountSource'
END
i realized i need to go back to include a temp table or table variable like i had in my original code to successfully update the value in the table that I am passing.
– Sonya
Mar 11 at 14:46
add a comment |
I was attempting to use table variable but are you indicating to use both a table variable and cursor?
When I run the below is takes a long time.
----UPDATE TABLE AND COLUMN FOUND
SELECT @STRUPDSQL =
'UPDATE' + 'I.TABLE_NAME' + 'SET C.COLUMN_NAME =
CASE
WHEN TP.[PicklistValue] = TP.[OldValue]
WHEN TP.[PicklistValue] != TP.[OldValue]
THEN SET TP.NOTES = C.COLUMN_NAME - TP.[OldValue]
AND SET C.COLUMN_NAME = NULL
END
FROM
INFORMATION_SCHEMA.TABLES I INNER JOIN @TBLS T
ON I.TABLE_NAME = T.v_TABLENAME
WHERE T.v_TABLENAME =@l_TBLNM AND T.v_COLUMNNAME =@l_COLNNM
'
SET @I = @I + 1
END
I have it working but the value that I want to update with is being read as a column name and not the column data value.
IF quotename(@COLNAME) IS NOT NULL
BEGIN
SET @SQL = 'UPDATE ' + quotename(@TABLENAME) + ' SET ' + quotename(@COLNAME) + ' = ' +'"' + @TVALUE + '"' + ' ' + 'WHERE ' + quotename(@COLNAME) + '= AccountSource'
END
I was attempting to use table variable but are you indicating to use both a table variable and cursor?
When I run the below is takes a long time.
----UPDATE TABLE AND COLUMN FOUND
SELECT @STRUPDSQL =
'UPDATE' + 'I.TABLE_NAME' + 'SET C.COLUMN_NAME =
CASE
WHEN TP.[PicklistValue] = TP.[OldValue]
WHEN TP.[PicklistValue] != TP.[OldValue]
THEN SET TP.NOTES = C.COLUMN_NAME - TP.[OldValue]
AND SET C.COLUMN_NAME = NULL
END
FROM
INFORMATION_SCHEMA.TABLES I INNER JOIN @TBLS T
ON I.TABLE_NAME = T.v_TABLENAME
WHERE T.v_TABLENAME =@l_TBLNM AND T.v_COLUMNNAME =@l_COLNNM
'
SET @I = @I + 1
END
I have it working but the value that I want to update with is being read as a column name and not the column data value.
IF quotename(@COLNAME) IS NOT NULL
BEGIN
SET @SQL = 'UPDATE ' + quotename(@TABLENAME) + ' SET ' + quotename(@COLNAME) + ' = ' +'"' + @TVALUE + '"' + ' ' + 'WHERE ' + quotename(@COLNAME) + '= AccountSource'
END
edited Mar 11 at 14:14
answered Mar 7 at 19:35
SonyaSonya
62
62
i realized i need to go back to include a temp table or table variable like i had in my original code to successfully update the value in the table that I am passing.
– Sonya
Mar 11 at 14:46
add a comment |
i realized i need to go back to include a temp table or table variable like i had in my original code to successfully update the value in the table that I am passing.
– Sonya
Mar 11 at 14:46
i realized i need to go back to include a temp table or table variable like i had in my original code to successfully update the value in the table that I am passing.
– Sonya
Mar 11 at 14:46
i realized i need to go back to include a temp table or table variable like i had in my original code to successfully update the value in the table that I am passing.
– Sonya
Mar 11 at 14:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049429%2fdynamic-query-with-table-variable-to-loop-through-all-tables-to-update-in-a-data%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
are you indicating that i should use a cursor with table variable?
– Sonya
Mar 7 at 19:30