我正在尝试对某些表进行测试,但似乎一直遇到错误。
我正在尝试运行以下存储过程:
INSERT INTO [Customer].[TableA_test] ([CustomerID], [VisitID], [TransactionId], [ArrivalDT], [DataA], [DataB], [DataC], [SnapDate])
SELECT
b.CustomerID,
b.VisitID,
b.TransactionID,
b.ArrivalDT,
b.DataA,
b.DataB,
b.DataC,
SnapDate = GETDATE()
FROM
[Customer].[TableA_test] a
LEFT JOIN
[Customer].[TableB] b ON a.VisitID = b.VisitID
AND a.TransactionId = b.TransactionID
WHERE
a.TransactionID IS NULL
该过程旨在从查询中获取结果并将其插入到TableA中。表A具有与表B完全相同的列和数据类型。当我尝试执行存储过程(上面提供的代码)时,出现以下错误:
消息241,级别16,状态1,过程InsertTo_TableA_test,第27行
从字符串转换日期和/或时间时转换失败。
我所有的日期列都设置为datetime
数据类型:ArrivalDT
和SnapDate
。 TableA_testing和TableB具有完全相同的列名和数据类型。
有什么我想念的吗?
这是[客户]的表信息。[TableA_test]:
CREATE TABLE [Customer].[TableA_test]
(
[TransactionID] [VARCHAR](254) NULL,
[VisitID] [VARCHAR](254) NULL,
[ArrivalDT] [DATETIME] NULL,
[CustomerID] [VARCHAR](254) NULL,
[DataA] [VARCHAR](50) NULL,
[DataB] [VARCHAR](50) NULL,
[DataC] [VARCHAR](50) NULL,
[SnapDate] [DATETIME] NULL
)
对于TableB:
CREATE TABLE [Customer].[TableB]
(
[TransactionID] [VARCHAR](254) NULL,
[VisitID] [VARCHAR](254) NULL,
[ArrivalDT] [DATETIME] NULL,
[CustomerID] [VARCHAR](254) NULL,
[DataA] [VARCHAR](50) NULL,
[DataB] [VARCHAR](50) NULL,
[DataC] [VARCHAR](50) NULL,
[SnapDate] [DATETIME] NULL
)
我的存储过程的第27行是:
INSERT INTO [Customer].[JTM_NY_SPARCS_VitalSigns_test] ([CustomerID],[VisitID], [TransactionID], [ArrivalDT], [DataA], [DataB], [DataC], [SnapDate])
这是存储过程的完整读出:
USE [Database_3]
GO
/****** Object: StoredProcedure [Customer].[InsertTo_TableA_test] Script Date: 4/12/2019 11:49:17 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [Customer].[InsertTo_TableA_test]
/*
CREATED: 04/11/2019
*/
AS
BEGIN
INSERT INTO [Customer].[TableA_test] ([CustomerID], [VisitID], [TransactionId], [ArrivalDT], [DataA], [DataB], [DataC], [SnapDate])
SELECT
b.CustomerID,
b.VisitID,
b.TransactionID,
b.ArrivalDT,
b.DataA,
b.DataB,
b.DataC,
SnapDate = GETDATE()
FROM
[Customer].[TableA_test] a
LEFT JOIN
[Customer].[TableB] b ON a.VisitID = b.VisitID
AND a.TransactionId = b.TransactionID
WHERE
a.TransactionID IS NULL
END
GO
从存储过程中运行查询时的结果行示例:
100010 [VisitID]
20000281542 [TransactionID]
2014-07-09 15:44:42.000 [ArrivalDT]
0032011 [CustomerID]
147 [DataA]
71 [DataB]
69 [DataC]
2019-04-12 11:54:23.753 [SnapDate]
答案 0 :(得分:0)
根据我在您的评论中看到的内容,您说过您正在尝试从TableA_test插入到TableB。如果是这种情况,那么您的查询应该是:
INSERT INTO [Customer].[TableB] ([CustomerID],[VisitID],[TransactionId],[ArrivalDT],[DataA],[DataB],[DataC],[SnapDate])
SELECT
a.CustomerID,
a.VisitID,
a.TranactionID,
a.ArrivalDT,
a.DataA,
a.DataB,
a.DataC,
SnapDate = GETDATE()
FROM [Customer].[TableA_test] a
LEFT JOIN [Customer].[TableB] b
ON a.VisitID = b.VisitID
AND a.TransactionId = b.TransactionID
WHERE b.TransactionID IS NULL