[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Cursor to insert data into mutliple databases on same server

Posted on 2014-03-05
4
Medium Priority
?
23 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 41

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 700 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 41

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 700 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

650 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