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
 General SQL Server Forums
 New to SQL Server Programming
 Subquery in an Exec

Author  Topic 

AAAV
Posting Yak Master

152 Posts

Posted - 2011-12-16 : 13:48:40
exec ('select empname from [(select databasename
from dbo.SQLDatabaseInstalled where databaseID='DB1').dbo.data_input_master_export')


Is this possible?

I have to get the employee list based on the db.
Like i can configure the sQLDBInstalled to point to a db instance and then this query has to fetch it from the instance i point to.

how to do it?

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-16 : 13:51:15
why are you using dynamic sql?

SELECT empname
FROM SQLDatabaseInstalled
JOIN data_input_master_export
ON ???
where databaseID='DB1'

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

AAAV
Posting Yak Master

152 Posts

Posted - 2011-12-16 : 13:58:50
This is what i want to do

i am checking if a empid is in few databases depending on which db the application is connected to(session)
hence i have to first get the dbname then check if that empid
is in that particular db...

The db the query has to check differs.

exec ('select empid from ['+ exec('select SUBSTRING(databaseconnectionstring,
CHARINDEX (''='',databaseconnectionstring)+1, CHARINDEX ('';'',databaseconnectionstring)-CHARINDEX (''='',databaseconnectionstring)-1)
from dbo.SQLDatabaseInstalled where databasename=''DB1''')
+'].dbo.emp_master_export')


it has to first exec the inner sql and find which db to prefix...
db1.dbo.emp_master_export
or db2.dbo.emp_master_export and so on.
Go to Top of Page
   

- Advertisement -