Is there any way to define database identifiers returned by a query as constants in order to reuse them in another query? Like table name or column name etc.
Asked
Active
Viewed 63 times
0
-
Well, you can store the returned value in variables. If your query returns multiple database / table names, then you probably need a cursos to loop through the resultset and process database / table names one by one. However, if you would like to use the returned names in another query, then you have to concatenate the query statement as text and used prepared statement to execute it. See the following answer for an example: https://stackoverflow.com/a/37314063/5389997 – Shadow Jul 06 '20 at 22:44
-
Are you using a scripting language, python, php, java, etc? – Azeame Jul 06 '20 at 23:59
-
@Shadow Thank you, but I am new to SQL, can you explain, what does this line means, `SET @queryText = CONCAT('UPDATE ', TableName, ' SET ', UpdateColumn, '=', QUOTE(UpdateValue), ' WHERE ', WhereColumn, '=', QUOTE(WhereValue));` Is it not going to give error code:1248 – KnowledgeGainer Jul 07 '20 at 07:02
-
@Azeame Currently i am not using any scripting language, just running queries and practising. I am using MySQL workbench. – KnowledgeGainer Jul 07 '20 at 07:04
-
That line creates the dynamic sql statement as a string. It cannot result in error 1248, since there is no derived table used in the statement. – Shadow Jul 07 '20 at 08:01
-
@Shadow What if I use a table-name generated using query `select * from table-name` and then do the following that you have mentioned above. Will it get executed or any error will be there ? – KnowledgeGainer Jul 07 '20 at 08:33
-
This is a too hypothetical question. Try it out and you will see. If you do get an error and you cannot sort it out, you can always come back to SO and ask a new question about it. Not in this question, but in a new one. – Shadow Jul 07 '20 at 09:23