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










1















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'









share|improve this question
























  • are you indicating that i should use a cursor with table variable?

    – Sonya
    Mar 7 at 19:30















1















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'









share|improve this question
























  • are you indicating that i should use a cursor with table variable?

    – Sonya
    Mar 7 at 19:30













1












1








1








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'









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












2 Answers
2






active

oldest

votes


















0














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...






share|improve this answer

























  • 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


















0














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





share|improve this answer

























  • 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










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
);



);













draft saved

draft discarded


















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









0














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...






share|improve this answer

























  • 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















0














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...






share|improve this answer

























  • 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













0












0








0







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...






share|improve this answer















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...







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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













0














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





share|improve this answer

























  • 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















0














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





share|improve this answer

























  • 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













0












0








0







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme