Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Concatenating results?

Author  Topic 

dmilam
Posting Yak Master

185 Posts

Posted - 2010-10-13 : 19:20:00
I have a table that has two attributes, one holding database names, the other holds tables in the respective databases. I want a result set that at least prints a series of queries combining both, like so:


SELECT TOP 1 * FROM database1.name1
SELECT TOP 1 * FROM database1.name2
SELECT TOP 1 * FROM database2.name1


so that I can copy the result and then paste, then run. How do I concatenate the result of


SELECT database FROM table + SELECT name FROM table


to yield the above? Without dynamic SQL would be nice.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-13 : 19:25:03
Do you mean like this:

SELECT 'SELECT ' + Column1 + ', ' + Column2 + ' FROM SomeTable'
FROM SomeOtherTable



Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-13 : 19:25:37
Maybe you could post a quick data example to make it more clear as I am sure I am missing something.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dmilam
Posting Yak Master

185 Posts

Posted - 2010-10-14 : 12:32:56
Trying again to post...

Tara, that's nice and succinct.

SELECT 'SELECT TOP 1 * FROM ' + dbase + '.' + tbl
FROM t..processTables


is what I am looking for. I love it when the solution is much simpler than anticipated! I also got a while loop to give me results, but only as distinct expressions.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-14 : 14:20:14


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -