Solved

Cursor to insert data into mutliple databases on same server

Posted on 2014-03-05
4
10 Views
Last Modified: 2016-06-04
Good afternoon.  I am trying to write a script that will insert a row of data to multiple databases on the same server.  I keep receiving an "Invalid object name 'DEANA'" error, however, the insert is successful if I were to run in each database indvidually.  The code below represents inserting a row of data into the "DEANA" table in two databases on the server.  In real life, we will be looking at 15 databases.  Any assistance would be GREATLY appreciated.  Thank you in advance for your time.
use [master]
go

-- Declare container variabels for each column we select in the cursor
declare @databaseName nvarchar(128)

-- Define the cursor name
declare DBCursor cursor
-- Define the dataset to loop
for
select [name] from sys.databases where [name] in ('prhiscpdconfig','prraspdconfig')


-- Start loop
open DBCursor

------ Get information from the first row
fetch next from DBCursor into @databaseName

---- Loop until there are no more rows
while @@fetch_status = 0
begin



Insert into DEANA (MCTR_ENTITY, MCTR_TYPE, MCTR_VALUE, MCTR_DESC, 
MCTR_FILTER, MCTR_VALUE_SIZE, MCTR_SORT, MCTR_LOCK_TOKEN, ATXR_SOURCE_ID)
Values ('CSTK', 'CATG', 'PB07', 'SPAP/ADAP', 'PCOB', 0, 0, 1, '1753-01-01')



    -- Get information from next row
    fetch next from DBCursor into @databaseName
end

-- End loop and clean up
close DBCursor
deallocate DBCursor
go

Open in new window

Sincerely,
Nikki
0
Comment
Question by:Nikki28838
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 39907267
use dynamic sql:


declare @sql nvarchar(max)

--in cursor loop:
set @sql =
Insert into @databaseName + '.dbo.DEANA (MCTR_ENTITY, MCTR_TYPE, MCTR_VALUE, MCTR_DESC,
MCTR_FILTER, MCTR_VALUE_SIZE, MCTR_SORT, MCTR_LOCK_TOKEN, ATXR_SOURCE_ID)
Values (''CSTK'', ''CATG'', ''PB07'', ''SPAP/ADAP'', ''PCOB'', 0, 0, 1, ''1753-01-01'')

exec (@sql)

--fetch next
0
 
LVL 10

Accepted Solution

by:
PadawanDBA earned 175 total points
ID: 39907280
You're not changing the context of the insertion away from the master db.  You'll want to probably use some dynamic sql to accomplish your task.  Try:

declare @dynamicSQL nvarchar( 1024 );

---- Loop until there are no more rows
while @@fetch_status = 0
begin

set @dynamicSQL = N'insert into ' + @databaseName + N'<schema here>.DEANA (MCTR_ENTITY, MCTR_TYPE, MCTR_VALUE, MCTR_DESC, 
MCTR_FILTER, MCTR_VALUE_SIZE, MCTR_SORT, MCTR_LOCK_TOKEN, ATXR_SOURCE_ID)
Values (''CSTK'', ''CATG'', ''PB07'', ''SPAP/ADAP'', ''PCOB'', 0, 0, 1, ''1753-01-01'')';

exec sp_executesql @dynamicSQL;

    -- Get information from next row
    fetch next from DBCursor into @databaseName
end

Open in new window

0
 
LVL 39

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 175 total points
ID: 39907289
combine the two queries.  

In my query:

Insert into @databaseName

should be
'Insert into ' + @databaseName +

in Padawan's you need the ' to be '' in the values section.

Feel free to split the points.  Same general principal.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now