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.
| 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 ONDECLARE @SQL VARCHAR(8000)SET @SQL = 'SELECT r.REListingID,r.REListingDate,r.RESubject,a.RentCharge,b.thumbFROM db_REListings as rLEFT 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.ListingIDWHERE 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 @SQLAny ideas? When i run that query i get Msg 102, Level 15, State 1, Line 6Incorrect 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 ONDECLARE @SQL VARCHAR(8000)SET @SQL = 'SELECT r.REListingID,r.REListingDate,r.RESubject,a.RentCharge,b.thumbFROM db_REListings as rLEFT 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.ListingIDWHERE 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 @SQLAny ideas? When i run that query i get Msg 102, Level 15, State 1, Line 6Incorrect 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/ |
 |
|
|
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... |
 |
|
|
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... |
 |
|
|
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. SELECTr.REListingID,r.REListingDate,r.RESubject,a.RentCharge,b.thumbFROM db_REListings as rLEFT 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.ListingIDWHERE 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- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
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... |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2007-12-03 : 06:31:29
|
Try:IF @WindowAir = 1SET @SQL = @SQL + ' AND a.WindowAir = ''' + CONVERT(VARCHAR(20), @WindowAir) + ''IF @CentralAC = 1SET @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? |
 |
|
|
|
|
|
|
|