Passing down dbname as parameter in stored proc

I have a collection of stored procedures that I use for data migration purposes. I have to run this collection of procs on a number of different databases, so what I would like is to be able to pass the database name into each proc, instead of retyping each of them. or having different copies of the procedures about the place.

i.e each statement contains the reference to the DB (INSERT INTO DATABASENAME.dbo.TABLENAME)

Ideally, some sort of global variable would be good - that I could refer to in each stored proc, and then just pass it down to them all in an overarching proc.

Can this be done? and how does the syntax look when referring to it - is it @databasevar.dbo.TABLENAME - or do I need to create it all as one string, and then execuite the string.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David ToddSenior DBACommented:

afaik you will need to create a string in the procedure, and dynamically execute that string.

Dynamic SQL is less secure than normal SQL.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
as dtodd indicates, the "short" version will be dynamic sql (see code snippet).
however, I do agree that it is not the "best" method in terms of security ...

note: whenever inside the @sql = ' ... ' you need to put a single quote, you will have to duplicate it: @sql = '  insert into yourtable values ('mc''afeee ') '
CREATE PROCEUDRE do_work (@dbname sysname)
DECLARE @sql varchar(2000)
set @sql = 'USE [' + @dbname + '
EXEC (@sql)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
j_young_80Author Commented:
So is there a concept of a global variable in stored procs? something like the @@ identity?

Also - the line you have listed;

CREATE PROCEUDRE do_work (@dbname sysname) - is this passing a type of sysname into the proc? wouldn't this just be a string?

Agreed on the security side of things - but in this instance it's probably not a huge issue - I had hoped there would be a more elegant solution than building up strings however.
David ToddSenior DBACommented:

iirc sysname in SQL 2000 is close to varchar( 128 ). So yes it is just a string. But sysname is strictly correct, and I use it as a type in this sort of situation myself.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.