Solved

Cursor to insert data into mutliple databases on same server

Posted on 2014-03-05
4
20 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

739 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