开发者

SQL Query...nulls hosing me

开发者 https://www.devze.com 2022-12-17 06:43 出处:网络
I tried to ask this question before but I don\'t think I explained myself very well. So here it is: asp.net 2.0 app hitting a SQL 2008 backend. This seems simple but I can\'t get it. 1 table. The user

I tried to ask this question before but I don't think I explained myself very well. So here it is: asp.net 2.0 app hitting a SQL 2008 backend. This seems simple but I can't get it. 1 table. The user selects a status. The query should return all records = the chosen status only. If t开发者_JAVA百科he user select "All Status", then ALL records should be returned, including those with a status = null (which is the part that is hosing me).

Ex:

CASE 1: User selects Status = "Satisfied"; ONLY satisfied records are return

CASE 2: User selects All Status = everything is returned, satisfied AND nulls and anything else

I tried passign in a wildcard but this doesn't return nulls. I tried dynamically buildign the query but I would like to avoid it.


For the case where you want them all, how about: (am I missing something?)

SELECT * FROM <tablename>


SELECT * FROM <tablename> WHERE Status = '*' OR Status IS NULL

...or '%' or whatever wildcard your SQL implementation uses.

...or if you really have no other conditions following this, just select *...


Try this in a stored procedure.

--Pass in @status as a parameter
DECLARE @Status varchar(100)

IF @Status = 'All Status'
BEGIN
  SELECT * FROM tablename
END
ELSE
BEGIN
  SELECT * FROM tablename where statusfield = @Status
END


One option is

declare @status varchar(50)

SELECT * FROM <tablename> WHERE (@status is null) or (Status = @status)

if you pass null in for the @status parameter then it will return all records. If you pass 'satisfied' or whatever then it will return just those matchng records.

If doing this in SQL 2008, be sure you have SP1 and Cumulative Update 5 installed. Further, I would recommend adding the WITH RECOMPILE option to the procedure. Under those conditions it will be as performant as embedded SQL or even using unions.

See the following article for an indepth discussion of the myriad of ways to perform searching in SQL 2008: Dynamic Search Conditions in T-SQL


The problem is that SQL's = operator always returns NULL when one of the operands is NULL, so using status = '%' indeed doesn't work. The best method is to just not include a condition on status if you want all of them. You can add extra NULL tests to the query, but that again is building it dynamically, I don't see a way to avoid that...


basically your statement will be for 'Statisfied'

SELECT * FROM testtab WHERE COALESCE(statuscolumn, '') LIKE '%Statified'

for 'All Status' it will be

SELECT * FROM testtab WHERE COALESCE(statuscolumn, '') LIKE '%'

you could use this statment and if selection is 'All Status' then pass a '' for the @status from your UI

SELECT * FROM testtab WHERE COALESCE(statuscolumn, '') LIKE '%' || @status

Or you can use this one and when you pass the selection from UI make sure it has a '%' (wild char) appended to your status when it not 'All Status'. When its 'All Status' just pass '%' for the @status

SELECT * FROM testtab WHERE COALESCE(statuscolumn, '') LIKE @status

oh your db is mssql? :) then you will need to replace the collace(statuscolumn, '') with isnull(statuscolumn, '').


Just skip the where clause or the part that is about the status field, example:

SELECT * from table_1 Where status = 'Satisfied' and SELECT * from table_1


When you want all records, you have to exclude STATUS from your WHERE clause (or use a UNION and a select statement where STATUS IS NULL).

Depending on what version of SQL you are using, you might be able to use an IF..ELSE... statement.

IF Status='ALL' THEN ... A SELECT statement where STATUS is NOT included in the WHERE

ELSE ... A SELECT statement that has a WHERE with only the status you are looking for


SELECT * FROM <tablename> WHERE isnull(Status,'*') = '*'


Assuming you're passing a variable to an SP:

SELECT * FROM Table WHERE Status CASE @status WHEN 'All status' THEN Status ELSE @status END

Otherwise, you need to concatenate in the selected value within quotes at both places where it currently says @status


The idea is what to do when the users chooses 'All Status'. By setting the param to NULL, you can use the isnull and then each [status] field just needs to equal itself. I've used ISNULL to set to '' to avoid having NULL = NULL>

declare @param_choice varchar(25)

if @param_choice = 'All Status'
   Begin
     @param_choice = NULL
   End

-- get your results
Select * from Some_Table
Where IsNull([Status], '') = IsNull(@param_choice, IsNull([Status], ''))


You'll get the best performance from:

IF @status IS NULL
  BEGIN

    SELECT t.*
      FROM TABLE t

  END
ELSE
  BEGIN

    SELECT t.*
      FROM TABLE t
     WHERE t.status = @status

  END

The next option is to use:

SELECT t.*
  FROM TABLE t
 WHERE (@status IS NULL OR t.status = @status)

...but that is not sargable.


I do not believe that anyone suggested using a UNION.

SELECT t.*
  FROM TABLE t
 WHERE (@status IS NULL)

UNION ALL

SELECT t.*
  FROM TABLE t
 WHERE (t.status = @status)

If @status is NULL, then the first query in the union is executed, and t.status = @status is clearly always false, so the second query in the union is not executed at all.

If @status is not null, then the first query in the union is not exected at all, and the second one is.

Importantly, since ISNULL, COALESCE or a function are not used on t.status or on @status, then if there is an index on status, it can be used. That is, the predicate is SARGABLE.

And I used UNION ALL (instead of UNION) to prevent a SORT and DISTINCT operation that can be very slow.

0

精彩评论

暂无评论...
验证码 换一张
取 消