Wednesday, March 28, 2012

how to use variable to refer to database name

I need to have a query like below to run to select all the fields from
different databases that each database have a table named 'table1'
select * from test...table1
select * from test1..table1
I have the following code sample:
declare @.dbname varchar(50)
set @.dbname='test'
select @.dbname
select * from @.dbname..table1
However I received the error messge when I ran this code. Can anyone
help to resolve this?
Thanks!dynamic sql can be used
exec ('select * from ' + @.dbname + '..table1')
"foru123" <s_he2000@.hotmail.com> wrote in message
news:1136691805.429931.164080@.g49g2000cwa.googlegroups.com...
> I need to have a query like below to run to select all the fields from
> different databases that each database have a table named 'table1'
> select * from test...table1
> select * from test1..table1
> I have the following code sample:
> declare @.dbname varchar(50)
> set @.dbname='test'
> select @.dbname
> select * from @.dbname..table1
> However I received the error messge when I ran this code. Can anyone
> help to resolve this?
> Thanks!
>|||I think database names are not of type varchar. They are of type sysobjects.
Message posted via droptable.com
http://www.droptable.com/Uwe/Forum...server/200601/1

No comments:

Post a Comment