Can a stored procedure be written with a column name?

For Table1 with ColA, ColB, ColC, I want a query like:

EXEC sp_Mine 'ColA', '100%'

I want the results to be like:

SELECT ColA, ColB, ColC
FROM Table1
WHERE ColA Like '100%'

I tried setting up this stored procedure, but it came out with the WHERE clause looking like:

WHERE 'ColA' Like '100%'

That is, it is trying to compare the two strings I passed in.

Recommended Answers

All 2 Replies

You need to use the logic in the SP to concatenate a dynamic SQL query, then execute the query.

Right now you know your parameter is going in right, you just now have to concatenate properly and execute a dynamic SQL query :)

Hello,

You need to do something like this:

DECLARE  @sql varchar(255)

@sql = 'SELECT ColA, ColB, ColC 
            FROM Table1  WHERE ' + parameterColumnName + ' Like ' + ''' +
              parameterPercent + '''
Exec @sql

Hope this help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.