0

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

1
  • Do you really need both single and double quotes around stackoverflow.com/? And why are you quoting ID after that? That's the same as just writing '"stackoverflow.com/"ID'. Is that ID supposed to be unquoted, so it gets it from Table1? Commented Oct 11, 2018 at 2:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

But MySQL can use the ANSI concatenation operator ||, if a certain flag is set +1.
@TimBiegeleisen I added "by default", since that flag is probably rarely set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.