I have a stored procedure where the column name is coming in the parameter as string – varchar
How to convert this to a column name to query in the select/insert/update as column name should be
colnName
Thanks
How to convert the incoming string to column name in stored procedure
2
Answers
But you can have multiple if conditions to solve the problem.
It is possible as well
Solution 1 - SQL query on FLY - but it can only be executed Solution 2 - Dynamic SQL - where we cannot add column name as it would be a string.
We need to use CASE and WHEN and THEN and create the query (OR) IF THEN
IF(columnName='product') THEN select
product
from purchase IF(columnName='test') THEN selecttest
from purchaseIn this way we can solve. so both the options are possible.
As far as a I know , column names can not be defined using a variable when executing a query. They have to be hardcoded. Therefore, we should focus on hardcoding column names when writing the statement, rather than determine the column name on the fly when executing the query. MySQL
PREPARE
statement is a viable choice for this. Here is the test sample if you are intrigued.