NVARCHAR转换为超出范围的日期时间值

时间:2016-06-02 16:24:12

标签: sql-server tsql

我已经编写了一个程序,可以从我们的服务提供商处获取驾驶执照上的数据。

这被抽出到Windows窗体/ GUI以便顾问阅读。

我有2个SQL Server,一个位于一个办公室SQL Server 2012,一个位于另一个办公室2008.虽然我同意这不是理想的办公室独立行事。该软件返回数据并将其输入SQL表以供审计。

为此,它将VB.NET中的DataTable转换为SQL,并使用存储过程和“用户定义的表类型”将其转换为SQL。

我位于拥有SQL 2008 Server的办公室,该软件运行得非常好,如果我将软件部署到其他办公室DOMAIN(可信)输入它INTO我们的SQL它可以正常工作。

但是,如果我尝试将数据输入到他们的SQL Server中,我会得到:

The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value. The data for table-valued parameter "@Endorsements" doesn't conform to the table type of the parameter. The statement has been terminated.

我已经更改了SQL Server的语言设置,并将它们设置为“英式英语”,区域设置也相同。

以下是用户定义的数据类型和存储过程。有人可以向我解释为什么会发生这种情况,BOTH SP和UDT在两台服务器上完全相同。

我还添加了我试图将数据插入的表的Create Table代码。

SP

CREATE PROCEDURE [dbo].[usp_MyL_InsertEndorsements]
-- Add the parameters for the stored procedure here
@Endorsements [dbo].[MyL_EndorsementDetails] READONLY
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
INSERT INTO dbo.MyL_Audit_Endorsements
        ( Code ,
          [Offence Date] ,
          [Conviction Date] ,
          Fine ,
          Points ,
          Disqualification ,
          [Disqualification Period] ,
          [Disqual Start Date] ,
          [Disqual End Date] ,
          [Custodial Period] ,
          [Rehab Spent Date] ,
          DLN ,
          RequestDT
        )
SELECT Code ,
       [Offence Date] ,
       [Conviction Date] ,
       Fine ,
       Points ,
       Disqualification ,
       [Disqualification Period] ,
       [Disqual Start Date] ,
       [Disqual End Date] ,
       [Custodial Period] ,
       [Rehab Spent Date] ,
       DLN ,
       DATEADD(ms, -(DATEPART(ms, RequestDT)), RequestDT) FROM @Endorsements
END

UDT

CREATE TYPE [dbo].[MyL_EndorsementDetails] AS TABLE(
[Code] [VARCHAR](5) NULL,
[Offence Date] [DATETIME] NULL,
[Conviction Date] [DATETIME] NULL,
[Fine] [VARCHAR](10) NULL,
[Points] [VARCHAR](10) NULL,
[Disqualification] [VARCHAR](10) NULL,
[Disqualification Period] [VARCHAR](20) NULL,
[Disqual Start Date] [VARCHAR](30) NULL,
[Disqual End Date] [VARCHAR](30) NULL,
[Custodial Period] [VARCHAR](30) NULL,
[Rehab Spent Date] [VARCHAR](30) NULL,
[DLN] [VARCHAR](30) NULL,
[RequestDT] [DATETIME] NULL
)

创建表格

CREATE TABLE [dbo].[MyL_Audit_Endorsements](
[Code] [VARCHAR](5) NULL,
[Offence Date] [DATETIME] NULL,
[Conviction Date] [DATETIME] NULL,
[Fine] [VARCHAR](10) NULL,
[Points] [VARCHAR](10) NULL,
[Disqualification] [VARCHAR](10) NULL,
[Disqualification Period] [VARCHAR](20) NULL,
[Disqual Start Date] [VARCHAR](30) NULL,
[Disqual End Date] [VARCHAR](30) NULL,
[Custodial Period] [VARCHAR](30) NULL,
[Rehab Spent Date] [VARCHAR](30) NULL,
[DLN] [VARCHAR](30) NULL,
[RequestDT] [DATETIME] NULL
) ON [PRIMARY]

在.NET中执行的代码

Public Shared Sub DataTableToSQL(ByVal DataTable As DataTable, ByVal SPName As String)

    Using cn As New SqlConnection(ObtainSQLConn(Audit.Domain))
        'SqlConnection.ClearAllPools()

        cn.Open()

        Using cmd As New SqlCommand(SPName, cn)

            cmd.CommandType = CommandType.StoredProcedure

            If SPName = "usp_MyL_InsertEndorsements" Then
                cmd.Parameters.AddWithValue("@Endorsements", DataTable)
            Else
                cmd.Parameters.AddWithValue("@LicenceDets", DataTable)
            End If

            cmd.ExecuteNonQuery()

        End Using
    End Using

End Sub

VB.NET创建了DataTable

        Results.EndorsementsTbl.Columns.Add(New DataColumn("Code", GetType(String)))
        Results.EndorsementsTbl.Columns.Add(New DataColumn("Offence Date", GetType(String)))
        Results.EndorsementsTbl.Columns.Add(New DataColumn("Conviction Date", GetType(String)))
        Results.EndorsementsTbl.Columns.Add(New DataColumn("Fine", GetType(String)))
        Results.EndorsementsTbl.Columns.Add(New DataColumn("Points", GetType(String)))
        Results.EndorsementsTbl.Columns.Add(New DataColumn("Disqualification?", GetType(String)))
        Results.EndorsementsTbl.Columns.Add(New DataColumn("Disqualification Period", GetType(String)))
        Results.EndorsementsTbl.Columns.Add(New DataColumn("Disqual Start Date", GetType(String)))
        Results.EndorsementsTbl.Columns.Add(New DataColumn("Disqual End Date", GetType(String)))
        Results.EndorsementsTbl.Columns.Add(New DataColumn("Custodial Period", GetType(String)))
        Results.EndorsementsTbl.Columns.Add(New DataColumn("Rehab Spent Date", GetType(String)))
        Results.EndorsementsTbl.Columns.Add(New DataColumn("DLN", GetType(String)))
        Results.EndorsementsTbl.Columns.Add(New DataColumn("RequestDT", GetType(Date)))

非常感谢。

0 个答案:

没有答案