Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 IF Syntax?

Author  Topic 

Mnemonic
Yak Posting Veteran

58 Posts

Posted - 2007-12-02 : 15:40:53
In my ASP .net page, there will always be a querystring equaling 0 or 1... how do i make my query select based on that? Here's what i got.. but it's probably wrong

(
@WindowAir int,
@CentalAC int
)
AS

SET NOCOUNT ON

DECLARE @SQL VARCHAR(8000)

SET @SQL = '
SELECT

r.REListingID,
r.REListingDate,
r.RESubject,
a.RentCharge,
b.thumb

FROM db_REListings as r
LEFT JOIN db_RentalAmenities AS a ON a.REListingID = r.REListingID INNER JOIN (
Select Row_Number() over (Partition by aa.ListingID order by aa.thumb) as RowID,aa.*
from db_REPics aa
) b ON a.REListingID = b.ListingID
WHERE 1 = 1 and b.RowID = 1
'

IF @WindowAir = '1'
SET @SQL = @SQL + ' AND a.WindowAir = ' + CONVERT(VARCHAR(20), @WindowAir)
IF @CentralAC = '1'
SET @SQL = @SQL + ' AND a.CentralAir = ' + CONVERT(VARCHAR(20), @CentralAC)

EXEC @SQL

Any ideas?

When i run that query i get

Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '@WindowAir'.


I dont really know the syntax..

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-12-02 : 15:49:37
quote:
Originally posted by Mnemonic

In my ASP .net page, there will always be a querystring equaling 0 or 1... how do i make my query select based on that? Here's what i got.. but it's probably wrong

(
@WindowAir int,
@CentralAC int
)
AS

SET NOCOUNT ON

DECLARE @SQL VARCHAR(8000)

SET @SQL = '
SELECT

r.REListingID,
r.REListingDate,
r.RESubject,
a.RentCharge,
b.thumb

FROM db_REListings as r
LEFT JOIN db_RentalAmenities AS a ON a.REListingID = r.REListingID INNER JOIN (
Select Row_Number() over (Partition by aa.ListingID order by aa.thumb) as RowID,aa.*
from db_REPics aa
) b ON a.REListingID = b.ListingID
WHERE 1 = 1 and b.RowID = 1
'

IF @WindowAir = '1'
SET @SQL = @SQL + ' AND a.WindowAir = ' + CONVERT(VARCHAR(20), @WindowAir)
IF @CentralAC = '1'
SET @SQL = @SQL + ' AND a.CentralAir = ' + CONVERT(VARCHAR(20), @CentralAC)

EXEC @SQL

Any ideas?

When i run that query i get

Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '@WindowAir'.


I dont really know the syntax..




You have a typo error with @Centralair in your code..



Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

Mnemonic
Yak Posting Veteran

58 Posts

Posted - 2007-12-02 : 15:51:46
Yea, thats a typo from typing it on here, not in the acctual query... so thats not it...
Go to Top of Page

Mnemonic
Yak Posting Veteran

58 Posts

Posted - 2007-12-02 : 15:52:53
Yea, thats a typo from typing it on here, not in the acctual query... so thats not it...
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-12-02 : 16:18:06
Do not use dynamic sql for this! That is over complicating something very simple.

All you need is regular boolean logic, which is essential to understand and use when writing SQL.

SELECT
r.REListingID,
r.REListingDate,
r.RESubject,
a.RentCharge,
b.thumb
FROM db_REListings as r
LEFT JOIN db_RentalAmenities AS a ON a.REListingID = r.REListingID INNER JOIN (
Select Row_Number() over (Partition by aa.ListingID order by aa.thumb) as RowID,aa.*
from db_REPics aa
) b ON a.REListingID = b.ListingID
WHERE b.RowID = 1 and (@WindowAir != 1 or WIndowAir = 1) and (@CentralAir != 1 or CentralAir = 1)

more here: http://weblogs.sqlteam.com/jeffs/archive/2003/11/14/513.aspx

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

Mnemonic
Yak Posting Veteran

58 Posts

Posted - 2007-12-02 : 16:40:38
Jeff -

I used dynamic sql for this because there are like 30 other options beside windowair and centralair...
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2007-12-03 : 06:31:29
Try:

IF @WindowAir = 1
SET @SQL = @SQL + ' AND a.WindowAir = ''' + CONVERT(VARCHAR(20), @WindowAir) + ''
IF @CentralAC = 1
SET @SQL = @SQL + ' AND a.CentralAir = ''' + CONVERT(VARCHAR(20), @CentralAC) + ''


You are trying to match a varchar with no quotes around it.

Why is it a varchar anyway? You declare the variable as an int at the beginning, so why then convert it?
Go to Top of Page
   

- Advertisement -