Solved

Cursor to insert data into mutliple databases on same server

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

770 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