2013-07-25 10 views
12

Ich habe eine gespeicherte Prozedur, die Informationen aus einer Tabelle basierend auf 4 Parameter abruft.SQL ignorieren Teil von WHERE, wenn der Parameter null ist

Ich möchte Werte basierend auf den Parametern abrufen, aber wenn ein Parameter NULL ist, dann wird dieser Parameter nicht überprüft. Wenn also alle 4 Parameter null sind, würde ich die gesamte Tabelle anzeigen.

Das ist mein SP (wie Sie funktioniert dies nur für 1 Parameter atm sehen):

CREATE PROCEDURE myProcedure 
    @Param1 nvarchar(50), 
    @Param2 nvarchar(50), 
    @Param3 nvarchar(50), 
    @Param4 nvarchar(50) 
AS 
BEGIN 
    IF(@Param1 IS NULL) 
     BEGIN 
      SELECT Id, col1, col2, col3, col4 FROM myTable 
     END 
    ELSE 
     BEGIN 
      SELECT Id, col1, col2, col3, col4 FROM myTable WHERE col1 LIKE @Param1+'%' 
     END 
END 

Gibt es eine Möglichkeit eine dieser ohne mit IF für jede mögliche Kombination zu tun (15 IFs)

+1

Erland Sommarskogs [Dynamische Suchbedingungen in T-SQL] (http://www.sommarskog.se/dyn-search.html) wäre der übliche Startplatz. –

+0

mögliches Duplikat von [Stored Procedure mit optionalen "WHERE" -Parametern] (http://stackoverflow.com/questions/697671/stored-procedure-with-optional-where- parameters) – GSerg

Antwort

21

Wie wäre es so etwas wie

SELECT Id, col1, col2, col3, col4 
FROM myTable 
WHERE col1 LIKE @Param1+'%' 
OR  @Param1 IS NULL 

in diesem speziellen Fall, dass Sie auch

genutzt haben könnte
SELECT Id, col1, col2, col3, col4 
FROM myTable 
WHERE col1 LIKE ISNULL(@Param1,'')+'%' 

Aber im Allgemeinen kann man so etwas wie

SELECT Id, col1, col2, col3, col4 
FROM myTable 
WHERE (condition1 OR @Param1 IS NULL) 
AND  (condition2 OR @Param2 IS NULL) 
AND  (condition3 OR @Param3 IS NULL) 
... 
AND  (conditionN OR @ParamN IS NULL) 
+3

Vorsicht. Das 'IS NULL' führt zu einem vollen Scan-Tabellenzugriff. –

2
CREATE PROCEDURE myProcedure 
    @Param1 nvarchar(50), 
    @Param2 nvarchar(50), 
    @Param3 nvarchar(50), 
    @Param4 nvarchar(50) 
AS 
BEGIN 
    IF(@Param1 IS NULL) 
     BEGIN 
      SELECT Id, col1, col2, col3, col4 FROM myTable 
     END 
    ELSE 
     BEGIN 
      SELECT Id, col1, col2, col3, col4 FROM myTable WHERE col1 LIKE @Param1+'%' OR @Param1 is Null 
     END 
END 

Diese versuchen sollte helfen

Grüße

Ashutosh Arya

1

Wenn Sie @ param1 meine ist, Parameter für col1, @ param2 ist Parameter für col2, ... etc können Sie dies versuchen:

CREATE PROCEDURE myProcedure 
@Param1 nvarchar(50), 
@Param2 nvarchar(50), 
@Param3 nvarchar(50), 
@Param4 nvarchar(50) 
AS 
BEGIN 
declare @query nvarchar(4000) 
SET @query='SELECT Id, col1, col2, col3, col4 FROM myTable '+ 
    (case when ((@Param1 is null) and (@Param2 is null) and (@Param3 is null) and (@Param4 is null)) 
    then '' 
    else 
     'where '+ 
     (case when @Param1 is not null 
     then ' col1 like '''[email protected]+'%'''+ 
      (case when @param2 is not null then ' AND ' else '' end) 
     else '' end)+ 
     (case when @Param2 is not null 
     then ' col2 like '''[email protected]+'%'''+ 
      (case when @param3 is not null then ' AND ' else '' end) 
     else '' end)+ 
     (case when @Param3 is not null 
     then ' col3 like '''[email protected]+'%'''+ 
      (case when @param4 is not null then ' AND ' else '' end) 
     else '' end)+ 
     (case when @Param4 is not null 
     then ' col4 like '''[email protected]+'%''' 
     else '' end) 
    end) 

exec sp_sqlexec @query 
0

Sie kann verwenden COALESCE() Funktion in SQL Server. Sie müssen IF- Else oder CASE in Ihren Aussagen nicht verwenden. Hier ist, wie Sie COALESCE Funktion verwenden können.

SELECT Id, col1, col2, col3, col4 FROM myTable where col1 = COALESCE(NULLIF(@param1, ''), col1) and col2 = COALESCE(NULLIF(@param2, ''), col2) and col3 = COALESCE(NULLIF(@param3, ''), col3) and col4= 
COALESCE(NULLIF(@param4, ''), col4) 

Die COALESCE Funktion in SQL gibt die erste Nicht-NULL-Expression unter ihren Argumenten. Wenn zum Beispiel @ param1 gleich null ist, gibt die Funktion col1 zurück, was zu col1 = col1 in der where-Anweisung führt, die wie 1 = 1 ist, was bedeutet, dass die Bedingung immer wahr ist.

Verwandte Themen