Solved

Cursor to insert data into mutliple databases on same server

Posted on 2014-03-05
4
17 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 40

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 40

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

840 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