March 2, 2004 at 5:28 pm
Hi,
I'm trying to use a local variable in the USE statement but can't seem to get the correct syntax.
Here's what I'm trying ---
declare @db SYSNAME
set @db =
(select TOP 1 Name from dbo.sysdatabases
where name like 'CCM%'
order by crdate DESC )
USE @db
select * from [dbo].[device]
There are several databases stored on the server begining with CCM ( CCM0301, CCM0302, etc). I need the data from the most recent one.
Thanks,
Tom
March 2, 2004 at 8:47 pm
set @db =
(select TOP 1 Name from dbo.sysdatabases
where name like 'CCM%'
order by crdate DESC )
EXEC ('USE '+ @db)
select * from [dbo].[device]
--Jeff Moden
Change is inevitable... Change for the better is not.
March 2, 2004 at 9:10 pm
declare @db SYSNAME
set @db =
(select TOP 1 Name from dbo.sysdatabases
where name like 'CCM%'
order by crdate DESC )
exec ('USE '+@db+';select * from [dbo].[device]')
March 2, 2004 at 9:13 pm
That works, thanks for the help
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply