1

Is there way to create variable from select ?

set @MY_ID = 'SELECT ID FROM TMS.MY_TABLE WHERE MY_TABLE.UNIQ_COLUMN= 2134';

Code above works but it just saves string:

SELECT @MY_ID; returns

SELECT ID FROM TMS.MY_TABLE WHERE MY_TABLE.UNIQ_COLUMN= 2134'

I have to know this id to use it as foreign key to insert into another tables

2 Answers 2

2

Are you trying to do this?

SELECT @MY_ID := ID
FROM TMS.MY_TABLE
WHERE MY_TABLE.UNIQ_COLUMN = 2134;
Sign up to request clarification or add additional context in comments.

3 Comments

@gstackoverflow Be sure the result can only have one record. So most likely you need to add ORDER BY column LIMIT 1 if MY_TABLE.UNIQ_COLUMN not has a unique or primary key
@Raymond Nijland, thanks for reminder but as you can see I provided that I use column with name UNIQ_COLUMN
@gstackoverflow Ok how should i know maybe UNIQ meant something unix quality or unix quantity or something else like that
1

The other variation would be

SELECT 
 ID
FROM 
 TMS.MY_TABLE
INTO @MY_ID
WHERE
 MY_TABLE.UNIQ_COLUMN = 2134

Be sure the result can only have one record. So most likely you need to add ORDER BY column LIMIT 1 if MY_TABLE.UNIQ_COLUMN not has a unique or primary key

Comments

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.