May 19, 2010 at 12:23 pm
Hi,
This statement is correct:
SELECT 'String'
But when I try to use dynamic it not be correct, How can I do that?
EXECUTE('SELECT 'String'')
Thanks!
May 19, 2010 at 12:25 pm
_ms65g_ (5/19/2010)
Hi,This statement is correct:
SELECT 'String'
But when I try to use dynamic it not be correct, How can I do that?
EXECUTE('SELECT 'String'')
Thanks!
every quote that exists between your forst and last quotes need to be escaped out with an additional quote:
EXECUTE('SELECT ''String'' ')
Lowell
May 19, 2010 at 12:27 pm
Thank you very much!
May 19, 2010 at 1:26 pm
I use database name before the INFORMATION_SCHEM view but when I use the USE statement with another database name my result is incorrect.
I have two database with names "concat" and "Northwind. And I use following FROM clause at the two queries:
FROM [concat].INFORMATION_SCHEMA.COLUMNS
See:
USE concat
SELECT 'UPDATE ' + 'concat' + '..[' + table_name + ']' +
' SET ' + '[' + column_name + ']' + ' = ' + column_default +
' WHERE ' + '[' + column_name + '] IS NULL' + ';'
FROM [concat].INFORMATION_SCHEMA.COLUMNS
WHERE column_default IS NOT NULL
AND OBJECTPROPERTY(OBJECT_ID(table_name),'istable') = 1;
/*
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UPDATE concat..[newOrders] SET [order_date] = (getdate()) WHERE [order_date] IS NULL;
UPDATE concat..[newOrders] SET [amount] = ((0)) WHERE [amount] IS NULL;
UPDATE concat..[Orders] SET [order_date] = (getdate()) WHERE [order_date] IS NULL;
UPDATE concat..[Orders] SET [amount] = ((0)) WHERE [amount] IS NULL;
UPDATE concat..[ttttttt] SET [j] = ((200)) WHERE [j] IS NULL;
*/
use Northwind
SELECT 'UPDATE ' + 'concat' + '..[' + table_name + ']' +
' SET ' + '[' + column_name + ']' + ' = ' + column_default +
' WHERE ' + '[' + column_name + '] IS NULL' + ';'
FROM [concat].INFORMATION_SCHEMA.COLUMNS
WHERE column_default IS NOT NULL
AND OBJECTPROPERTY(OBJECT_ID(table_name),'istable') = 1;
/*
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UPDATE concat..[Orders] SET [order_date] = (getdate()) WHERE [order_date] IS NULL;
UPDATE concat..[Orders] SET [amount] = ((0)) WHERE [amount] IS NULL;
*/
now What do I do?
May 19, 2010 at 1:35 pm
From OBJECTPROPERTY BOL:
The Database Engine assumes that object_id is in the current database context. A query that references an object_id in another database will return NULL or incorrect results...
May 19, 2010 at 1:52 pm
Steve Cullen (5/19/2010)
From OBJECTPROPERTY BOL:The Database Engine assumes that object_id is in the current database context. A query that references an object_id in another database will return NULL or incorrect results...
Good tip, I did not notice the OBJECT_ID function!
Thanks
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply