if else sql语句(选择语句使用param而不使用param)?

时间:2017-10-16 03:03:36

标签: sql sql-server if-statement

CASE 
   WHEN @vessel_certificate_id IS NULL 
      THEN SELECT 
               id, name as file_name,
               SUM(DATALENGTH(data)) / 1048576.0  AS file_size,
               dbo.fn_format_datetime(created_on) as file_date  
           FROM
               vessel_cert_attachment 
           GROUP BY 
               id, name, type, created_by, created_on 
      ELSE 
          SELECT
              id, name as file_name, 
              SUM(DATALENGTH(data)) / 1048576.0  AS file_size ,
              dbo.fn_format_datetime(created_on) as file_date  
          FROM 
              vessel_cert_attachment 
          WHERE 
              vessel_certificate_id = @vessel_certificate_id 
          GROUP BY 
              id, name, type, created_by, created_on

如果param不为null,基于上面的SQL,我希望它执行没有param的select语句,如果有param传递,我希望它执行带where where条件的select语句。

可以这样执行吗?

1 个答案:

答案 0 :(得分:0)

Ubuntu Server子句

中使用OR条件
Where