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 |
V8S
Starting Member
3 Posts |
Posted - 2011-10-19 : 09:10:49
|
Hi,We have a table called Articles where each record contains an article about buying property in a particular area which we display on our website. Each article record has an ID.Each record also contains fields called Country, Region and PropertyType that describe criteria for properties that relate to that article, so that on our website we can serve a list of properties for sale from our Properties table next to the article text.At the moment I am reading both the article text and these criteria fields into my ASP page and then passing all the criteria back again into a separate stored procedure in order to generate a recordset to display the correct properties. This seems rather inefficient, and produces problems when those options include the apostrophe character such as Cote d'Azur.EXAMPLE RECORDArticleID = 1ArticleTitle = Normandy Houses For SaleCountry = FranceRegion = NormanyPropertyType = HouseMy question is:Can I write a single stored procedure that will read the criteria from the Articles record in one SELECT, and then use them in a second SELECT statement to produce the recordset I want?So, in essence...SELECT ArticleTitle,Country,Region,PropertyType FROM Articles WHERE ArticleID = 1SELECT PropertyID,Country,Region,District,PropertyType,Price,Picture,Bedrooms FROM Properties WHERE Country = ArticleCountry AND Region = ArticleRegion AND PropertyType = ArticlePropertyTypeThis will allow me, in the ASP page, to just call the procedure with just the ArticleID and not worry about the apostrophe problem.Thanks in advance. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-19 : 09:44:43
|
you canSELECT p.PropertyID,p.Country,.pRegion,p.District,p.PropertyType,p.Price,p.Picture,p.Bedrooms FROM Properties pINNER JOIN Articles aON a.Country = p.CountryAND a.Region = p.RegionAND a.PropertyType = p.PropertyType WHERE ArticleID = 1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-10-19 : 09:49:13
|
You can make the first SELECT a derived table and join it to the second SELECT.SELECT p.PropertyID,p.Country,p.Region,p.District,p.PropertyType,p.Price,p.Picture,p.Bedrooms FROM Properties pjoin (SELECT ArticleTitle,Country,Region,PropertyType FROM Articles WHERE ArticleID = 1) dton dt.Country = p.Country and dt.Region = p.Region and dt.PropertyType = p.PropertyType No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-10-19 : 09:50:13
|
Argh - have not seen that there isn't a reason to have a derived table.  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|