简体   繁体   English

用户定义的表类型插入导致转换错误

[英]User-Defined Table Type insertion causing conversion error

Not a duplicate of User-Defined Table Type insertion sometimes causing conversion error不是用户定义表类型的重复插入有时会导致转换错误

I have a user defined table type:我有一个用户定义的表类型:

CREATE TYPE [dbo].[udtImport] AS TABLE(
    Name    varchar(256)    null
    Code    varchar(32) null
    StartDate   varchar(256)    null
    EndDate varchar(256)    null
    DateCreated datetime    null

The DateCreated field is populated in my DB layer using DateTime.Now(), while all other fields are from an imported table. DateCreated 字段使用DateTime.Now(),填充在我的数据库层中DateTime.Now(),而所有其他字段都来自导入的表。

When I import a file with the date fields populated I get a SQL error:当我导入一个填充了日期字段的文件时,我收到一个 SQL 错误:

Conversion failed when converting date and/or time from character string.从字符串转换日期和/或时间时转换失败。

Intercepting the generated code using SQL Profiler shows this:使用 SQL Profiler 拦截生成的代码显示:

DECLARE @p1 dbo.udtImport;
INSERT INTO @p1
VALUES
  ( N'Kit A1', 
    N'A002',  
    '2016-04-02 00:00:00.000',
    '2016-10-10 00:01:00.000', 
    '2018-10-22 16:08:28.6823468' );

exec impSaveImport @ImportList=@p1

impSaveImport is a stored procedure that has just one parameter: the table type var and does a straight insert to table [Import]. impSaveImport 是一个只有一个参数的存储过程:表类型 var 并直接插入表 [Import]。 No logic, no triggers, no reference to other tables.没有逻辑,没有触发器,没有对其他表的引用。

Executing this code in SSMS shows the same error as expected.在 SSMS 中执行此代码显示与预期相同的错误。
Trimming the last 4 digits off the last DateTime field causes the insert query to succeed.修剪最后一个 DateTime 字段的最后 4 位数字会导致插入查询成功。

So far so good.到现在为止还挺好。

When I import a file with the StartDate and EndDate fields empty, I get no error, and data is successfully inserted into the Import table.当我导入一个 StartDate 和 EndDate 字段为空的文件时,我没有收到任何错误,并且数据已成功插入到导入表中。

When I intercept the successful insert using profiler I get this:当我使用探查器拦截成功插入时,我得到了这个:

DECLARE @p1 dbo.udtImport;
   INSERT INTO @p1
        VALUES
          ( N'Kit A1', 
            N'A002',  
            null,
            null, 
            '2018-10-22 16:34:11.5243245' );

        exec impSaveImport @ImportList=@p1

Keep in mind this query SUCCESSFULLY insert one row into the table Import.请记住此查询成功地将一行插入到表导入中。

When I run this latest query in SSMS I get the same conversion error as before,当我在 SSMS 中运行这个最新查询时,我得到与以前相同的转换错误,

but it ran without error from within my MVC app!但它在我的 MVC 应用程序中运行没有错误!

This last part has me stumped.最后一部分让我难住了。

How can it be?怎么会这样?

Project is using MVC with SQL2016.项目使用 MVC 和 SQL2016。

You could use DATETIME2 :您可以使用DATETIME2

CREATE TYPE [dbo].[udtImport] AS TABLE(
    Name    varchar(256)    null,
    Code    varchar(32) null,
    StartDate   varchar(256)    null,  -- should be datetime2 format
    EndDate varchar(256)    null,      -- should be datetime2 format
    DateCreated datetime2    null);

DECLARE @p1 dbo.udtImport;
INSERT INTO @p1(Name, Code, StartDate, EndDate, DateCreated)
VALUES
  ( N'Kit A1', 
    N'A002',  
    '2016-04-02 00:00:00.000',
    '2016-10-10 00:01:00.000', 
    '2018-10-22 16:08:28.6823468' );

db<>fiddle demo db<>小提琴演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM