2011-09-07 54 views
0

这不是关于使用表变量 - 这是关于使用本地变量在动态SQL游标中携带数据库地址,理论上它的工作原理如下: - 假定全局变量@sql,AnalysisLocation和@sp_executeSql已被宣布。动态SQL将局部变量作为表变量读取?

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 
'[email protected]+' 
WHERE pfc.pfc_status = 0 
AND  year(mtx.pfc_dateofservice) = '+cast(@Year as nvarchar)+' 
'[email protected]+' 
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 

这编译动态代码时不产生语法错误,调用这个程序然而,当:消息1087,级别15,状态2,行27 必须声明表变量“@ServerAndDB_for_pfcAppended”。

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

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

+0

你得到了什么确切的错误信息?如果您提供可运行的代码来演示此问题,这也会很有帮助。 –

+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 
'[email protected]+' 
WHERE pfc.pfc_status = 0 
AND  year(mtx.pfc_dateofservice) = '+cast(@Year as nvarchar)+' 
'[email protected]+' 
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

这证实了我的怀疑。感谢您的答复。 –