我希望每个月使用sp_send_dbmail
发送自动报告。我已经在过去设置了基本的报告,但无论出于何种原因,我目前的报告都无法做到这一点。我有两个查询与Union语句连接在一起。单独,每个查询结果都可以通过电子邮件发送,但是使用union语句我收到以下错误:
无法初始化sqlcmd库,错误号为-2147467259
由于此错误,我一直在查询我的查询。我的查询运行得很好但是当我将它分配给变量时,它给出了一个非常一般的错误:
消息102,级别15,状态1,行29'='附近的语法不正确。
有没有人知道我可以改变什么?
DECLARE @QueryString NVARCHAR(800) = N'
Select
STAFFMSTR.SMMNUM
,STAFFMSTR.SMMLNM
,STAFFMSTR.SMMFNM
,STAFFMSTR.SMMSTS
,STFMST.SMEMAL
,STFMST.SMUSRI
,''In Essentia, Not HRIS''
From
Essentia.dbo.STAFFMSTR
left outer join hris.dbo.hris on (HRIS.[Empl ID] = STAFFMSTR.SMMNUM)
left outer join essentia.dbo.STFMST on stfstf = SMMNUM
WHERE
HRIS.[Empl ID] IS NULL
and SMMSTS = ''A''
and SMMNUM not in (1,2,10001,99999,8447)
UNION
Select
STAFFMSTR.SMMNUM
,STAFFMSTR.SMMLNM
,STAFFMSTR.SMMFNM
,STAFFMSTR.SMMSTS
,STFMST.SMEMAL
,STFMST.SMUSRI
,''Active in Essentia, Termed in HRIS''
From
Essentia.dbo.STAFFMSTR
-- Line 29 -- left outer join hris.dbo.hris on (HRIS.[Empl ID] = STAFFMSTR.SMMNUM)
left outer join essentia.dbo.STFMST on stfstf = SMMNUM
WHERE
SMMSTS = ''A''
and [EE Status] = ''T''
and SMMNUM not in (1,2,10001,99999,8447)
order by SMMNUM'
Exec (@querystring)
答案 0 :(得分:1)
首先尝试声明您的变量(将列的大小从800
更改为MAX
):
DECLARE @QueryString NVARCHAR(MAX);
然后初始化它(删除N
字符串文字):
SET @QueryString = '
Select
STAFFMSTR.SMMNUM
,STAFFMSTR.SMMLNM
,STAFFMSTR.SMMFNM
,STAFFMSTR.SMMSTS
,STFMST.SMEMAL
,STFMST.SMUSRI
,''In Essentia, Not HRIS''
From
Essentia.dbo.STAFFMSTR
left outer join hris.dbo.hris on (HRIS.[Empl ID] = STAFFMSTR.SMMNUM)
left outer join essentia.dbo.STFMST on stfstf = SMMNUM
WHERE
HRIS.[Empl ID] IS NULL
and SMMSTS = ''A''
and SMMNUM not in (1,2,10001,99999,8447)
UNION
Select
STAFFMSTR.SMMNUM
,STAFFMSTR.SMMLNM
,STAFFMSTR.SMMFNM
,STAFFMSTR.SMMSTS
,STFMST.SMEMAL
,STFMST.SMUSRI
,''Active in Essentia, Termed in HRIS''
From
Essentia.dbo.STAFFMSTR
-- Line 29 -- left outer join hris.dbo.hris on (HRIS.[Empl ID] = STAFFMSTR.SMMNUM)
left outer join essentia.dbo.STFMST on stfstf = SMMNUM
WHERE
SMMSTS = ''A''
and [EE Status] = ''T''
and SMMNUM not in (1,2,10001,99999,8447)
order by SMMNUM';
然后执行它(将EXEC
更改为EXECUTE
):
EXECUTE(@QueryString);