•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 426,478 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,180 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums
Views: 1499 | Replies: 1
![]() |
•
•
Join Date: Aug 2007
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
dear to all,
i want to make my database name as a dynamic one which i'll make it as as a parameter value in a stored procedure.
pls help me of what the codes should be. any hint or something.
i've tried:
declare @data varchar(10)
set @data = dbName
USE @data
this is not working. error shows. pls help.
i want to make my database name as a dynamic one which i'll make it as as a parameter value in a stored procedure.
pls help me of what the codes should be. any hint or something.
i've tried:
declare @data varchar(10)
set @data = dbName
USE @data
this is not working. error shows. pls help.
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,165
Reputation:
Rep Power: 7
Solved Threads: 59
You have to concatonate a TSQL string first and then execute it dynamically.
When using the USE keyword to change database context it is only valid until sp_executesql has completed after which the context is returned to the database in which you are running this TSQL. That is why I include an example select statement, it has to be done all in one string to work. The TSQL string HAS to be an nvarchar datatype sp_executesql requires that. Read more details in books online that comes with Sql Server.
declare @tsql nvarchar(4000) declare @dbName varchar(32) set @dbName = 'dbName' set @tsql = 'use ' + @dbName + '; ' set @tsql = @tsql + 'select * from table' exec sp_executesql @tsql
When using the USE keyword to change database context it is only valid until sp_executesql has completed after which the context is returned to the database in which you are running this TSQL. That is why I include an example select statement, it has to be done all in one string to work. The TSQL string HAS to be an nvarchar datatype sp_executesql requires that. Read more details in books online that comes with Sql Server.
![]() |
•
•
•
•
•
•
•
•
DaniWeb MS SQL Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Reading from a text file and using it as a database (Visual Basic 4 / 5 / 6)
- Help on making and using Database in VB (Visual Basic 4 / 5 / 6)
- How do i create a database in vb (Visual Basic 4 / 5 / 6)
- Need help with my database (Visual Basic 4 / 5 / 6)
- Gallery database (MS SQL)
- Sending quotes to my database (PHP)
- database problem (C#)
- Snyc'n Local SQL database online (MS SQL)
- Do uncompression of disk drives affects my Database? (MS SQL)
- Database Question From Newbie (MySQL)
Other Threads in the MS SQL Forum
- Previous Thread: Connectivity problems
- Next Thread: sql server middleware



Linear Mode