CREATE PROCEDURE pro_sample @A varchar(8000), @B int, @C datetime AS declare @where varchar(8000) declare @sql varchar(8000) set @sql='select * from table where 1=1' 这里不知道该如何写了,大体意思是如果@A不空,where 1=1 and a like '@A' 如果@B不空,where 1=1 and b=@B
就是根据不同的条件连接查询条件 最后再 @sql =@sql+@where+' order by a,b,c' exec(@sql) GO
CREATEPROCEDURE pro_sample
@Avarchar(8000),
@Bint,
@CdatetimeASbeginselect*fromtablewhere1=1and (@Aisnullor a like@A+'%')
and (@Bisnullor b =@B)
and (@Cisnullor c =@C)
orderby a,b,c
endGO
or
SQL code
CREATEPROCEDURE pro_sample
@Avarchar(8000),
@Bint,
@CdatetimeASbegindeclare@wherevarchar(8000)
declare@sqlvarchar(8000)
set@sql='select * from table where 1=1'set@where=''set@where=@where+isnull(' and a like '''+@A+'%''','')
set@where=@where+isnull(' and b = '+cast(@Basvarchar),'')
set@where=@where+isnull(' and c = '''+convert(varchar,@C,120)+'''','')
set@sql=@sql+@where+' order by a,b,c'exec(@sql)
endGO
CREATEPROCEDURE pro_sample
@Avarchar(8000),
@Bint,
@CdatetimeASbegindeclare@wherevarchar(8000)
declare@sqlvarchar(8000)
set@sql='select * from table where 1=1 'set@where=''set@where=@where+isnull(' and a like '''+@A+'%''','')+isnull(' and b = '+cast(@Basvarchar),'')+isnull(' and c = '''+convert(varchar,@C,120)+'''','')+' order by a,b,c'set@sql=@sql+@whereexec(@sql)
end