Link to home
Start Free TrialLog in
Avatar of Midnight2005
Midnight2005

asked on

SQL SERVER 2005 Filter

I see that things has changed in SQL Server 2005, maybe I am going to need to learn XML, I am not sure, but will do whatever it takes.

Let me simplify my question
Lets say I have a State, Car, City Table and have 5 records Like this
TX Ford Houston
TX Chevy Dallas
LA Mazda NewOrleans
GA Ford Atlanta
NY Ford NewYork

On Visual Studio, I have a distinct State Checkboxes and distinct car checkboxes seperate for each.
And the user only want to see the cities in (TX and NY) and (Fords and Mazda)
So if we apply the filters the result is
TX Ford         Houston
NY Ford        NewYork

So I need to pass parameters (TX,NY) AND (FORD, MAZDA) to my Stored Procedure somehow.
I currently have (TX, NY) in an ArrayList (500) and Ford, Mazda in another ArrayList(500) in VB.NET.
SQL side needs to be dynamic in size because I have many things as you see.
I am using VS2005 and SQL Server 2005
Thanks bunch for help.
Avatar of bamboo7431
bamboo7431

The dirty solution would be: join your arrays, creating comma-delimited lists "TX,NY" and "FORD,"MAZDA"; pass them to the proc.
Then you could do
SELECT * FROM MyTable WHERE State IN (@stateList) AND Car IN (@carList)
declare the @stateList and @carList parameters as NVARCHAR(4000) so you should be good until they are greater than 4000 characters long.
The clean solution would be to pass XML with all the states and cars and parse it in the SP, but I'd try the dirty solution first :)
Avatar of Midnight2005

ASKER

Interesting, I tired that, this is exactly how I tired to do, passing them in a comma delimited string.
In this situation, I think I can say I passed in to two different varchar, and I tired to use the "IN" command, I thought the reason why I got the error was because SQL Server 200 doesn't like IN anymore.
The best is maybe to create this simple database in SQL only and see it can be worked with this 5 records...You sound like you are sure it would work...
As long as it works...:)
ASKER CERTIFIED SOLUTION
Avatar of bamboo7431
bamboo7431

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just for fun, here's my script with dynamic SQL - NOT something I'd like to put on a production system...

DROP TABLE #Test
CREATE TABLE #Test(State CHAR(2), Car VARCHAR(10), City VARCHAR(20))
SET NOCOUNT ON
INSERT INTO #Test VALUES ('TX', 'Ford', 'Houston')
INSERT INTO #Test VALUES ('TX', 'Chevy', 'Dallas')
INSERT INTO #Test VALUES ('LA', 'Mazda', 'New Orleans')
INSERT INTO #Test VALUES ('GA', 'Ford', 'Atlanta')
INSERT INTO #Test VALUES ('NY', 'Ford', 'New York')
DROP PROCEDURE getRecords
GO
CREATE PROCEDURE getRecords(@carList VARCHAR(2000), @stateList VARCHAR(2000))
AS BEGIN
DECLARE @dynamicSQL NVARCHAR(4000)
      SET @dynamicSQL = 'SELECT * FROM #Test WHERE State IN(''' +
      + Replace(@stateList, ',', ''',''') + ''') AND Car IN(''' +
      + Replace(@carList, ',', ''',''') + ''')'
      --PRINT @dynamicSQL
      EXECUTE sp_executesql @dynamicSQL
END
GO
EXEC dbo.getRecords 'Ford,Mazda', 'TX,NY'
Thanks for shraing your script, I also see what you did. I will try at my computer tomorrow..Currently, I am going to wait for other people's inputs, too, in case anybody would like to add.
What would be the best way to solve this?
I am going to put this on our production server, we have some speed problem, too so I'd like to get it in the right way.
Bamboo,
Did you mean you wouldn't put Dynamic SQL solution in production server or you meant you wouldn't put this temporary test (script) at the production server?
If the best way is XML, could you maybe give me a small sample, so that we would done with this? I need to implement something soon.
I see why you said it, dynamic Sql's are slower compare to static ones.
I wouldn't do this crazy string manipulation coupled with dynamic SQL on a production server. Use the link I gave above - he has a good article and a nice function for you to use.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial