Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building SQL Within a Stored Procedure 1

Status
Not open for further replies.

BoulderBum

Programmer
Jul 11, 2002
2,179
US
Is it possible to build a SQL string within SQL Server, then execute it only once at the end of proc?

E.G. Can I do something like:

SqlString = 'SELECT * FROM table'
IF NOT @filter IS NULL
SqlString = SqlString + ' WHERE filter = ' + @filter

//Run query according to SqlString
 
declare @sqlstring varchar(400)

SET @SqlString = 'SELECT * FROM table'

IF NOT @filter IS NULL

SET @SqlString = @SqlString + ' WHERE filter = ' + @filter

exec (@sql_string)

If @filter is a character string, you will need to enclose it in '' like so ...

SET @SqlString = @SqlString + ' WHERE filter = ' + '''' + @filter + ''''





Thanks

J. Kusch
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top