存储过程中的动态SQL-日期时间参数

存储过程中的动态SQL-日期时间参数,sql,stored-procedures,dynamic-sql,Sql,Stored Procedures,Dynamic Sql,获取了一个已转换为动态SQL的存储过程,原因是在执行该过程之前,将从外部系统向该过程传递额外的SQL 从字符串转换日期时间时转换失败。 以下是完整的存储过程: USE [DBName]; GO SET ANSI_NULLS ON; GO SET QUOTED_IDENTIFIER ON; GO ALTER PROCEDURE [DB_Admin].[GetMiniCalendarDataNew] @userID int, @startDate datetime, @endDate datetim

获取了一个已转换为动态SQL的存储过程,原因是在执行该过程之前,将从外部系统向该过程传递额外的SQL

从字符串转换日期时间时转换失败。 以下是完整的存储过程:

USE [DBName];
GO
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
ALTER PROCEDURE [DB_Admin].[GetMiniCalendarDataNew]
@userID int, @startDate datetime, @endDate datetime, @JVID int = 0

WITH EXEC AS CALLER
AS

set nocount on

declare @SQLQuery AS NVARCHAR(max)

declare @t as table([day] int, [end] datetime, sortorder int, jv int)

SET @SQLQuery= 'insert into @t([day], [end], sortorder, jv)
select day((A.STARTTIME)) [day], max(a.endtime) ''end'', 3 sortorder,min(a.jv) jv 
from DB_Admin.CSTM_CALENDAR a
join DB_Admin.CSTM_CALENDAR b on a.id<>b.id
join DB_Admin.CSTM_CALENDARParticipants m1 on a.id=m1.CalendarID
join DB_Admin.CSTM_CALENDARParticipants m2 on b.id=m2.CalendarID
join DB_Admin.DTree DTree on a.FolderDataID=DTree.DataID
where a.starttime between ' + CAST(@startDate AS DATETIME) + ' AND ' + CAST(@endDate AS DATETIME) +
' AND DTree.OwnerID > 0
and b.starttime between ' + CAST(@startDate AS DATETIME) + ' AND ' + CAST(@endDate AS DATETIME) +
' AND a.starttime<b.endtime --find overlapping meetings
AND a.endtime>b.starttime --find overlapping meetings
AND M1.PARTICIPANT IN (
select id from DB_Admin.kuaf where id in (
    select id from DB_Admin.kuafchildren
    where childid=' +@userID+')
    or id=' +@userID+
')
AND M2.PARTICIPANT IN (
select id from DB_Admin.kuaf where id in (
    select id from DB_Admin.kuafchildren
    where childid='+@userID+') 
    or id='+@userID+
')'+

--Filter on JV
' AND ( exists (select 1 where a.jv='+@JVID+')
or    '+@JVID+'=0'+
')'+

'group by day(A.STARTTIME)'

+' insert into @t ([day], [end], sortorder, jv)
select day(A.STARTTIME) [day], max(a.endtime) ''end'', 2 SORTORDER,min(a.jv) jv
from DB_Admin.CSTM_CALENDAR a
join DB_Admin.CSTM_CALENDAR b on a.id<>b.id
join DB_Admin.CSTM_CALENDARParticipants m1 on a.id=m1.CalendarID
join DB_Admin.CSTM_CALENDARParticipants m2 on b.id=m2.CalendarID
join DB_Admin.DTree DTree on a.FolderDataID=DTree.DataID
where a.starttime between ' + CAST(@startDate AS DATETIME) +' AND ' +CAST(@endDate AS DATETIME)+
' AND DTree.OwnerID > 0
--Filter on JV
AND ( exists (select 1 where a.jv='+@JVID+')
or  '+@JVID+'=0'+')
and M1.PARTICIPANT IN (
select id from DB_Admin.kuaf where id in (
    select id from DB_Admin.kuafchildren
    where childid='+@userID+') 
    or id='+@userID+
')
group by (A.STARTTIME)'+

' insert into @t ([day], [end], sortorder, jv)
select day(A.STARTTIME) [day], max(a.endtime) ''end'', 1 SORTORDER,min(a.jv) jv
from DB_Admin.CSTM_CALENDAR a
join DB_Admin.CSTM_CALENDARParticipants m1 on a.ID=m1.CalendarID
join DB_Admin.DTree DTree on a.FolderDataID=DTree.DataID
where a.starttime between '+CAST(@startDate AS DATETIME)+' AND '+CAST(@endDate AS DATETIME)+
' AND DTree.OwnerID > 0
--Filter on JV
AND ( exists (select 1 where a.jv='+@JVID+')
or    '+@JVID+'=0'+ ')
and M1.PARTICIPANT NOT IN (
select id from DB_Admin.kuaf where id in (
    select id from DB_Admin.kuafchildren
    where childid='+@userID+') 
    or id='+@userID+'
)
group by (A.STARTTIME)'


--format query
+' select [day], max(month('+CAST(@startDate AS DATETIME)+' [month], max(year('+CAST(@endDate AS DATETIME)+')) [year], max([end]) ''end'', 
    case 
        when max(sortorder)=3 then ''Overlapping'' 
        when max(sortorder)=2 then ''Participating''
        when max(sortorder)=1 then ''Existing''
        when max(sortorder)=0 then ''Empty''

    end sortOrder , min(jv) JVID
from @t
group by [day]
order by sortorder desc'

--EXEC (@SQLQuery)

PRINT (@SQLQuery)

GO

我认为你的问题在于:

'where a.starttime between ' + CAST(@startDate AS DATETIME) + ' AND ' + CAST(@endDate AS DATETIME) +
您应该将其转换为字符串,而不是日期时间。看

好吧,你需要

引用 确保它们是字符串 使他们的语言/区域设置安全 因此:

编辑:

整数错误。您需要强制转换@userID来连接它。
SQL不执行VBA样式的隐式强制转换

对不起,这只会让我看着它眼睛流血。如果您希望人们提供帮助,您应该隔离导致错误的位,并对其进行格式化,以使其更易于阅读。您需要将日期转换为字符串,然后才能连接它们。谢谢,我现在尝试使用此转换建议。我的结果现在无法转换为varchar。将varchar值“insert”转换为@t[day]、[end]、sortorder、jv选择dayA.STARTTIME[day]、maxa.endtime“end”、3 sortorder、mina.jv jv…时转换失败。请尝试将121作为ODBC格式而不是ISO格式。在执行日期转换之前也要打印字符串。日期转换似乎已解决。现在,int转换出现了一个失败的问题:当将varchar值“declare@t as table[day]int,[end]datetime….转换为表时,转换失败了。。。。。转换为数据类型int。将datetime参数临时替换为getdate以使其通过转换错误。
...
where a.starttime between ''' + CONVERT(varchar(30), @startDate, 126) +''' AND ''' + ...
...