SQL Server存储过程WHERE子句

时间:2021-11-16 11:03:52

Need help with WHERE clause in this stored procedure.

在此存储过程中需要有关WHERE子句的帮助。

How to write WHERE with this parameters and if any of these param contains specific value, then I need to get all values from that column ?

如何使用此参数编写WHERE,如果这些参数中的任何一个包含特定值,那么我需要从该列获取所有值?

Sample if @post1 contains 1 then select values from that columns that are equals to 1. But if @post1 contains 0 than select all values from that column. And that for all other parameters.

如果@ post1包含1,则为样本,然后从该列中选择等于1的值。但是,如果@ post1包含0,则选择该列中的所有值。对于所有其他参数。

ALTER PROCEDURE [dbo].[spStavke] 
    @dat1 date,
    @dat2 date,
    @god int, 
    @post1 int,
    @post2 int,
    @post3 int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT 
       [test1]
      ,[test2]
      ,[test3]
      ,[test3]
    FROM 
      [PN].[dbo].[Stavke] AS stavke
    LEFT JOIN 
      PN.dbo.Tip AS tip ON stavke.Vrsta = tip.id
    LEFT JOIN
      PN.dbo.Vrsta AS vrs ON stavke.Jedinica = vrs.id
END

1 个解决方案

#1


2  

SELECT 
      [test1]
     ,[test2]
     ,[test3]
     ,[test3]

FROM [PN].[dbo].[Stavke] as  stavke
left join PN.dbo.Tip  as tip on  stavke.Vrsta=tip.id
left join PN.dbo.Vrsta  as vrs on  stavke.Jedinica = vrs.id
WHERE (@Post1 = 0 OR (@Post1 = 1 AND 1 IN( TEST1,TEST2,TEST3))
 AND (@Post2 = 0 OR (@Post2 = 1 AND 1 IN( TEST1,TEST2,TEST3))

#1


2  

SELECT 
      [test1]
     ,[test2]
     ,[test3]
     ,[test3]

FROM [PN].[dbo].[Stavke] as  stavke
left join PN.dbo.Tip  as tip on  stavke.Vrsta=tip.id
left join PN.dbo.Vrsta  as vrs on  stavke.Jedinica = vrs.id
WHERE (@Post1 = 0 OR (@Post1 = 1 AND 1 IN( TEST1,TEST2,TEST3))
 AND (@Post2 = 0 OR (@Post2 = 1 AND 1 IN( TEST1,TEST2,TEST3))