Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Multiple db query call from within different context into ..

 
   Database Forums (Home) -> General Discussions RSS
Next:  Insert too slow on Linux 12.5.3ESD#6  
Author Message
santaferubber

External


Since: Mar 20, 2007
Posts: 1



(Msg. 1) Posted: Tue Mar 20, 2007 11:57 pm
Post subject: Multiple db query call from within different context into #temp table
Archived from groups: comp>databases>ms-sqlserver (more info?)

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

 >> Stay informed about: Multiple db query call from within different context into .. 
Back to top
Login to vote
othellomy

External


Since: Sep 15, 2006
Posts: 13



(Msg. 2) Posted: Wed Mar 21, 2007 1:38 am
Post subject: Re: Multiple db query call from within different context into #temp table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Mar 21, 12:57 pm, 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

 >> Stay informed about: Multiple db query call from within different context into .. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Call store procedure from function - Hi, is there any method to call a store procedure into a function? Thanks Fabio

how to call a stored procedure in an insert command -

SQL Server Procedure call from ADO not returning the corre.. - Hi, I have a SQL database that accepts data loads from Lotus Notes thru ADO calls. We have several tables and from the ADO connection these tables are populated thru stored procedures(with parameters). Inside each stored procedures, we are doing proper....

Importing multiple files to SQL - I have over three hundred text files that I need to import to SQL Server. Each is in the exact same format. I want to import tham as seperate tables. Is there any way to do it in one process? Regards, Ciarán

Getting count with multiple fields - Hi all, I'm running into a road block, and I know I've done this before. I'm getting fields from two tables, and I need to do a count of similar items with it showing some extra info. Here's my fields: Log.LogId - Int Log.LogDispatcherID - Int..
   Database Forums (Home) -> General Discussions All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]