Sql Datetime throw将数据类型varchar转换为datetime时出错

Jua*_*mez 3 sql sql-server datetime

我有这个存储过程

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET DATEFORMAT DMY -- This line of code was added by my trying to avoid the problem
GO

ALTER PROCEDURE dbo.sp_Hotel_RegistroHuesped 
-- Add the parameters for the stored procedure here
  @p_accion                             int 
, @p_IdRegistroHuesped                  numeric (18,0)
, @p_IntIdHabitacion                    int 
, @p_CheckInFecha                       datetime 
, @p_CheckOutFecha                      datetime
, @p_CheckOutFechaEsperada              datetime 
, @p_NumIdTerceroCondicionesComerciales int 
, @p_StrUsuarioCrea                     usuario  = suser_sname
, @p_DatfechaCrea                       datetime = getdate
, @p_StrUsuarioModifica                 usuario  = NULL
, @p_DatFechaModifica                   datetime = NULL
, @p_ListaHuespedes                     char(400)= null
AS
BEGIN

END     
Run Code Online (Sandbox Code Playgroud)

对我来说没有什么特别的或奇怪的,但是当我尝试像这样执行时我真的被困住了

set dateformat dmy 

exec dbo.sp_Hotel_RegistroHuesped 
    @p_accion=1,
    @p_IdRegistroHuesped=0,
    @p_IntIdHabitacion=37,
    @p_CheckInFecha='11/03/2014 21:48:28.301',
    @p_ListaHuespedes='',
    @p_CheckOutFecha=NULL,
    @p_CheckOutFechaEsperada='11/03/2014 22:48:28.301',
    @p_NumIdTerceroCondicionesComerciales=1
Run Code Online (Sandbox Code Playgroud)

它总是扔

男士8114,Nivel 16,Estado 1,Procedureimiento sp_Hotel_RegistroHuesped,Línea0
将数据类型varchar转换为datetime时出错

这是我正在尝试的变种

set dateformat dmy 
exec dbo.sp_Hotel_RegistroHuesped 
    @p_accion=1,
    @p_IdRegistroHuesped=0,
    @p_IntIdHabitacion=37,
    @p_CheckInFecha='11/03/2014 21:48:28',
    @p_ListaHuespedes='',
    @p_CheckOutFecha=NULL,
    @p_CheckOutFechaEsperada='11/03/2014 22:48:28',
    @p_NumIdTerceroCondicionesComerciales=1
Run Code Online (Sandbox Code Playgroud)

只需删除毫秒部分.

执行此sp的正确方法是什么?

Szy*_*mon 7

由于您使用的日期格式的转换('11/03/2014 22:48:28')取决于语言设置(日/月的顺序等),因此最好使用应该正确识别的ISO标准日期格式 - YYYY-MM-DDTHH:MM:SS:

'2014-03-11T22:48:28'
Run Code Online (Sandbox Code Playgroud)