0

I am trying to create a INSERT SELECT statement which inserts and converts data from Imported_table to Destination_table.

Imported_table

+------------------+-----------------------+
| Id (varchar(10)) | genre (varchar(4000)) |
+------------------+-----------------------+
| 6                | Comedy                |
+------------------+-----------------------+
| 5                | Comedy                |
+------------------+-----------------------+
| 1                | Action                |
+------------------+-----------------------+

Destination_table (How it should be looking)

+-----------------------------+----------------------------+
| genre_name (PK,varchar(50)) | description (varchar(255)) |
+-----------------------------+----------------------------+
| Comedy                      | Description of Comedy      |
+-----------------------------+----------------------------+
| Action                      | Description of Action      |
+-----------------------------+----------------------------+
  • Imported_table.Id isn't used at all but is still in this (old) table
  • Destination_table.genre_name is a primairy key and should be unique (distinct)
  • Destination_table.description is compiled with CONCAT('Description of ',genre)

My best try

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 

Gives the error: 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.

Thanks in advance.

Thomas Beumer
  • 89
  • 1
  • 14

3 Answers3

1

The largest error in your query is that you are trying to insert 3 columns into a destination table having only two columns. That being said, I would just use LEFT for both inserted values and take as much space as the new table can hold:

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;

As a side note, the original genre field is 4000 characters wide, and your new table structure runs the risk of throwing away a lot of information. It is not clear whether you are concerned with this, but it is worth pointing out.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

This means your SELECT (genre, genre_name,description) and INSERT (genre_name, description) lists don't match. You need to SELECT the same number of fields as you are specifying in your INSERT.

Try this:

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 
ravioli
  • 3,749
  • 3
  • 14
  • 28
1

You have 3 columns in your SELECT, try:

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 
Steve Ford
  • 7,433
  • 19
  • 40