环境: SQL Server 2008 R2。无法访问正确的工具(sql server management studio等)。必须在广告系列管理GUI的高级sql标签中创建(因此只能通过查看日志来查看错误!)。
问题:我正在尝试创建一个动态sql视图,该视图引用了许多在系统表中列出其表名的临时表。我的背景是oracle和VBA,我在SQL Server中没有太多的知识。我需要动态执行此操作的原因是因为每次在GUI中更新段时,后端临时表名称都会更改。所以我需要一个按需例程,让我知道他们的时间点临时表名是什么。
查看我想在程序中创建(可以根据前缀为“%EX_MBR%”的表格数量进行更改)
Create view dbo.VW_Exclusions_Matrix as select a.durable_mbr_acc_id
,case when TB_EX_MBR_COMPLAINTS.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_COMPLAINTS
,case when TB_EX_MBR_DECEASED.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_DECEASED
,case when TB_EX_MBR_DO_NOT_EMAIL.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_DO_NOT_EMAIL
,case when TB_EX_MBR_DO_NOT_MAIL.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_DO_NOT_MAIL
,case when TB_EX_MBR_DO_NOT_MARKET_ALL.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_DO_NOT_MARKET_ALL
,case when TB_EX_MBR_DO_NOT_PHONE.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_DO_NOT_PHONE
,case when TB_EX_MBR_DO_NOT_SMS.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_DO_NOT_SMS
,case when TB_EX_MBR_EMAIL_BOUNCE_HARD.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_EMAIL_BOUNCE_HARD
,case when TB_EX_MBR_EMAIL_BOUNCE_SOFT.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_EMAIL_BOUNCE_SOFT
,case when TB_EX_MBR_FIN_PLAN_REFERRAL.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_FIN_PLAN_REFERRAL
,case when TB_EX_MBR_NO_CONTRIBS_12MTHS.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_NO_CONTRIBS_12MTHS
,case when TB_EX_MBR_INVALID_TFN.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_INVALID_TFN
,case when TB_EX_MBR_OVERSEAS.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_OVERSEAS
,case when TB_EX_MBR_STAFF.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_STAFF
,case when TB_EX_MBR_RESEARCH_OPT_OUT.durable_mbr_acc_id is null then 'N' ELSE 'Y' END AS EX_MBR_RESEARCH_OPT_OUT
from (dbo.UACS_1843_y a LEFT JOIN dbo.uacs_1799_1m TB_EX_MBR_COMPLAINTS on a.durable_mbr_acc_id = TB_EX_MBR_COMPLAINTS.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1n TB_EX_MBR_DECEASED on (a.durable_mbr_acc_id = TB_EX_MBR_DECEASED.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1o TB_EX_MBR_DO_NOT_EMAIL on (a.durable_mbr_acc_id = TB_EX_MBR_DO_NOT_EMAIL.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1p TB_EX_MBR_DO_NOT_MAIL on (a.durable_mbr_acc_id = TB_EX_MBR_DO_NOT_MAIL.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1q TB_EX_MBR_DO_NOT_MARKET_ALL on (a.durable_mbr_acc_id = TB_EX_MBR_DO_NOT_MARKET_ALL.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1r TB_EX_MBR_DO_NOT_PHONE on (a.durable_mbr_acc_id = TB_EX_MBR_DO_NOT_PHONE.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1t TB_EX_MBR_DO_NOT_SMS on (a.durable_mbr_acc_id = TB_EX_MBR_DO_NOT_SMS.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1u TB_EX_MBR_EMAIL_BOUNCE_HARD on (a.durable_mbr_acc_id = TB_EX_MBR_EMAIL_BOUNCE_HARD.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1v TB_EX_MBR_EMAIL_BOUNCE_SOFT on (a.durable_mbr_acc_id = TB_EX_MBR_EMAIL_BOUNCE_SOFT.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1w TB_EX_MBR_FIN_PLAN_REFERRAL on (a.durable_mbr_acc_id = TB_EX_MBR_FIN_PLAN_REFERRAL.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1j TB_EX_MBR_NO_CONTRIBS_12MTHS on (a.durable_mbr_acc_id = TB_EX_MBR_NO_CONTRIBS_12MTHS.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1s TB_EX_MBR_INVALID_TFN on (a.durable_mbr_acc_id = TB_EX_MBR_INVALID_TFN.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1x TB_EX_MBR_OVERSEAS on (a.durable_mbr_acc_id = TB_EX_MBR_OVERSEAS.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1799_1y TB_EX_MBR_STAFF on (a.durable_mbr_acc_id = TB_EX_MBR_STAFF.durable_mbr_acc_id)
LEFT JOIN dbo.uacs_1861_c TB_EX_MBR_RESEARCH_OPT_OUT on (a.durable_mbr_acc_id = TB_EX_MBR_RESEARCH_OPT_OUT.durable_mbr_acc_id)
该视图目前已使用Excel公式创建,并且可以正常工作。我正在尝试在下面的过程中重新创建公式逻辑......但我不知道我在做什么。
首先进入程序(我是否需要一个程序?)
CREATE PROCEDURE GenerateExclusionMatrix
AS
SET NOCOUNT ON;
Declare @sqAll nvarchar(8000), @sq1 nvarchar(1000), @sq2 nvarchar(1000), @UnicaBase varchar(100), @UnicaFieldName nvarchar(100), @UnicaTempTable nvarchar(100), @TableAlias nvarchar(100), @cnt int := 0;
Declare curP cursor For select Name as Var_FieldName, TempTableName as Var_UnicaTempTable, Name as Var_TableAlias FROM dbo.UA_Segment WHERE dbo.UA_Segment.name like '%EX_MBR%' and activeflag = 1;
SET @sq1 = ''
SET @sq2 = ''
@sq1 = 'CREATE VIEW dbo.VW_EXCLUSIONS_MATRIX AS SELECT a.durable_mbr_acc_id '
OPEN curP
Fetch Next From curP Into @UnicaFieldName, @UnicaTempTable, @TableAlias
While @@Fetch_Status = 0 Begin
SET @UnicaBase = (select distinct TempTableName as Var_UnicaBase FROM dbo.UA_Segment a WHERE a.name = 'IN_MBR_ACTIVE_OR_DEEMED' and ActiveFlag = 1)
open cursor
@cnt := @cnt + 1;
@sq1 := @sq1 + ',case when TB_' + @TableAlias + '.durable_mbr_acc_id is null then '''N''' ELSE '''Y''' END AS ' + @UnicaFieldName
IF (@cnt = 1)
@sq2 := @sq2 + ' from (dbo.UACS' + @UnicaBase + ' a LEFT JOIN dbo.UACS' + @UnicaTempTable + ' TB_' + @TableAlias + ' on a.durable_mbr_acc_id = TB_' + @TableAlias + '.durable_mbr_acc_id)'
ELSE
@sq2 := @sq2 + ' LEFT JOIN dbo.UACS' + @UnicaTempTable + ' TB_' + @TableAlias + ' on (a.durable_mbr_acc_id = TB_' + @TableAlias + '.durable_mbr_acc_id)'
END IF
Fetch Next From curP Into @UnicaFieldName, @UnicaTempTable, @TableAlias
End
@sq1 = @sq1 + @sq2
Close curP
Deallocate curP
exec sp_executesql @sq1
END;
我希望这是有道理的。非常感谢任何帮助。
干杯,
詹姆斯
语法在反馈后更新 - 2014年2月14日下午3:11
答案 0 :(得分:0)
我无法测试代码,但你可以尝试一些不使用游标的东西(这里不需要它们)
CREATE PROCEDURE GenerateExclusionMatrix
AS
DECLARE @UnicaBase nvarchar(100)
SET @UnicaBase = (select TempTableName as Var_UnicaBase FROM dbo.UA_Segment a WHERE a.name = 'IN_MBR_ACTIVE_OR_DEEMED' and ActiveFlag = 1)
DECLARE @SQL1 NVARCHAR(MAX) = 'CREATE VIEW dbo.VW_EXCLUSIONS_MATRIX AS SELECT a.durable_mbr_acc_id '
select @SQL1 = @SQL1 + ',case when TB_' + Name + '.durable_mbr_acc_id is null then ''N'' ELSE ''Y'' END AS ' + Name
FROM dbo.UA_Segment
WHERE dbo.UA_Segment.name like '%EX_MBR%' and activeflag = 1;
DECLARE @SQL2 NVARCHAR(MAX) = 'from (dbo.UACS' + @UnicaBase + ' a '
select @SQL2 = @SQL2 + ' LEFT JOIN dbo.UACS' + TempTableName + ' TB_' + Name + ' on a.durable_mbr_acc_id = TB_' + Name + '.durable_mbr_acc_id'
FROM dbo.UA_Segment
WHERE dbo.UA_Segment.name like '%EX_MBR%' and activeflag = 1;
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = @SQL1 + @SQL2
exec sp_executesql @sq1
END;
更新:我无法运行,但可以查看要创建的生成动态sql http://sqlfiddle.com/#!3/bc9a9/1/0