具有不同 table/col 结构的 INSERT SELECT
INSERT SELECT with differed table/col stucture
我正在尝试创建一个 INSERT SELECT 语句,该语句将数据从 Imported_table
插入并转换为 Destination_table
。
Imported_table
+------------------+-----------------------+
| Id (varchar(10)) | genre (varchar(4000)) |
+------------------+-----------------------+
| 6 | Comedy |
+------------------+-----------------------+
| 5 | Comedy |
+------------------+-----------------------+
| 1 | Action |
+------------------+-----------------------+
Destination_table(应该是什么样子)
+-----------------------------+----------------------------+
| genre_name (PK,varchar(50)) | description (varchar(255)) |
+-----------------------------+----------------------------+
| Comedy | Description of Comedy |
+-----------------------------+----------------------------+
| Action | Description of Action |
+-----------------------------+----------------------------+
Imported_table.Id
根本没有使用,但仍然在这个(旧)table
Destination_table.genre_name
是主键,应该是唯一的 (distinct)
Destination_table.description
是用 CONCAT('Description of ',genre)
编译的
我最好的尝试
INSERT INTO testdb.dbo.Destination_table (genre_name, description)
SELECT DISTINCT Genre,
LEFT(Genre,50) AS genre_name,
CAST(CONCAT('Description of ',Genre) AS varchar(255)) AS description
FROM MYIMDB.dbo.Imported_table
给出错误:The select list for the INSERT statement contains more items than the insert list. The number of SELECT values must match the number of INSERT columns.
提前致谢。
查询中最大的错误是您试图将 3 列插入到只有两列的目标 table 中。话虽这么说,我只会对两个插入值使用 LEFT
,并尽可能多地使用 space,因为新的 table 可以容纳:
INSERT INTO testdb.dbo.Destination_table (genre_name, description)
SELECT DISTINCT
LEFT(Genre, 50),
'Description of ' + LEFT(Genre, 240) -- 240 + 15 = 255
FROM MYIMDB.dbo.Imported_table;
附带说明一下,原来的 genre
字段有 4000 个字符宽,而您的新 table 结构有丢失大量信息的风险。不清楚您是否关心这一点,但值得指出。
这意味着您的 SELECT
(类型,genre_name,描述)和 INSERT
(genre_name,描述)列表不匹配。您需要 SELECT
与您在 INSERT
.
中指定的相同数量的字段
试试这个:
INSERT INTO testdb.dbo.Destination_table (genre_name, description)
SELECT DISTINCT Genre,
CAST(CONCAT('Description of ',Genre) AS varchar(255)) AS description
FROM MYIMDB.dbo.Imported_table
您的 SELECT 中有 3 列,尝试:
INSERT INTO testdb.dbo.Destination_table (genre_name, description)
SELECT DISTINCT LEFT(Genre,50) AS genre_name,
CAST(CONCAT('Description of ',Genre) AS varchar(255)) AS description
FROM MYIMDB.dbo.Imported_table
我正在尝试创建一个 INSERT SELECT 语句,该语句将数据从 Imported_table
插入并转换为 Destination_table
。
Imported_table
+------------------+-----------------------+
| Id (varchar(10)) | genre (varchar(4000)) |
+------------------+-----------------------+
| 6 | Comedy |
+------------------+-----------------------+
| 5 | Comedy |
+------------------+-----------------------+
| 1 | Action |
+------------------+-----------------------+
Destination_table(应该是什么样子)
+-----------------------------+----------------------------+
| genre_name (PK,varchar(50)) | description (varchar(255)) |
+-----------------------------+----------------------------+
| Comedy | Description of Comedy |
+-----------------------------+----------------------------+
| Action | Description of Action |
+-----------------------------+----------------------------+
Imported_table.Id
根本没有使用,但仍然在这个(旧)tableDestination_table.genre_name
是主键,应该是唯一的(distinct)
Destination_table.description
是用CONCAT('Description of ',genre)
编译的
我最好的尝试
INSERT INTO testdb.dbo.Destination_table (genre_name, description)
SELECT DISTINCT Genre,
LEFT(Genre,50) AS genre_name,
CAST(CONCAT('Description of ',Genre) AS varchar(255)) AS description
FROM MYIMDB.dbo.Imported_table
给出错误:The select list for the INSERT statement contains more items than the insert list. The number of SELECT values must match the number of INSERT columns.
提前致谢。
查询中最大的错误是您试图将 3 列插入到只有两列的目标 table 中。话虽这么说,我只会对两个插入值使用 LEFT
,并尽可能多地使用 space,因为新的 table 可以容纳:
INSERT INTO testdb.dbo.Destination_table (genre_name, description)
SELECT DISTINCT
LEFT(Genre, 50),
'Description of ' + LEFT(Genre, 240) -- 240 + 15 = 255
FROM MYIMDB.dbo.Imported_table;
附带说明一下,原来的 genre
字段有 4000 个字符宽,而您的新 table 结构有丢失大量信息的风险。不清楚您是否关心这一点,但值得指出。
这意味着您的 SELECT
(类型,genre_name,描述)和 INSERT
(genre_name,描述)列表不匹配。您需要 SELECT
与您在 INSERT
.
试试这个:
INSERT INTO testdb.dbo.Destination_table (genre_name, description)
SELECT DISTINCT Genre,
CAST(CONCAT('Description of ',Genre) AS varchar(255)) AS description
FROM MYIMDB.dbo.Imported_table
您的 SELECT 中有 3 列,尝试:
INSERT INTO testdb.dbo.Destination_table (genre_name, description)
SELECT DISTINCT LEFT(Genre,50) AS genre_name,
CAST(CONCAT('Description of ',Genre) AS varchar(255)) AS description
FROM MYIMDB.dbo.Imported_table