Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old May 13th, 2004, 03:54 AM
Fredjeu Fredjeu is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 3 Fredjeu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Dynamic Database Name

Hi guys,
I have a question,
how can I put an SQL statement with a dynamic database name?

I really don't know,
is it like

Declare $dbname varchar(30)
select * from $dbname.dbo.Customer

=> $dbname is the variable
but this is wrong,
does anyone know the good sql statement?

Thanks!

Reply With Quote
  #2  
Old May 13th, 2004, 09:35 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,760 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 5 h 18 m 17 sec
Reputation Power: 443
You can create a dynamic connectionstring in a function.
Code:
Dynamic Database Function
Function ConnectToDatabase(strDatabaseName)
   myConnectionString = "Provider=sqloledb;Data Source='" & strDatabaseName & "';Initial Catalog=Customer;User Id=sa;Password=password;"
End Function

Dynamic Database and Table Function
Function ConnectToDatabase(strDatabaseName, strTableName)
   myConnectionString = "Provider=sqloledb;Data Source='" & strDatabaseName & "';Initial Catalog='" & strTableName & "';User Id=sa;Password=password;"
End Function

Then call them like this
Set Conn = Server.CreateObject("ADODB.Connection")

Conn.Open ConnectToDatabase(DatabaseName)
or
Conn.Open ConnectToDatabase(DatabaseName, TableName)

Reply With Quote
  #3  
Old May 14th, 2004, 06:37 PM
code-kitty code-kitty is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 10 code-kitty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
If you want to use just straight sql, you can use the system stored procedure SP_EXECUTESQL to execute a dynamic sql statement.
Code:
DECLARE @dbname nvarchar(100), @sqlstm nvarchar(500), @sqlparams nvarchar(200), @error int
SELECT @dbname = N'LoadStatsPremium01'
SET @sqlstm = N'select * from ' + @dbname + N'.dbo.Customer'
/* In this example, @sqlparams is not used since there are no parameters */
@error = EXECUTE SP_EXECUTESQL @sqlstm, @sqlparams


If you use SP_EXECUTESQL you can use arguments. The arguments can not be identifiers of database objects (i.e. table names, database names, column names, etc), but it is useful if you have other variables that change such as the customer id.

Code:
/* ASSUMING YOU ALREADY HAVE AN INPUT VARIABLE FOR CUSTOMER ID CALLED @CID */
DECLARE @dbname nvarchar(100), @sqlstm nvarchar(500), @sqlparams nvarchar(200)
SELECT @dbname = N'LoadStatsPremium01'
SELECT @sqlparams = N'@customerId int'
SET @sqlstm = N'select * from ' + @dbname + N'.dbo.Customer WHERE CustomerId=@customerId'
/* Here @sqlparams has the parameter list/declaration, 
   then the arguments follow (@CID) */
@error = EXECUTE SP_EXECUTESQL @sqlstm, @sqlparams, @CID


You can read more about SP_EXECUTESQL at http://msdn.microsoft.com/library/d..._ea-ez_2h7w.asp.

I hope this helps.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Dynamic Database Name


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway