SQL将数据从一个表插入另一个表/数据类型错误

时间:2023-01-15 03:45:25

I am trying to insert data from one SQL table to another. The problem is that the columns have different data types. And when I use Cast() I still get the following error message:

我试图将数据从一个SQL表插入另一个SQL表。问题是列具有不同的数据类型。当我使用Cast()时,我仍然收到以下错误消息:

[Msg 8114, Level 16, State 5, Line 3 Error converting data type varchar to bigint.]

[Msg 8114,Level 16,State 5,Line 3将数据类型varchar转换为bigint时出错。]

Table1 columns have data types of bigint or int while Table2 columns are varchar. Any suggestions?

Table1列的数据类型为bigint或int,而Table2列为varchar。有什么建议?

Here is my query:

这是我的查询:

INSERT INTO Table1 
        ([RowId],
         [Scenario],
         [Entity],
         [Project],
         [TimePeriod])

SELECT Cast([ProjectMgrID] as bigint), Cast('ACT' as bigint), 
Cast('APP' as bigint), Cast([Project] as bigint), Cast('201801' as int)
FROM Table2

1 个解决方案

#1


0  

use this

INSERT INTO Table1 
        ([RowId],
         [Scenario],
         [Entity],
         [Project],
         [TimePeriod])

SELECT Cast([ProjectMgrID] as bigint), Cast([ACT] as bigint), 
Cast([APP] as bigint), Cast([Project] as bigint), Cast('201801' as int)
FROM Table2

you are casting string to bigint here Cast('ACT' as bigint), Cast('APP' as bigint) you should give column names in []

你在这里把字符串转换为bigint Cast('ACT'作为bigint),Cast('APP'作为bigint)你应该在[]中给出列名

#1


0  

use this

INSERT INTO Table1 
        ([RowId],
         [Scenario],
         [Entity],
         [Project],
         [TimePeriod])

SELECT Cast([ProjectMgrID] as bigint), Cast([ACT] as bigint), 
Cast([APP] as bigint), Cast([Project] as bigint), Cast('201801' as int)
FROM Table2

you are casting string to bigint here Cast('ACT' as bigint), Cast('APP' as bigint) you should give column names in []

你在这里把字符串转换为bigint Cast('ACT'作为bigint),Cast('APP'作为bigint)你应该在[]中给出列名