存储过程传递过滤条件SQLcreate  procedure  [dbo].[testquerysql]
@rdsdate date ,
@glsql char(100)
as
select ID,型号,规格,数量 from tableA 
where 日期=@rdsdate  & @glsql  order by id这样会报错,怎么添加这个参数呢?exec testquerysql '2013-05-01','and 1=1'

解决方案 »

  1.   

    动态SQL实现,create  procedure [dbo].[testquerysql]
    (@rdsdate date,
     @glsql char(100))
    as
    begin
     declare @tsql varchar(6000) select @tsql='select ID,型号,规格,数量 '
                 +' from tableA '
                 +' where 日期='''+cast(@rdsdate as varchar)+''' '
                 +' '+ @glsql + ' order by id ' exec(@tsql)
    end
      

  2.   

    create  procedure  [dbo].[testquerysql]
    @rdsdate date ,
    @glsql char(100)
    asdelcare @sql nvarchar(max)set @sql =' select ID,型号,规格,数量 from tableA where 日期= '+ cast (@rdsdate  as varchar(10)) +  @glsql +'  order by id'exec(@sql)return GO-- test
    exec testquerysql '2013-05-01','and 1=1' 
      

  3.   

    这样测试了一下,在显示条件的时候会符号会问题
    exec testquerysql '2013-05-01','and 1=1 and 编码='R5''  
      

  4.   

    try this,  exec testquerysql '2013-05-01','and 1=1 and 编码=''R5'' '