August 20, 2010 at 3:34 pm
I am trying to make use of databasename dynamicall so that the sql script can loop through different databases.
declare @dbname varchar(10)
select @dbname = name from sys.databases
where name like'test'
Use @dbname
select top 10 * from dbo.all_names
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near '@dbname'.
Please advice...
August 20, 2010 at 11:24 pm
Dynamic SQL's the only way to do that and once the USE is in dynamic, everything to be done in the DB needs to be as well.
What are you trying to do? There may be a better way.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 22, 2010 at 2:16 pm
so Gail does it mean that i cant use Use @dynamicvariable?. I have a control server and i want to push all the ddl/dml changes to other servers from this server instead of physically logging into each one. I do have linked servers set up from my control server to all other servers. Any thoughts?
August 22, 2010 at 2:37 pm
If you have management studio 2008 (regardless of the database engine version), you can do multi-server queries. Other than that, Redgate has a multi-server query tool iirc.
You can use the SQLCMD mode to connect to servers and run code. I've before now written stored procs to generate SQLCMD mode scripts for running code on multiple servers
But, yes, as I said, dynamic SQL's the only way to do a USE with a variable and once the USE is in dynamic, everything to be done in the DB needs to be as well.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 23, 2010 at 11:13 am
Thanks Gail. I still have few questions,
i) How can i use "Use @dynamicvariable" please provide the script if you have.
ii) could you give some more detail on how can i run query from 2008 client across multiple servers.
Thanks again for your help.
August 23, 2010 at 1:55 pm
Gail from my previous post i got the second part by using 2008 but does only server level. Still need an explanation for the first part..Thanks again...
August 23, 2010 at 3:29 pm
You would need to do something like
declare @dbname nvarchar(255)
declare @sql nvarchar(500)
select @dbname = 'msdb' --Set up your cursor here to insert into this variable
select @sql = 'use ['+@dbname+']
select top 1000 * from sysjobhistory
'
exec(@sql)
Context is reverted back to the original DB once the query is executed.
August 24, 2010 at 9:16 am
awesome, this is wonderfull. Thanks
August 24, 2010 at 1:04 pm
all right another pbm now. Create table works fine but create/alter procedure doesn't work. Can you please take a look at the script which i have attached. Basically i have grouped the servers in 2008 client and want to use this script to loop across different dbs and run the scripts. Create table works, but doesnt work for create/alter sp's, views. Please take a look..thanks
August 24, 2010 at 1:12 pm
You have a BEGIN in your stored procedure code, but no END. That is probably your error.
One of the problems with dynammic SQL is that it can sometimes be difficult to debug...
August 24, 2010 at 1:14 pm
Have you thought about using replication? It seems like that's what you're trying to do...
August 24, 2010 at 4:11 pm
jvanderberg (8/24/2010)
You have a BEGIN in your stored procedure code, but no END. That is probably your error.One of the problems with dynammic SQL is that it can sometimes be difficult to debug...
I did test with end also same error "'CREATE/ALTER PROCEDURE' must be the first statement in a query batch."
August 24, 2010 at 4:13 pm
jvanderberg (8/24/2010)
Have you thought about using replication? It seems like that's what you're trying to do...
no replication cannot be an option, firstly setup is complicated as i am talking about 200 gb dbs with around 20-30 secondly maintenance would be an overhead. I did find one sure shot solution but is little lengthy , trying to find a better one. Thanks
August 24, 2010 at 4:26 pm
As the error states, create/alter procedure must be the first statement in the batch. In your dynamic query, you need to separate the USE from the CREATE/ALTER with a batch separator.
You need to add a GO after the USE statement and before the CREATE/ALTER.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
August 25, 2010 at 8:50 am
Jeffrey Williams-493691 (8/24/2010)
As the error states, create/alter procedure must be the first statement in the batch. In your dynamic query, you need to separate the USE from the CREATE/ALTER with a batch separator.You need to add a GO after the USE statement and before the CREATE/ALTER.
Thanks . Tried doesn't work, same error. :unsure:
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply