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 |
|
dimitri24
Starting Member
8 Posts |
Posted - 2003-03-13 : 12:25:13
|
| Below is my stored procedure which produces error when @UNITS <> 0 because there is a conflict with assigning.Is there any other way to assign or am i missing something.Ofcource my stored procedure is much bigger and this is simplified versonCREATE PROCEDURE [SEARCH] @Units intASDECLARE @SQL nvarchar(1500)SELECT @SQL = "SELECT * FROM TABLE WHERE ID IS NOT NULL"If @Units <> 0 Begin SELECT @SQL = @SQL + " AND UNITS >" + @UNITS End EXECUTE sp_executesql @SQL |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2003-03-13 : 12:30:56
|
Don't use dynamic SQL here. Try this....create proc [search] @units intasselect *from tablewhere id is not null and units >= coalesce(@units,units) + 1 orwhere id is not null and ((@units <> 0 and units > @units) or (isnull(@units,0) = 0)) Jay White{0} |
 |
|
|
dimitri24
Starting Member
8 Posts |
Posted - 2003-03-13 : 14:39:29
|
quote: Sorry it has to be a dynamic sql. Is there any special encoding i could do ?
|
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-03-13 : 14:56:08
|
| WHY?Brett8-) |
 |
|
|
dimitri24
Starting Member
8 Posts |
Posted - 2003-03-13 : 15:05:04
|
Because i had a lot of different data coming into stored procedure and i only have problems with integer because of the quotesquote: WHY?Brett8-)
|
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-03-13 : 15:12:18
|
quote: Because i had a lot of different data coming into stored procedure and i only have problems with integer because of the quotes
But that's not what you posted. You only have a sproc with an input parameter on int. What Jay Posted will work.I'm thinikng though that you've got something else going on. Can you post what you actually have, or is it too huge?Brett8-) |
 |
|
|
dimitri24
Starting Member
8 Posts |
Posted - 2003-03-13 : 15:24:43
|
| HERE IT COMES:CREATE PROCEDURE [PROPERTIES SEARCH] @ArborUserID varchar(50), @PropertyAddress varchar(300), @PropertyCity varchar(100), @PropertyState char(10), @PropertyZipCode char(50), @NumberOfUnits char(50), @NumberOfFloors char(50), @ClosingDateFrom varchar(100), @ClosingDateTo varchar(100), @MinimumPrice char(50), @MaximumPrice char(50), @Properties varchar(100), @Sort varchar(100) ASDECLARE @SQL nvarchar(1500)SELECT @SQL = "SELECT PROPERTY.*, CONTACT.SALES_ID AS B_SALES_ID,"+ " S_CONTACT.SALES_ID AS S_SALES_ID, SB_CONTACT.SALES_ID AS SB_SALES_ID, "+ " LB_CONTACT.SALES_ID AS LB_SALES_ID, USERINFO.FULLNAME AS B_FULLNAME, "+ " USERINFO1.FULLNAME AS LB_FULLNAME, USERINFO2.FULLNAME AS S_FULLNAME, "+ " USERINFO3.FULLNAME AS SB_FULLNAME "+ "FROM USERINFO "+ " INNER JOIN CONTACT ON USERINFO.USERID = CONTACT.SALES_ID "+ " RIGHT OUTER JOIN USERINFO USERINFO2 "+ " INNER JOIN CONTACT S_CONTACT ON USERINFO2.USERID = S_CONTACT.SALES_ID "+ " RIGHT OUTER JOIN CONTACT SB_CONTACT "+ " INNER JOIN USERINFO USERINFO3 ON SB_CONTACT.SALES_ID = USERINFO3.USERID "+ " RIGHT OUTER JOIN PROPERTY ON SB_CONTACT.CONTACT_ID = PROPERTY.[Selling Broker] "+ " ON S_CONTACT.CONTACT_ID = PROPERTY.Seller ON CONTACT.CONTACT_ID = PROPERTY.Buyer "+ " LEFT OUTER JOIN USERINFO USERINFO1 "+ " INNER JOIN CONTACT LB_CONTACT ON USERINFO1.USERID = LB_CONTACT.SALES_ID "+ " ON PROPERTY.[Listing Broker] = LB_CONTACT.CONTACT_ID "+ "WHERE (PROPERTY.PROPERTY_ID IS NOT NULL)" If @PropertyAddress <> "Empty" Begin SELECT @SQL = @SQL + " AND PROPERTY.[PROPERTY ADDRESS] = '" + @PropertyAddress + "'" EndIf @PropertyCity <> "Empty" Begin SELECT @SQL = @SQL + " AND PROPERTY.[PROPERTY CITY] Like '%" + @PropertyCity + "%'" EndIf @PropertyState <> "Empty"Begin SELECT @SQL = @SQL + " AND PROPERTY.[PROPERTY STATE] = '" + ltrim(rtrim(@PropertyState)) + "'"EndIf @PropertyZipCode <> 0Begin SELECT @SQL = @SQL + " AND PROPERTY.[PROPERTY ZIP CODE] = '" + ltrim(rtrim(@PropertyZipCode)) + "'"EndIf @NumberOfUnits <> 0Begin SELECT @SQL = @SQL + " AND PROPERTY.[NUMBER OF UNITS] >= '" + ltrim(rtrim(@NumberOfUnits)) + "'"EndIf @NumberOfFloors <> 0Begin SELECT @SQL = @SQL + " AND PROPERTY.[NUMBER OF FLOORS] >= '" + ltrim(rtrim(@NumberOfFloors)) + "'"EndIf @ClosingDateFrom <> "Empty" AND @ClosingDateTo <> "Empty"Begin SELECT @SQL = @SQL + " AND PROPERTY.[CLOSING DATE] BETWEEN '" + ltrim(rtrim(@ClosingDateFrom)) + "' AND '" + ltrim(rtrim(@ClosingDateTo)) + "'"EndSELECT @SQL = @SQL + " AND PROPERTY.[SALES PRICE] BETWEEN " + ltrim(rtrim(@MinimumPrice)) + " AND " + ltrim(rtrim(@MaximumPrice)) + ""If @Properties <> "Empty"Begin SELECT @SQL = @SQL + " AND (CONTACT.SALES_ID = '" + @ArborUserID + "' OR LB_CONTACT.SALES_ID = '" + @ArborUserID + "' OR S_CONTACT.SALES_ID = '" + @ArborUserID + "' OR SB_CONTACT.SALES_ID = '" + @ArborUserID + "') "EndSELECT @SQL = @SQL + " ORDER BY " + @Sort--PRINT @SQL + char(13) + char(13) + char(13) + char(13)EXECUTE sp_executesql @SQL |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-03-13 : 16:00:05
|
| OK, Try the following:But first:1. You definetly cut and pasted this out of Access. Access makes messy code, I wouldn't do that if I was you2. Change the values you are passing from the word empty and just allows Nulls3. Are you building the string because the query is a dog?4. Label your tables with something simple, so you only have to qualify the columns with 1 or 2 letters (t1.columnname)5. Don't use spaces in the names of Objects, special chars either (although I reserve the right to defend "_" from my DB2 Days)6. I'm not going to begin to even try and understand what's going on here. You should really simply what you're doing.7. Never use SELECT * in a sprocAnyway, GOOD LUCK, and hope this helps.CREATE PROCEDURE [PROPERTIES SEARCH] @ArborUserID varchar(50) = Null, @PropertyAddress varchar(300) = Null, @PropertyCity varchar(100) = Null, @PropertyState char(10) = Null, @PropertyZipCode char(50) = Null, @NumberOfUnits char(50) = Null, @NumberOfFloors char(50) = Null, @ClosingDateFrom varchar(100) = Null, @ClosingDateTo varchar(100) = Null, @MinimumPrice char(50) = Null, @MaximumPrice char(50) = Null, @Properties varchar(100) = Null, @Sort varchar(100) AS SELECT PROPERTY.*, CONTACT.SALES_ID AS B_SALES_ID,"S_CONTACT.SALES_ID AS S_SALES_ID, SB_CONTACT.SALES_ID AS SB_SALES_ID,LB_CONTACT.SALES_ID AS LB_SALES_ID, USERINFO.FULLNAME AS B_FULLNAME,USERINFO1.FULLNAME AS LB_FULLNAME, USERINFO2.FULLNAME AS S_FULLNAME,USERINFO3.FULLNAME AS SB_FULLNAMEFROM USERINFO INNER JOIN CONTACT ON USERINFO.USERID = CONTACT.SALES_ID RIGHT OUTER JOIN USERINFO USERINFO2 INNER JOIN CONTACT S_CONTACT ON USERINFO2.USERID = S_CONTACT.SALES_ID RIGHT OUTER JOIN CONTACT SB_CONTACT INNER JOIN USERINFO USERINFO3 ON SB_CONTACT.SALES_ID = USERINFO3.USERID RIGHT OUTER JOIN PROPERTY ON SB_CONTACT.CONTACT_ID = PROPERTY.[Selling Broker] ON S_CONTACT.CONTACT_ID = PROPERTY.Seller ON CONTACT.CONTACT_ID = PROPERTY.Buyer LEFT OUTER JOIN USERINFO USERINFO1 INNER JOIN CONTACT LB_CONTACT ON USERINFO1.USERID = LB_CONTACT.SALES_ID ON PROPERTY.[Listing Broker] = LB_CONTACT.CONTACT_IDWHERE (PROPERTY.PROPERTY_ID IS NOT NULL)AND PROPERTY.[PROPERTY ADDRESS] = @PropertyAddress AND PROPERTY.[PROPERTY CITY] Like '%' + @PropertyCity + '%'AND PROPERTY.[PROPERTY STATE] = ltrim(rtrim(@PropertyState))AND PROPERTY.[PROPERTY ZIP CODE] = ltrim(rtrim(@PropertyZipCode)) AND PROPERTY.[NUMBER OF UNITS] >= ltrim(rtrim(@NumberOfUnits)) AND PROPERTY.[NUMBER OF FLOORS] >= ltrim(rtrim(@NumberOfFloors))AND PROPERTY.[CLOSING DATE] BETWEEN ltrim(rtrim(@ClosingDateFrom)) AND ltrim(rtrim(@ClosingDateTo))AND PROPERTY.[SALES PRICE] BETWEEN + ltrim(rtrim(@MinimumPrice)) AND ltrim(rtrim(@MaximumPrice))AND (CONTACT.SALES_ID = @ArborUserID OR LB_CONTACT.SALES_ID = @ArborUserID OR S_CONTACT.SALES_ID = @ArborUserID OR SB_CONTACT.SALES_ID = @ArborUserID)ORDER BY CASE WHEN @SORT = 'S_SALES_ID' THEN S_CONTACT.SALES_ID WHEN @SORT = 'SB_SALES_ID' THEN SB_CONTACT.SALES_ID END-- ADD A WHEN CLAUSE FOR EVERY VALUE YOU CAN HAVE FOR @SORTBrett8-)Edited by - x002548 on 03/13/2003 16:08:04 |
 |
|
|
dimitri24
Starting Member
8 Posts |
Posted - 2003-03-13 : 17:12:54
|
1. I cut this code from a View in the SQL Enterprise2. If the value for example a parameter @PropertyAddress is empty then i am compering field to a null value which will not return any results.I adjusted the code you have given and i didn't return any value !quote: OK, Try the following:But first:1. You definetly cut and pasted this out of Access. Access makes messy code, I wouldn't do that if I was you2. Change the values you are passing from the word empty and just allows Nulls3. Are you building the string because the query is a dog?4. Label your tables with something simple, so you only have to qualify the columns with 1 or 2 letters (t1.columnname)5. Don't use spaces in the names of Objects, special chars either (although I reserve the right to defend "_" from my DB2 Days)6. I'm not going to begin to even try and understand what's going on here. You should really simply what you're doing.7. Never use SELECT * in a sprocAnyway, GOOD LUCK, and hope this helps.CREATE PROCEDURE [PROPERTIES SEARCH] @ArborUserID varchar(50) = Null, @PropertyAddress varchar(300) = Null, @PropertyCity varchar(100) = Null, @PropertyState char(10) = Null, @PropertyZipCode char(50) = Null, @NumberOfUnits char(50) = Null, @NumberOfFloors char(50) = Null, @ClosingDateFrom varchar(100) = Null, @ClosingDateTo varchar(100) = Null, @MinimumPrice char(50) = Null, @MaximumPrice char(50) = Null, @Properties varchar(100) = Null, @Sort varchar(100) AS SELECT PROPERTY.*, CONTACT.SALES_ID AS B_SALES_ID,"S_CONTACT.SALES_ID AS S_SALES_ID, SB_CONTACT.SALES_ID AS SB_SALES_ID,LB_CONTACT.SALES_ID AS LB_SALES_ID, USERINFO.FULLNAME AS B_FULLNAME,USERINFO1.FULLNAME AS LB_FULLNAME, USERINFO2.FULLNAME AS S_FULLNAME,USERINFO3.FULLNAME AS SB_FULLNAMEFROM USERINFO INNER JOIN CONTACT ON USERINFO.USERID = CONTACT.SALES_ID RIGHT OUTER JOIN USERINFO USERINFO2 INNER JOIN CONTACT S_CONTACT ON USERINFO2.USERID = S_CONTACT.SALES_ID RIGHT OUTER JOIN CONTACT SB_CONTACT INNER JOIN USERINFO USERINFO3 ON SB_CONTACT.SALES_ID = USERINFO3.USERID RIGHT OUTER JOIN PROPERTY ON SB_CONTACT.CONTACT_ID = PROPERTY.[Selling Broker] ON S_CONTACT.CONTACT_ID = PROPERTY.Seller ON CONTACT.CONTACT_ID = PROPERTY.Buyer LEFT OUTER JOIN USERINFO USERINFO1 INNER JOIN CONTACT LB_CONTACT ON USERINFO1.USERID = LB_CONTACT.SALES_ID ON PROPERTY.[Listing Broker] = LB_CONTACT.CONTACT_IDWHERE (PROPERTY.PROPERTY_ID IS NOT NULL)AND PROPERTY.[PROPERTY ADDRESS] = @PropertyAddress AND PROPERTY.[PROPERTY CITY] Like '%' + @PropertyCity + '%'AND PROPERTY.[PROPERTY STATE] = ltrim(rtrim(@PropertyState))AND PROPERTY.[PROPERTY ZIP CODE] = ltrim(rtrim(@PropertyZipCode)) AND PROPERTY.[NUMBER OF UNITS] >= ltrim(rtrim(@NumberOfUnits)) AND PROPERTY.[NUMBER OF FLOORS] >= ltrim(rtrim(@NumberOfFloors))AND PROPERTY.[CLOSING DATE] BETWEEN ltrim(rtrim(@ClosingDateFrom)) AND ltrim(rtrim(@ClosingDateTo))AND PROPERTY.[SALES PRICE] BETWEEN + ltrim(rtrim(@MinimumPrice)) AND ltrim(rtrim(@MaximumPrice))AND (CONTACT.SALES_ID = @ArborUserID OR LB_CONTACT.SALES_ID = @ArborUserID OR S_CONTACT.SALES_ID = @ArborUserID OR SB_CONTACT.SALES_ID = @ArborUserID)ORDER BY CASE WHEN @SORT = 'S_SALES_ID' THEN S_CONTACT.SALES_ID WHEN @SORT = 'SB_SALES_ID' THEN SB_CONTACT.SALES_ID END-- ADD A WHEN CLAUSE FOR EVERY VALUE YOU CAN HAVE FOR @SORTBrett8-)Edited by - x002548 on 03/13/2003 16:08:04
|
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-03-14 : 14:41:06
|
| Ok, Try wrapping the local variables with this:IsNull(@LocVar,'') Give that a try, and let me know.Brett8-) |
 |
|
|
dimitri24
Starting Member
8 Posts |
Posted - 2003-03-19 : 08:43:40
|
You have solved the problem of Null values but now in the query the values are compared to a empty string.Ex: AND PROPERTY.[PROPERTY ADDRESS] = '' AND PROPERTY.[PROPERTY STATE] = ''By having IF statement i have an option of including compare statement if the value is not null, if it is the criteria is not included.quote: Ok, Try wrapping the local variables with this:IsNull(@LocVar,'') Give that a try, and let me know.Brett8-)
|
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-03-19 : 09:55:22
|
| OK, I got ya...I guess we're all at a point of which is the better performing method. I found (and tested) a method that will work for you. The BIG question is, will it be more of a dog than dynamic sql. With the few test rows I created, it won't give you that answer.But I offer this as a possible solution for you.How many rows are we talking about, btw?Anyway, good luck with your development.Here's the code:CREATE TABLE TABLE1 (Item varchar(10), Status varchar(10))GoInsert Into Table1 (Item, Status)SELECT '10180-004','ACTIVE' UNION ALL SELECT '10180-005','ACTIVE' UNION ALL SELECT '10180-012','OBSOLETE' UNION ALL SELECT '10180-013','PRECOST' UNION ALL SELECT '10181-001','PHASEOUT' UNION ALL SELECT '10181-002','OBSOLETE' UNION ALL SELECT '10182-000','OBSOLETE' UNION ALL SELECT '10182-001','ACTIVE' UNION ALL SELECT '10183-000','RELEASED' UNION ALL SELECT '10183-001','PHASEOUT' UNION ALL SELECT '10183-002','OBSOLETE'GoDeclare @x varchar(10), @y varchar(10)SELECT @x = Null, @y = 'ACTIVE'SELECT * FROM TABLE1WHERE Item Like Case When @x Is Null Then '%' Else @x End And Status Like Case When @y Is Null Then '%' Else @y EndGoDrop Table Table1Brett8-) |
 |
|
|
|
|
|
|
|