Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have around 60 databases that I am querying against that have the same tables; however, I want the result of my query to be inserted into a temp table so that I can create a well formatted report using the result stored in that temp table. How do I achieve that? Below is my query that loops through each database and run the query to select all active orders:

SQL
DECLARE @db_name Varchar(100)

DECLARE @SQL Varchar(3000)

DECLARE c_db_names CURSOR FOR
SELECT name 
FROM master.dbo.sysdatabases
WHERE name NOT IN('master','AdventureWorks2008','TestDB','Development','Northwind') --might need to exclude more databases

OPEN c_db_names

FETCH c_db_names INTO @db_name

WHILE @@Fetch_Status = 0
BEGIN   

SET @SQL = 'SELECT OrderID, OrderStatus, OrderDetails, FulfillmentDate, ShippingDate , AddDate from 
       '+ @db_name +'.dbo.ActiveOrders inner join '+ @db_name +'.dbo.FinalShipmentNumbers on '+ @db_name +'.dbo.ActiveOrders.OrderDetails = 
       '+ @db_name +'.dbo.FinalShipmentNumbers.OrderID where addDate between ''1/30/2016'' and ''2/26/2016'''

SELECT @db_name As Client_Name
EXEC(@SQL)      
FETCH c_db_names INTO @db_name
END

CLOSE c_db_names
DEALLOCATE c_db_names


What I have tried:

I have tried to insert the result of my query into a temp Table
Posted
Updated 22-Feb-16 11:11am
v3
Comments
CHill60 22-Feb-16 11:44am    
Just include the creation of the temporary table and put an INSERT statement into your dynamic SQL

1 solution

Please try this way:
SQL
DECLARE @c_db_names AS TABLE
(
ID INT IDENTITY(1,1),
name sysname
)

DECLARE @Records AS TABLE
(
ID INT IDENTITY(1,1),
name sysname,
OrderID NVARCHAR(10), 
OrderStatus NVARCHAR(10), 
OrderDetails NVARCHAR(10), 
FulfillmentDate DATETIME, 
ShippingDate DATETIME, 
AddDate DATETIME
)


DECLARE @SQL NVarchar(3000)
DECLARE @ID INT
DECLARE @DBName sysname

SET NOCOUNT ON

INSERT @c_db_names
SELECT name 
FROM master.dbo.sysdatabases
WHERE name NOT IN('master','AdventureWorks2008','TestDB','Development','Northwind') --might need to exclude more database

SELECT @ID = COUNT(1) FROM @c_db_names

WHILE (@ID > 0)

	BEGIN

		SELECT @DBName = name
		FROM @c_db_names
		WHERE ID = @ID
		SET @ID = @ID - 1

		SET @SQL =    'SELECT ''' + @DBName + ''' AS [Name], AO.OrderID, AO.OrderStatus, AO.OrderDetails, FS.FulfillmentDate, FS.ShippingDate, FS.AddDate' + CHAR(13) + CHAR(10) 
					+ 'FROM ' + @DBName +'.dbo.ActiveOrders AS AO ' + CHAR(13) + CHAR(10) 
					+ 'INNER JOIN '+ @DBName +'.dbo.FinalShipmentNumbers AS FS ' + CHAR(13) + CHAR(10) 
					+ 'ON' + CHAR(13) + CHAR(10)
					+ ' AO.OrderDetails = FS.OrderID where addDate between ''1/30/2016'' and ''2/26/2016'''  + CHAR(13) + CHAR(10)+ CHAR(13) + CHAR(10)

	   PRINT @SQL;
	   INSERT INTO @Records
	   EXEC SP_EXECUTESQL @SQL

	END

SET NOCOUNT OFF
SELECT * FROM @Records
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900