操作数类型冲突:日期尝试使用动态SQL时与INT不兼容
我创建了一个SQL语句,该语句使用变量用于开始日期和结束日期,这是
declare @startDate varchar(20) = '2022-06-01'
declare @endDate varchar(20) = '2022-06-29'
select distinct instanceId as [JourneyID],
CAST((select case when ISDATE(questionComment) = 1 THEN CAST(questionComment as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 170346 and instanceId = qanda.instanceId ) as varchar) as [AuditDate]
,CAST((select case when ISDATE(UpdateDate) = 1 THEN CAST(UpdateDate as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 176537 and instanceId = qanda.instanceId) as varchar) as [UpdatedDate]
from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] qanda
where (select case when ISDATE(questionComment) = 1 THEN CAST(questionComment as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 170346 and instanceId = qanda.instanceId ) >= CAST(@startDate as date)
and (select case when ISDATE(questionComment) = 1 THEN CAST(questionComment as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 170346 and instanceId = qanda.instanceId ) <= CAST(@endDate as date)
and UpdateDate is not null
我尝试使用Dynamic SQL运行的,因为我需要动态传递服务器和数据库名称,因为此查询从SSRS报告中使用。
我得到了操作数类型冲突:日期与int不兼容
这是我的动态sql,
@startDate varchar(15),
@endDate varchar(15),
set @OPENQUERY = 'Select [JourneyID], [AuditDate], [UpdatedDate] from OPENQUERY(' + @ServerName + ','''
set @sql = 'Select distinct instanceId as [JourneyID],
CAST((select case when ISDATE(questionComment) = 1 THEN CAST(questionComment as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 170346 and instanceId = qanda.instanceId ) as varchar) as [AuditDate]
,CAST((select case when ISDATE(UpdateDate) = 1 THEN CAST(UpdateDate as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 176537 and instanceId = qanda.instanceId) as varchar) as [UpdatedDate]
from ' + @DatabaseName + '.dbo.AB_SurveyQandA qanda where
(select case when ISDATE(questionComment) = 1 THEN CAST(questionComment as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 170346 and instanceId = qanda.instanceId ) >= ' + CAST(@startDate as nvarchar) + '
and (select case when ISDATE(questionComment) = 1 THEN CAST(questionComment as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 170346 and instanceId = qanda.instanceId ) <= ' + CAST(@endDate as nvarchar) +
' and UpdateDate is not null
'')'
我尝试铸造@startdate和 @enddate作为日期,但后来说日期与操作数不相容 +
我需要做些什么才能获得此功能。在动态SQL字符串中工作吗?
I have created a sql statement which uses variables for the start and end dates and this works
declare @startDate varchar(20) = '2022-06-01'
declare @endDate varchar(20) = '2022-06-29'
select distinct instanceId as [JourneyID],
CAST((select case when ISDATE(questionComment) = 1 THEN CAST(questionComment as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 170346 and instanceId = qanda.instanceId ) as varchar) as [AuditDate]
,CAST((select case when ISDATE(UpdateDate) = 1 THEN CAST(UpdateDate as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 176537 and instanceId = qanda.instanceId) as varchar) as [UpdatedDate]
from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] qanda
where (select case when ISDATE(questionComment) = 1 THEN CAST(questionComment as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 170346 and instanceId = qanda.instanceId ) >= CAST(@startDate as date)
and (select case when ISDATE(questionComment) = 1 THEN CAST(questionComment as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 170346 and instanceId = qanda.instanceId ) <= CAST(@endDate as date)
and UpdateDate is not null
I am trying to run this using dynamic SQL as I need to pass in server and database name dynamically as this query is used from an SSRS report.
I get a Operand type clash: date is incompatible with int
This is my dynamic SQL
@startDate varchar(15),
@endDate varchar(15),
set @OPENQUERY = 'Select [JourneyID], [AuditDate], [UpdatedDate] from OPENQUERY(' + @ServerName + ','''
set @sql = 'Select distinct instanceId as [JourneyID],
CAST((select case when ISDATE(questionComment) = 1 THEN CAST(questionComment as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 170346 and instanceId = qanda.instanceId ) as varchar) as [AuditDate]
,CAST((select case when ISDATE(UpdateDate) = 1 THEN CAST(UpdateDate as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 176537 and instanceId = qanda.instanceId) as varchar) as [UpdatedDate]
from ' + @DatabaseName + '.dbo.AB_SurveyQandA qanda where
(select case when ISDATE(questionComment) = 1 THEN CAST(questionComment as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 170346 and instanceId = qanda.instanceId ) >= ' + CAST(@startDate as nvarchar) + '
and (select case when ISDATE(questionComment) = 1 THEN CAST(questionComment as date) END from [AbellioWMTData_April_21].[dbo].[AB_SurveyQandA] where protoQuestionId = 170346 and instanceId = qanda.instanceId ) <= ' + CAST(@endDate as nvarchar) +
' and UpdateDate is not null
'')'
I have tried casting @startDate and @endDate as date but then it says date is incompatible with operand +
What do I need to do to be able to get this to work in a dynamic SQL string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据Ed Bangga的建议,这里是SQL打印的结果
这是印刷SQL的结果
As Per Ed Bangga suggestion here is the result of the SQL print
here is the result of the print SQL