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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

@variable Concatenation problem

Status
Not open for further replies.

Azita79

Programmer
Jan 30, 2003
41
US
Hi All,

in my sproc I have a local variable @strSQL. I keep concatenating to this variable in my sproc and I wana use it in my "WHERE" clause at the end.

IF ...
SET @strSQL = @strSQL + ....
IF
.
.
.

@strSQL contains something like this : 'TBL.ID = 345'
BEGIN
SELECT ....
WHERE ---->> here I wana use what's in @strSQL


any ideas?

Thanks
Azita
 
You can't use a variable for the WHERE clause. You must name the columns. You'll need to create a SQL statement and EXECUTE it.

Declare @strSQLStmnt varchar(4000)

IF ...
SET @strSQL = @strSQL + ....
IF
.
.
.

@strSQL contains something like this : 'TBL.ID = 345'
BEGIN

SET @strSQLStmnt = 'SELECT .... WHERE ' + @strSQL

Exec(@strSQLStmnt) Terry L. Broadbent - DBA
SQL Server Page:
If you want to get the best answer for your question read faq183-874.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top