开发者

Dynamic SQL reads a local variable as a Table Variable?

This is NOT about using a table variable - this is开发者_StackOverflow about using a local variable to carry a db address within a dynamic SQL cursor which theoretically would work as follows: -- Assume the global variables @sql, AnalysisLocation, and @sp_executeSql have been declared.

ALTER PROCEDURE [dbo].[sp_AggregateCompliance_Report]
@clientID int,
@InvScrDBLocation nvarchar(250),
@JoinFilter nvarchar(max) = '',
@Criteria nvarchar(max) = '',
@Year int = NULL

as

declare @sql nvarchar(4000)


set @sql = '
IF EXISTS (SELECT * FROM sys.tables WHERE name = ''tmp_Aggregate_Compliance_counts'')
TRUNCATE TABLE tmp_Aggregate_Compliance_counts
ELSE
CREATE TABLE tmp_Aggregate_Compliance_counts (
pfc_fk_prv_pkid int,
RxYear int,
RxMonth int,
Compliance decimal (6,5))
' print @sql  EXEC sp_executesql @sql


SET @Criteria = isnull(case when @Criteria like 'WHERE %' then 'AND '+substring(@criteria,7,len(@criteria)-6) else @Criteria end ,'')
SET @Year = isnull(@year, year(getdate())-1)


 set @sql = '
DECLARE @fk_cli_pkid INT
    ,   @ServerAndDB_for_pfcAppended nvarchar(100)

DECLARE client_set CURSOR FOR
SELECT  DISTINCT mtx.fk_cli_pkid, SettingValue+ ''.dbo.pfc_appended''
FROM    mtx_ComplianceAndEarlyRefill_tracking AS mtx
JOIN    prola7.Invoice_Screens.dbo.client_definition AS def
ON      mtx.fk_cli_pkID = def.fk_cli_pkid
AND     fk_lkSettings_pkID  = 45
AND     RecordStatus = 1 

OPEN    client_set

FETCH next FROM client_set
INTO    @fk_cli_pkid, @ServerAndDB_for_pfcAppended

WHILE   @@FETCH_STATUS = 0 BEGIN

INSERT INTO tmp_Aggregate_Compliance_counts (pfc_fk_prv_pkid, RxYear, RxMonth, Compliance)

SELECT  pfc.pfc_fk_prv_pkid
    ,   year(mtx.pfc_dateofservice) AS RxYear
    ,   0 AS RxMonth
    ,   cast(mtx.Compliance as decimal (6,5))
FROM    mtx_ComplianceAndEarlyRefill_tracking AS mtx
LEFT OUTER JOIN @ServerAndDB_for_pfcAppended AS pfc
ON      mtx.pp_clientfile   = pfc.pp_clientfile
AND     mtx.pp_mirror_pkid  = pfc.pp_mirror_pkid
AND     mtx.fk_cli_pkid     = @fk_cli_pkid
'+@JoinFilter+'
WHERE   pfc.pfc_status = 0
AND     year(mtx.pfc_dateofservice) = '+cast(@Year as nvarchar)+'
'+@Criteria+'
GROUP BY pfc.pfc_fk_prv_pkid, year(mtx.pfc_dateofservice)


FETCH next FROM client_set
INTO    @fk_cli_pkid, @ServerAndDB_for_pfcAppended

END

CLOSE client_set
DEALLOCATE client_set
' print @sql  EXEC sp_executesql @sql

This creates no syntax errors when compiling the dynamic code, however when calling this procedure: Msg 1087, Level 15, State 2, Line 27 Must declare the table variable "@ServerAndDB_for_pfcAppended".

When I use this type of structure passing the location variable in as a global variable from outside the procedure it accepts it correctly, however as a local variable it seems to default to presuming I intend it to be a table variable.

I do NOT want to create a table variable. Is this an impossible structure?


The error is caused by the fact that you are attempting to have a parametrised table name. This is not possible, and whenever a table name should be a parameter, a dynamic query is used, basically like this:

SET @sql = 'SELECT … FROM ' + @tablename + ' WHERE …'

I think, in your situation the cursor should be taken out of the dynamic query, except for the part that uses the parametrised table name. Something like this should probably do:

ALTER PROCEDURE [dbo].[sp_AggregateCompliance_Report]
@clientID int,
@InvScrDBLocation nvarchar(250),
@JoinFilter nvarchar(max) = '',
@Criteria nvarchar(max) = '',
@Year int = NULL

as

declare @sql nvarchar(4000)


set @sql = '
IF EXISTS (SELECT * FROM sys.tables WHERE name = ''tmp_Aggregate_Compliance_counts'')
TRUNCATE TABLE tmp_Aggregate_Compliance_counts
ELSE
CREATE TABLE tmp_Aggregate_Compliance_counts (
pfc_fk_prv_pkid int,
RxYear int,
RxMonth int,
Compliance decimal (6,5))
' print @sql  EXEC sp_executesql @sql


SET @Criteria = isnull(case when @Criteria like 'WHERE %' then 'AND '+substring(@criteria,7,len(@criteria)-6) else @Criteria end ,'')
SET @Year = isnull(@year, year(getdate())-1)


DECLARE @fk_cli_pkid INT
    ,   @ServerAndDB_for_pfcAppended nvarchar(100)

DECLARE client_set CURSOR FOR
SELECT  DISTINCT mtx.fk_cli_pkid, SettingValue+ ''.dbo.pfc_appended''
FROM    mtx_ComplianceAndEarlyRefill_tracking AS mtx
JOIN    prola7.Invoice_Screens.dbo.client_definition AS def
ON      mtx.fk_cli_pkID = def.fk_cli_pkid
AND     fk_lkSettings_pkID  = 45
AND     RecordStatus = 1 

OPEN    client_set

FETCH next FROM client_set
INTO    @fk_cli_pkid, @ServerAndDB_for_pfcAppended

WHILE   @@FETCH_STATUS = 0 BEGIN

 set @sql = '
INSERT INTO tmp_Aggregate_Compliance_counts (pfc_fk_prv_pkid, RxYear, RxMonth, Compliance)

SELECT  pfc.pfc_fk_prv_pkid
    ,   year(mtx.pfc_dateofservice) AS RxYear
    ,   0 AS RxMonth
    ,   cast(mtx.Compliance as decimal (6,5))
FROM    mtx_ComplianceAndEarlyRefill_tracking AS mtx
LEFT OUTER JOIN @ServerAndDB_for_pfcAppended AS pfc
ON      mtx.pp_clientfile   = pfc.pp_clientfile
AND     mtx.pp_mirror_pkid  = pfc.pp_mirror_pkid
AND     mtx.fk_cli_pkid     = @fk_cli_pkid
'+@JoinFilter+'
WHERE   pfc.pfc_status = 0
AND     year(mtx.pfc_dateofservice) = '+cast(@Year as nvarchar)+'
'+@Criteria+'
GROUP BY pfc.pfc_fk_prv_pkid, year(mtx.pfc_dateofservice)
' print @sql  EXEC sp_executesql @sql


FETCH next FROM client_set
INTO    @fk_cli_pkid, @ServerAndDB_for_pfcAppended

END

CLOSE client_set
DEALLOCATE client_set
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜