I'm trying to open a cursor using an SQL statement based on a DB whose name changes from month to month. I have the following code:
SET @dProcDate = '02-01-2007 00:00:00'
SET @cMonth = 'Test_' + CASE WHEN MONTH(@dProcDate) < 10 THEN '0' + CAST(MONTH(@dProcDate) AS char(1)) ELSE CAST(MONTH(@dProcDate) AS char(2)) END +
CAST(YEAR(@dProcDate) AS char(4)) + '.Dbo.SomeTable'
DECLARE cur3011 CURSOR LOCAL FAST_FORWARD
FOR
SELECT cAgency
FROM [@cMonth]
open cur3011
I'm receiving the error 'Invalid Object @cMonth'
Does anybody know of a way to accomplish this?
SET @dProcDate = '02-01-2007 00:00:00'
SET @cMonth = 'Test_' + CASE WHEN MONTH(@dProcDate) < 10 THEN '0' + CAST(MONTH(@dProcDate) AS char(1)) ELSE CAST(MONTH(@dProcDate) AS char(2)) END +
CAST(YEAR(@dProcDate) AS char(4)) + '.Dbo.SomeTable'
DECLARE cur3011 CURSOR LOCAL FAST_FORWARD
FOR
SELECT cAgency
FROM [@cMonth]
open cur3011
I'm receiving the error 'Invalid Object @cMonth'
Does anybody know of a way to accomplish this?