动态SQL将局部变量读取为表变量?

时间:2011-09-07 18:38:57

标签: tsql variables dynamic local

这不是关于使用表变量 - 这是关于使用局部变量在动态SQL游标中携带db地址,理论上它将如下工作: - 假设已声明全局变量@sql,AnalysisLocation和@sp_executeSql。

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

这在编译动态代码时不会产生语法错误,但是在调用此过程时:Msg 1087,Level 15,State 2,Line 27 必须声明表变量“@ServerAndDB_for_pfcAppended”。

当我使用这种类型的结构传递位置变量作为一个全局变量从过程外部正确接受它时,但是作为局部变量,它似乎默认假设我打算将它作为一个表变量。

我不想创建表变量。这是一个不可能的结构吗?

1 个答案:

答案 0 :(得分:1)

错误是由于您尝试使用参数化表名称引起的。这是不可能的,只要表名应该是一个参数,就会使用动态查询,基本上是这样的:

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

我认为,在您的情况下,除了使用参数化表名的部分外,光标应该从动态查询中取出。这样的事可能应该这样做:

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