我的查询需要帮助。我试图从1月到2月生成一份清单。但是在我的查询中我不确定哪个部分我做错了或错误它只抓住了二月份的记录。好心提醒 。谢谢你
set @FirstDateOfNextMth = CONVERT(varchar,dateadd(d,-(day(dateadd(m,1,getdate()-2))),dateadd(m,1,getdate())),112)
set @LastDateOfNextMth = CONVERT(varchar,dateadd(d,-(day(dateadd(m,2,getdate()))),DATEADD(m,2,getdate())),112)
--------------------------------------------------
INSERT INTO [dbo].[ProcessLog] ([LogTime] ,[LogDescription] ,[LogRemark])
VALUES (GETDATE(), 'Import Client Info', '')
--------------------------------------------------
SET @sSQL = 'SELECT DISTINCT CHDR.CHDRNUM, ZTRN.CCDATE, CHDR.CRDATE, CLNT.CLNTNUM, CLNT.SURNAME, CLNT.GIVNAME,
CLNT.SECUITYNO, ZCLN.EMAIL from (((MPIDTA.ZTRNPF AS ZTRN
LEFT JOIN MPIDTA.CHDRPF AS CHDR ON ZTRN.RLDGACCT = CHDR.CHDRNUM AND ZTRN.EFFDATE = CHDR.CURRFROM)
LEFT JOIN MPIDTA.CLNTPF AS CLNT ON CHDR.COWNNUM = CLNT.CLNTNUM)
LEFT JOIN MPIDTA.ZCLNPF AS ZCLN ON CLNT.CLNTNUM = ZCLN.CLNTNUM)
where (ZTRN.BATCPFX = ''BA'' AND ZTRN.BATCCOY = ''1''
AND ZTRN.CNTTYPE = ''PTB'' AND ZTRN.TRANDATE >= ''20140101''
AND (ZTRN.EXPIRY_DATE BETWEEN ' + @FirstDateOfNextMth + ' AND ' + @LastDateOfNextMth + '))
AND (CHDR.MPLNUM = '''' AND CHDR.CHDRPFX = ''CH''
AND CHDR.CHDRCOY = ''1'' AND CHDR.VALIDFLAG = ''1'')'
SET @sExe = 'Insert into dbo.TPA_Client_Info Select * FROM OPENQUERY(AS400, ''' + REPLACE(@sSQL, '''', '''''') + ''')'
exec (@sExe)
答案 0 :(得分:0)
问题出在@FirstDateOfNextMth
。
set @FirstDateOfNextMth = CONVERT(varchar,dateadd(d,-(day(dateadd(m,1,getdate()-2))),
dateadd(m,1,getdate())),112)
结果为20150202
由于@FirstDateOfNextMth
为20150202
,因此条件ZTRN.EXPIRY_DATE
仅在feb
个月之间进行过滤。尝试改变这样。
set @FirstDateOfNextMth = DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)