Unable to insert database name in sql string

Unable to insert database name in sql string

am 03.04.2008 16:28:47 von Chris H

I'm trying to create a procedure that will insert items from multiple
(identically structured) databases into a master database table. The
first field "Project" in the select string causes an error when the
procedure is executed (the procedure itself can be created withouth
error). If I hard code a value for "Project" (ie: ...1 AS Project...)
the query runs fine for all the other columns (but the results aren't
what I need). I think the issue is the punctuation around
"+'['+rtrim(@daba_name)+']'+" in the first line of @sql but don't know
how to correct. Any thoughts would be apprecitated!

I also have a WHERE clause that drops off older values by date that is
remmed out since I couldn't get that punctuation straight either.

Thanks, Chris.

The error generated when the procedure is run states:

....
Msg 207, Level 16, State 1, Line 1
Invalid column name 'BroadReach'.
....

This error repeats with the different database names.

Procedure:

....
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DELETE
FROM [BroadReach].[dbo].[Activity_MasterII]
DECLARE @db_row_counter AS INT
DECLARE @daba_name AS CHAR(50)
DECLARE @db_row_max AS INT
DECLARE @sql AS CHAR(1500)
SET @db_row_counter = 7
SELECT name, dbid FROM [master].[dbo].[sysdatabases]
SET @db_row_max = @@rowcount
start_point:
IF @db_row_counter <= @db_row_max
BEGIN
SET @daba_name = (SELECT name
FROM (SELECT name,
ROW_NUMBER()OVER(ORDER BY dbid) AS db_row
FROM master.dbo.sysdatabases) AS names_from_master
WHERE db_row = @db_row_counter)
IF @daba_name like 'oncontact%' GOTO skip_defaults
IF @daba_name like 'TestVersion%' GOTO skip_defaults
IF @daba_name like 'Training%' GOTO skip_defaults
/* --Next Line is the problem-- */
SET @sql = 'SELECT TOP (100) PERCENT '+'['+rtrim(@daba_name)+']'+'
AS Project,
COUNT('+'['+rtrim(@daba_name)+']'+'.[dbo].
[oncd_activity].activity_id) AS CountAct,
'+'['+rtrim(@daba_name)+']'+'.[dbo].[onca_user].full_name,
'+'['+rtrim(@daba_name)+']'+'.[dbo].[oncd_activity].action_c ode,
YEAR('+'['+rtrim(@daba_name)+']'+'.[dbo].
[oncd_activity].creation_date) AS ActYear,
MONTH('+'['+rtrim(@daba_name)+']'+'.[dbo].
[oncd_activity].creation_date) AS ActMonth
FROM '+'['+rtrim(@daba_name)+']'+'.[dbo].[oncd_activity]
LEFT OUTER JOIN '+'['+rtrim(@daba_name)+']'+'.[dbo].
[oncd_activity_company] ON '+'['+rtrim(@daba_name)+']'+'.[dbo].
[oncd_activity].activity_id = '+'['+rtrim(@daba_name)+']'+'.[dbo].
[oncd_activity_company].activity_id
LEFT OUTER JOIN '+'['+rtrim(@daba_name)+']'+'.[dbo].[onca_user] ON
'+'['+rtrim(@daba_name)+']'+'.[dbo].
[oncd_activity].created_by_user_code = '+'['+rtrim(@daba_name)+']'+'.
[dbo].[onca_user].user_code
/* - also interested in proper punctuation around the 2006... date
string on following line */
/* WHERE ('+'['+rtrim(@daba_name)+']'+'.[dbo].
[oncd_activity].creation_date > CONVERT(DATETIME, '2006-01-01
00:00:00', 102))*/

GROUP BY '+'['+rtrim(@daba_name)+']'+'.[dbo].
[oncd_activity].action_code,
'+'['+rtrim(@daba_name)+']'+'.[dbo].[onca_user].full_name,
YEAR('+'['+rtrim(@daba_name)+']'+'.[dbo].
[oncd_activity].creation_date),
MONTH('+'['+rtrim(@daba_name)+']'+'.[dbo].
[oncd_activity].creation_date)
HAVING('+'['+rtrim(@daba_name)+']'+'.[dbo].
[oncd_activity].action_code IS NOT NULL)'
INSERT INTO [BroadReach].[dbo].[Activity_MasterII]
EXEC (@sql)
skip_defaults:
SET @db_row_counter = @db_row_counter + 1
GOTO start_point
END
END

Re: Unable to insert database name in sql string

am 03.04.2008 22:27:38 von Chris H

On Apr 3, 10:28=A0am, Chris H
wrote:
> I'm trying to create a procedure that will insert items from multiple
> (identically structured) databases into a master database table. =A0 The
> first field "Project" in the select string causes an error when the
> procedure is executed (the procedure itself can be created withouth
> error). =A0If I hard code a value for "Project" (ie: ...1 AS Project...)
> the query runs fine for all the other columns (but the results aren't
> what I need). =A0I think the issue is the punctuation around
> "+'['+rtrim(@daba_name)+']'+" in the first line of @sql but don't know
> how to correct. =A0Any thoughts would be apprecitated!

Found my own fixes:
'+'''['+rtrim(@daba_name)+']'+''' AS Project

CONVERT(DATETIME, '+'''2006-01-01 00:00:00'''+'

Thanks.

Re: Unable to insert database name in sql string

am 03.04.2008 23:40:11 von Erland Sommarskog

Chris H (chollstein@broadreachpartnersinc.com) writes:
> I'm trying to create a procedure that will insert items from multiple
> (identically structured) databases into a master database table. The
> first field "Project" in the select string causes an error when the
> procedure is executed (the procedure itself can be created withouth
> error). If I hard code a value for "Project" (ie: ...1 AS Project...)
> the query runs fine for all the other columns (but the results aren't
> what I need). I think the issue is the punctuation around
> "+'['+rtrim(@daba_name)+']'+" in the first line of @sql but don't know
> how to correct. Any thoughts would be apprecitated!

You have:

> SET @sql = 'SELECT TOP (100) PERCENT
> '+'['+rtrim(@daba_name)+']'+' AS Project,

This results in

[BroadReach] AS Project,

Judging from the error message, you don't have any column named
BroadReach in any of the tables in your query.

[] quotes an identifier. This permits you haves and and columns with
irregular characters in them, for instance [Order Details].

In this case you apparently want a string literal, so the right would
be

quotename(@daba_name, '''')

The quotename function wraps the parameter in the delimiter specified
by the second parameter, and doubles any instances of the delimiter.

And thus, this:

> COUNT('+'['+rtrim(@daba_name)+']'+'.[dbo].
> [oncd_activity].activity_id) AS CountAct,

should be:

COUNT(' + quotename(@daba_name) +
'.[dbo]. [oncd_activity].activity_id) AS CountAct,

I removed the rtrim function, because if a database really has trailing
spaces in the name, you should retain it in your queries. Else you will
be using another database.


By the way, remove that meaning lesd SELECT TOP(100) PERCENT.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx