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 2000 Forums
 Transact-SQL (2000)
 A more efficient way to achieve this SELECT?

Author  Topic 

julianfraser
Starting Member

19 Posts

Posted - 2005-05-09 : 13:21:07
I have a stored procedure that returns a table using a dynamic WHERE claus that is generated by an ASP script. I wan to know if there is a more efficient way to achive this.

Also the '&_' that I have put at the end of each line, is this correct, I haven't tried this sproc yet so I don't know.

And is the correct way to cancel a quote mark within a string with a backslash? Ie. 'here is \'some\' text'???



Here is the sproc...

CREATE PROC sproc_search
@where_clause
AS
DECLARE @sql_string varchar(8000)

SELECT @sql_string = 'SELECT p.propertyID, pt.property_type, p.address1, p.address2, p.town, p.county, p.postcode, p.price, p.bedrooms, p.bathrooms ' &_
'FROM property p ' &_
'INNER JOIN property_type pt ON pt.property_typeID = p.property_typeID ' &_
'WHERE ' + @where_clause

EXEC (@sql_string)
GO



Here is an example of the dynamically generated where clause...

(property_typeID = 'HDE' OR property_typeID = 'HSD' OR property_typeID = 'HTE')
AND (price BETWEEN 70000 AND 450000)
AND (bedrooms BETWEEN 0 AND 8)
AND (bathrooms BETWEEN 0 AND 8)
AND (sale_statusID = 'FSL' OR sale_statusID = 'UOF')
AND (property_termID = 'FRE' OR property_termID = 'FSH')
AND (propertyID IN (SELECT propertyID FROM LM001_property_property_feature WHERE property_featureID = 'BGR')
AND (propertyID IN (SELECT propertyID FROM LM001_property_property_feature WHERE property_featureID = 'GDE')
AND (propertyID IN (SELECT propertyID FROM LM001_property_property_feature WHERE property_featureID = 'GUN')
AND (propertyID IN (SELECT propertyID FROM LM001_property_property_feature WHERE property_featureID = 'CAB')





Thanks,
Julian.

X002548
Not Just a Number

15586 Posts

Posted - 2005-05-09 : 14:24:40
Just use a + instead of &_

Here's a trick that populates a table per column...and it's not dynamic

http://weblogs.sqlteam.com/brettk/archive/2004/05/05/1312.aspx



Brett

8-)
Go to Top of Page
   

- Advertisement -