troubleshooting Question

Help with T-SQL

Avatar of gtrapp
gtrapp asked on
Microsoft SQL ServerMicrosoft SQL Server 2005
3 Comments1 Solution224 ViewsLast Modified:
I am trying to concatenate strings in my WHERE clause where the string is based upon some condition (IF statements). However, I get a "Incorrect syntax near the keyword 'ORDER'." error.
Any suggestions on how to correct this?

set @strWhere = '(@BondNum is null or BondNum = @BondNum) AND
      (@BondLocation is null or BondLocation = @BondLocation) AND       
      (@BondDesc is null or BondDesc = @BondDesc) AND
      (@VolNum is null or HistoryVolume = @VolNum)  AND
      (@Active = 3 or Active = @Active) AND
      (@NeedsAttention = 3 or NeedsAttention = @NeedsAttention) AND
      (@BondReduced  = 3 or BondReduced = @BondReduced) AND
      (@Development = 0 or DevelopmentID = @Development)'

if @BondDate <> null
      set @strWhere = @strWhere + 'AND (convert(smalldatetime,OrigBondDate) = @BondDate)';

if @BondTicklerDate <> null
      set @strWhere = @strWhere + 'AND (convert(smalldatetime,TickerDate) = @BondTicklerDate)';

SELECT ' <All>' As BondNum
SELECT BOND.BondNum As BondNum
       dbo.luBondType ON dbo.BOND.BondTypeID = dbo.luBondType.BondTypeID LEFT OUTER JOIN
       dbo.luBondStatus ON dbo.BOND.BondStatusID = dbo.luBondStatus.BondStatusID
WHERE @strWhere
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros