Groups | Blog | Home
all groups > sql server programming > may 2007 >

sql server programming : Dynamically changing database context ('USE [@dbnname]') from query analyzer


mplpost NO[at]SPAM yahoo.com
5/14/2007 11:27:47 PM
[quoted text, click to view]
database context(USE [@dbname](tried dynamic sql, both exec() and
sp_executesql, but this doesnt work) .
We would like to run the alter scripts(service pack) for tables and
stored procedures in some specific databases. Our requirement is that
from our web application when each organization is created a separate
database is created for it. The database names of these organizations
are maintained in our main DB. When we supply the service pack
script(this is applied using query analyzer) for any further database
changes, these scripts are to be run in the corresponding organization
DBs too. If we could dynamically switch contexts, from the query
analyzer while applying the service pack script in the main database,
we could have avoided creating a separate tool to update all the
databases. We could run manually in all the databases
, but the issue is that the client applies this service pack and it
would be cumbersome for them to check all the database names and
update it.

If anybody has any workaround for this, would appreciate your
suggestions.

Thanks,
MPL
Uri Dimant
5/15/2007 12:00:00 AM
Hi
You will have to use dynamic sql. Please read Erland's articles to get more
info

http://www.sommarskog.se/dynamic_sql.html


SET @name='msdb'
SET @sql_db='USE '+@name
SET @sql1=(' IF NOT EXISTS (SELECT * FROM sysusers WHERE name=
'''+@login+''')')
SET @sql2=(' BEGIN EXEC sp_grantdbaccess '''+@login+'''')
SET @sql3=(' EXEC sp_addrolemember ''db_owner'','''+@login+''' END')
EXEC (@sql_db+@sql1+@sql2+@sql3)








[quoted text, click to view]

mplpost NO[at]SPAM yahoo.com
5/15/2007 1:41:53 AM
Hi,
Thanks all, just now found some post that says this cannot be done.
One post suggests to rename the DB after running the script. But in my
case this wouldnt be possible since this is being done on a production
database. Let this be a Microsoft wishlist and so we better proceed to
create an application.
Uri, hope u got the mail and the reply post.
Thanks,
MPL

Erland Sommarskog
5/15/2007 10:42:32 PM
(mplpost@yahoo.com) writes:
[quoted text, click to view]

I think it is a much better idea to run this from an application, since
you have more control over other things. I don't think it is a good
idea to run things like this from a query window.

If you are on SQL 2005, you may want to investigate what variable
substituion in SQLCMD can do for you.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
mplpost NO[at]SPAM yahoo.com
5/15/2007 11:53:35 PM
Thanks for the reply.

[quoted text, click to view]
But it would have been a save of time if we could have dynamically
changed from the query analyzer.

[quoted text, click to view]
Will check it out.


Hi Uri,
Sorry I had typed in a long reply for your earlier posting but
somehow it didnt get posted. Thought it would be good to post it again
so anybody who follows the same problem would know what was the
limitations of not using dynamic queries in this particula scenario:

The usage of
"SELECT @sql = 'CREATE VIEW ...'
SELECT @sp_executesql = quotename(@dbname) + '..sp_executesql'
EXEC @sp_executesql @sql"
as suggested in the URL u posted, will require a tremendous amount of
modification in our script, which creates about 1000 objects and has
140,000 lines of codes and besides this script in intself uses a lot
of dynamic queries and it would be very difficult to get "single
quotes" synchronized.


The usage of ' USE '+ @newdbname+' GO SCRIPT TO BE INCLUDED HERE '
would require the script to be included within the quotes itself.
Again the earlier problems arises, 140,000 lines of codes and dynamic
queries within the scrip to be synchrnoized. If only the dynamic 'USE
'+@dbname would have changed the context of the query analyzer.
Hope this would be a microsoft wishlist.

Thanks,
MPLPost



mplpost NO[at]SPAM yahoo.com
5/16/2007 1:42:12 AM
Good Analogy:)
Erland Sommarskog
5/16/2007 7:29:28 AM
(mplpost@yahoo.com) writes:
[quoted text, click to view]

You're up on a ladder, and in your left you have a nail, and in your
right a screwdriver. The hammer is in a box down on the floor in another
room. It may seem that you could save a lot of time if you could push that
nail with the screwdriver.

[quoted text, click to view]

http://connect.microsoft.com/SqlServer is a place where you add things to
the wishlist.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
AddThis Social Bookmark Button