Unix Technical Forum

Multiple db query call from within different context into #temp table

This is a discussion on Multiple db query call from within different context into #temp table within the SQL Server forums, part of the Microsoft SQL Server category; --> The first query returns me the results from multiple databases, the second does the same thing except it puts ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:43 PM
santaferubber@gmail.com
 
Posts: n/a
Default Multiple db query call from within different context into #temp table

The first query returns me the results from multiple databases, the
second does the same thing except it puts the result into a #temp
table? Could someone please show me an example of this using the first
query? The first query uses the @exec_context and I am having a
challenge trying to figure out how to make the call from within a
different context and still insert into a #temp table.

DECLARE @exec_context varchar(30)
declare @sql nvarchar(4000)
DECLARE @DBNAME nvarchar(50)
DECLARE companies_cursor CURSOR FOR
SELECT DBNAME
FROM DBINFO
WHERE DBNAME NOT IN ('master', 'tempdb', 'msdb', 'model')
ORDER BY DBNAME
OPEN companies_cursor
FETCH NEXT FROM companies_cursor INTO @DBNAME
WHILE @@FETCH_STATUS = 0
BEGIN
set @exec_context = @DBNAME + '.dbo.sp_executesql '
set @sql = N'select top 10 * from products'
exec @exec_context @sql
FETCH NEXT FROM companies_cursor INTO @DBNAME
END
CLOSE companies_cursor
DEALLOCATE companies_cursor
-------------------------------------------------------------------------------------
CREATE TABLE #Test (field list here)
declare @sql nvarchar(4000)
DECLARE @DBNAME nvarchar(50)

DECLARE companies_cursor CURSOR FOR
SELECT NAME
FROM sysdatabases
WHERE OBJECT_ID(Name+'.dbo.products') IS NOT NULL
ORDER BY NAME
OPEN companies_cursor
FETCH NEXT FROM companies_cursor INTO @DBNAME
WHILE @@FETCH_STATUS = 0
BEGIN
set @sql = N'select top 10 * from '+@DBNAME+'.dbo.products'
INSERT INTO #Test
exec (@sql)
FETCH NEXT FROM companies_cursor INTO @DBNAME
END
CLOSE companies_cursor
DEALLOCATE companies_cursor
SELECT * from #Test
DROP TABLE #Test

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02:43 PM
othellomy@yahoo.com
 
Posts: n/a
Default Re: Multiple db query call from within different context into #temp table

On Mar 21, 12:57 pm, santaferub...@gmail.com wrote:
> The first query returns me the results from multiple databases, the
> second does the same thing except it puts the result into a #temp
> table? Could someone please show me an example of this using the first
> query? The first query uses the @exec_context and I am having a
> challenge trying to figure out how to make the call from within a
> different context and still insert into a #temp table.
>
> DECLARE @exec_context varchar(30)
> declare @sql nvarchar(4000)
> DECLARE @DBNAME nvarchar(50)
> DECLARE companies_cursor CURSOR FOR
> SELECT DBNAME
> FROM DBINFO
> WHERE DBNAME NOT IN ('master', 'tempdb', 'msdb', 'model')
> ORDER BY DBNAME
> OPEN companies_cursor
> FETCH NEXT FROM companies_cursor INTO @DBNAME
> WHILE @@FETCH_STATUS = 0
> BEGIN
> set @exec_context = @DBNAME + '.dbo.sp_executesql '
> set @sql = N'select top 10 * from products'
> exec @exec_context @sql
> FETCH NEXT FROM companies_cursor INTO @DBNAME
> END
> CLOSE companies_cursor
> DEALLOCATE companies_cursor
> ---------------------------------------------------------------------------*----------
> CREATE TABLE #Test (field list here)
> declare @sql nvarchar(4000)
> DECLARE @DBNAME nvarchar(50)
>
> DECLARE companies_cursor CURSOR FOR
> SELECT NAME
> FROM sysdatabases
> WHERE OBJECT_ID(Name+'.dbo.products') IS NOT NULL
> ORDER BY NAME
> OPEN companies_cursor
> FETCH NEXT FROM companies_cursor INTO @DBNAME
> WHILE @@FETCH_STATUS = 0
> BEGIN
> set @sql = N'select top 10 * from '+@DBNAME+'.dbo.products'
> INSERT INTO #Test
> exec (@sql)
> FETCH NEXT FROM companies_cursor INTO @DBNAME
> END
> CLOSE companies_cursor
> DEALLOCATE companies_cursor
> SELECT * from #Test
> DROP TABLE #Test


Not sure if this solves the problem:

WHILE @@FETCH_STATUS = 0
BEGIN
set @sql = ' INSERT INTO #Test select top 10 * from '+@DBNAME
+'.dbo.products'
exec (@sql)
FETCH NEXT FROM companies_cursor INTO @DBNAME
END

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:57 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com