Beck
Beck

Reputation: 27

Inserting row into SQL table with some predefined values, and some from another table as well as join string.

Hi I am trying to run this query below:

INSERT INTO Database2.Table2 ('1', getdate(), getdate(), '',ID,'','0','0','0','',ID,'','',getdate(),getdate(),'','0','"stackoverflow.com/"'+'ID','0','product','','0') 
SELECT ID
FROM Database1.Table1;

I am inserting a new row with constant data mixed with data from another table, this case Database1.Table1 "ID". I constantly keep getting a select statement error when I try to run this code. Is there something I am overseeing or is this statement all wrong? Thank You

Upvotes: 0

Views: 21

Answers (1)

Barmar
Barmar

Reputation: 781088

Put the constants into the SELECT list.

INSERT INTO Database2.Table2 
SELECT '1', getdate(), getdate(), '',ID,'','0','0','0','',ID,'','',getdate(),getdate(),
       '','0',CONCAT('"stackoverflow.com/"','ID'),'0','product','','0', ID
FROM Database1.Table1;

BTW, I strongly recommend you get out of the habit of using INSERT without listing the column names before the values. Depending on the specific order of columns in the table definition is very error-prone.

Also, MySQL doesn't use + for string concatenation by default, it uses the CONCAT() function.

Upvotes: 1

Related Questions