SQL Query WHERE clause is a parameter

Could you please help me to solve the problem I am facing with my request. I am very new to database programming and I search the internet thoroughly for help, but no one is solving my problem. I'm bored and wondering if this is possible. I want to execute a SELECT query where the column to search needs to be provided at runtime. Like this:

SELECT *
FROM myTable
WHERE @columnToSeach = @_ColumnName

      

The advice is really appreciated as my brain practically bleeds on it.

+3


source to share


1 answer


Very quick solution:



execute ('SELECT * FROM myTable WHERE ' + @columnToSeach + '=' + @_ColumnName)

      

+1


source







All Articles