General discussion

Locked

Execute an SQL string in SQL Stored Proc

By frankus ·
How to execute a concatenated SQL string within a SQL Server Stored Procedure.

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Execute an SQL string in SQL Stored Proc

by andrew In reply to Execute an SQL string in ...

No problem.
Use Exec (execute) to run the built up SQL string:

Create Proc pListNames (@FieldName varchar(128),@FieldValue varchar(128))as
exec ('select * from authors where ' + @FieldName + ' = ' + "'" + @FieldValue + "'")

exec pListNames 'au_lname','white'

(If you're passing in string values, remember to wrap them in single quotes once they're in the proc)

The downside to this is that SQL Server can't compile the proc, so performance is poorer.

Collapse -

Execute an SQL string in SQL Stored Proc

by frankus In reply to Execute an SQL string in ...
Collapse -

Execute an SQL string in SQL Stored Proc

by frankus In reply to Execute an SQL string in ...

This question was closed by the author

Back to Web Development Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums