Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 Dynamic DB

Author  Topic 

just.net
Starting Member

24 Posts

Posted - 2010-09-21 : 04:04:38
Hi,
I want to do something like this:

ALTER PROCEDURE [dbo].[GetTest]
@DB varchar(max)
AS
BEGIN
SET NOCOUNT ON;
SELECT [id]
,[date_start]
,[date_end]
,[total_nights]
,[first_name]
,[last_name]
,[rate_code]
,[user_email]
FROM [@DB].[dbo].[Report]
END

how can i selcet from another DB by parmater ?

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-09-21 : 04:19:38
Read about Dynamic Sql.

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


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-09-21 : 04:21:24
ALTER PROCEDURE [dbo].[GetTest]
@DB varchar(max)
AS
BEGIN
SET NOCOUNT ON;

Declare @sql varchar(max)

Set @sql='
SELECT [id]
,[date_start]
,[date_end]
,[total_nights]
,[first_name]
,[last_name]
,[rate_code]
,[user_email]
FROM ' +@DB +'.[dbo].[Report]'

-- print @sql

Exec (@sql)
END

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-09-21 : 04:31:04
Senthil - Nagore.

Maybe you should read the link you posted.

Dynamic sql like this is unsafe. You are not validating the input at all.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2010-09-21 : 04:36:36
quote:
Originally posted by Transact Charlie

Senthil - Nagore.

Maybe you should read the link you posted.

Dynamic sql like this is unsafe. You are not validating the input at all.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION




Ya i aware what you mention! need to validate the input!

here considering the input was valid!

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

just.net
Starting Member

24 Posts

Posted - 2010-09-21 : 04:45:20
Thank you all.
Go to Top of Page
   

- Advertisement -