Hello,
I'm trying to avoid using dynamic sql as much as possible. In the environment for which I must write some procedures the databases do not have standard names (will have multiple db's of the same structure but with slightly different names on one server).
Most of the procedures on the system are being passed the database name as a string, which is then being incorporated into a lot of dynamic sql. In my case I'm trying to address the issue of coding SQL to be more or less generic, for all db iterations.
I'm trying to do something like this and don't seem to be having too much luck:
declare @TargetDB varChar(7), @sqltxt varchar(100)
set @targetcdidb = 'dbname'
set @sqltxt = 'use ' + @targetcdidb
exec (@sqltxt)
select * from Tbl_In_dbname
My hope was the USE statement would function through EXEC the same way it does in the Query Analyzer, allowing me to change the db with a minimum of dynamic sql. This doesn't seem to change the DB though.
Anyone know how to make something like this work, or know a better way to accomplish this task?
Thanks,
O.
I'm trying to avoid using dynamic sql as much as possible. In the environment for which I must write some procedures the databases do not have standard names (will have multiple db's of the same structure but with slightly different names on one server).
Most of the procedures on the system are being passed the database name as a string, which is then being incorporated into a lot of dynamic sql. In my case I'm trying to address the issue of coding SQL to be more or less generic, for all db iterations.
I'm trying to do something like this and don't seem to be having too much luck:
declare @TargetDB varChar(7), @sqltxt varchar(100)
set @targetcdidb = 'dbname'
set @sqltxt = 'use ' + @targetcdidb
exec (@sqltxt)
select * from Tbl_In_dbname
My hope was the USE statement would function through EXEC the same way it does in the Query Analyzer, allowing me to change the db with a minimum of dynamic sql. This doesn't seem to change the DB though.
Anyone know how to make something like this work, or know a better way to accomplish this task?
Thanks,
O.