Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Can I Do Dynamic Query Construction in SQL Server SP
Message
 
 
À
11/10/2009 16:24:55
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
Information générale
Forum:
Visual FoxPro
Catégorie:
Client/serveur
Divers
Thread ID:
01428769
Message ID:
01428878
Vues:
43
>
>create procedure MySP ( @customerId varchar(10) = NULL, @employeeID int = NULL )
>as
>begin
>   select * from Orders
>   where 
>          CustomerID = case when @customerId is null then CustomerID else @CustomerID end
>   and EmployeeID = case when @employeeID is null then employeeID else @employeeID end  
>end
>
>You could then do:
>
>Exec MySP
>Exec MySP 'BONAP'
>Exec MySP 'BONAP',4
>Exec MySP null,4
>
>
>The other approach is based on sp_executeSQL and builds the query as a string dropping the NULL conditions.
>Cetin

The two simpler versions (though they return different results) would be
select * from Orders where (@CustomerID IS NULL or CustomerIID = @CustomerID) 
and (@EmployeeID IS NULL or EmployeeID = @EmployeeID)
Alternative solution
select * from Orders where CustomerIID = ISNULL(@CustomerID,CustomerID and EmployeeID = ISNULL(@EmployeeID, EmployeeID)
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform