use mydb
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[ReadOrder]
(
@cs varchar(50) = NULL,
@xm varchar(50) = NULL
)
As
--定义两个变量
declare @sql varchar(500)
declare @both bit
set @sql = 'select * from 订单'
set @both = 0
if @cs is not null or @cs <> ''
begin
/*有一个条件*/
set @sql = @sql + ' where 货主城市 like ''%' + @cs + '%'''
set @both = 1
end
/*第一个条件已成立*/
if @both = 1
begin
if @xm is not null or @xm <> ''
begin
/*设定第二个条件*/
set @sql = @sql + ' And 货主名称 like ''%' + @xm + '%'''
end
end
else
/*第一个条件不成立*/
begin
if @xm is not null or @xm <> ''
begin
set @sql = @sql + ' Where 货主名称 like ''%' + @xm + '%'''
end
end
--select @sql
exec(@sql)
文章来源:http://jarod.z.blog.163.com/blog/static/939936632008101724036975