Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

run sql script against database from management studio

Posted on 2013-12-03
1
Medium Priority
?
361 Views
Last Modified: 2013-12-12
Hi,

I have a number of sql databases in an instance and need to run a sql script on the operating system against each database;
I have the query below which runs against each database but when i add a sql command like sqlcmd -i c:\upgrade.sql against each database it won't work;

EXEC master..sp_MSforeachdb '
USE [?]
IF ''?'' <> ''master'' AND ''?'' <> ''model'' AND ''?'' <> ''msdb'' AND ''?'' <> ''tempdb''
BEGIN
                print ''Processing database ?''
         
                sqlcmd -i c:\upgrade.sql
END
'

how can i run my upgrade scrip agains the 50 database in my instance from sql management studio or some other better way ?
0
Comment
Question by:hraja77
[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
1 Comment
 
LVL 9

Accepted Solution

by:
BlueYonder earned 1500 total points
ID: 39692784
Here is some of the code you will have to fill in the  t-sql in the while loop




--Check if #TableNames exists

PRINT 'Job Start Time: ' + convert(varchar(30), getdate(), 109)

IF OBJECT_ID('tempdb..#TableNames') IS NOT NULL
BEGIN
      DROP TABLE #TableNames
END

--CREATE TEMP TABLE TO STORE TABLE NAMES AND NUMBER OF MISMATCH RECORDS
CREATE TABLE #TableNames(
      table_id INT PRIMARY KEY IDENTITY
      , table_name varchar(100)
)

--INSERT TABLE NAMES
INSERT INTO #TableNames(table_name)
SELECT
      name
FROM
      sys.databases
WHERE
      --database_id = 13
      database_id > 6
ORDER BY 1

DECLARE @currCount int
SELECT @currCount = COUNT(*) FROM #TableNames

WHILE(@currCount > 0)
BEGIN
      --fill in with t-sql for each table
END


--delete temp table
DROP TABLE #TableNames



PRINT 'Job End Time: ' + convert(varchar(30), getdate(), 109)

--DATABASE FILES
--SELECT * from sys.database_files
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

722 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