How can I write a stored procedure which can switch to new databases using something like "use DBASENAME", but without knowing the dbase names before the procedure runs? The example below was my attempt to do this using SP_EXECUTESQL. The reason for this is so that I can call stored procedures within my stored procedure so that they will use the data in the desired dbase.
set nocount on
declare @NSqlStatment as nvarchar(500)
declare @DbaseName as varchar(20)
set @DbaseName = 'poports'
SET @NSqlStatment = 'use ' + @DbaseName
EXEC SP_EXECUTESQL @NSqlStatment
November 7, 2007 1:40 PM
December 2, 2007 12:09 PM