CREATE proc sp_yuyue_all
@searchText nvarchar(15),
@searchFiled int,
@mydate int,--1:预约日期;0:咨询日期 初始值为:-1
@StartDate datetime,
@EndDate datetime,
@orderType int--1:升序;2:降序 初始值为:2
as
declare @sql nvarchar(1000),@where nvarchar(1000),@order nvarchar(500)
select @sql='select * from Yuyue',@where='',@order=''
if @searchText<>'' and (@searchFiled=0 or @searchFiled=1)
begin
if(@searchFiled=0)
begin
if @where<>'' set @where=@where+' and Number like ''%'+@searchText+'%'''
else set @where=@where+' Number like ''%'+@searchText+'%'''
end
else
begin
if @where<>'' set @where=@where+' and trueName like ''%'+@searchText+'%'''
else set @where=@where+' trueName like ''%'+@searchText+'%'''
endendif @mydate<>2
begin
if(@mydate=0)
beginif @where<>'' and (@StartDate<>'' and @EndDate<>'') set @where=@where+'and Zxdate>='''+convert(varchar(100),@StartDate)+''' and Zxdate<='''+convert(varchar(100),@EndDate)+''''
else if @StartDate<>'' and @EndDate<>''set @where=@where+'Zxdate>='''+convert(varchar(100),@StartDate)+''' and Zxdate<='''+convert(varchar(100),@EndDate)+''''else if @StartDate<>'' and @EndDate='' set @where=@where+'Zxdate='''+convert(varchar(100),@StartDate)+''''
else if @where<>'' and (@StartDate<>'' and @EndDate='') set @where=@where+'and Zxdate='''+convert(varchar(100),@StartDate)+''''else if @StartDate='' and @EndDate<>'' set @where=@where+'Zxdate='''+convert(varchar(100),@EndDate)+''''
else if @where<>'' and (@StartDate='' and @EndDate<>'') set @where=@where+'and Zxdate='''+convert(varchar(100),@EndDate)+''''
end
else
begin
if @where<>'' and (@StartDate<>'' and @EndDate<>'') set @where=@where+'and YuyueTime>='+@StartDate+' and YuyueTime<='+@EndDate+''
else if @StartDate<>'' and @EndDate<>''set @where=@where+'YuyueTime>='+@StartDate+' and YuyueTime<='+@EndDate+''
else if @StartDate<>'' or @EndDate<>'' set @where=@where+'YuyueTime='+@StartDate+' or YuyueTime='+@EndDate+''
else if @where<>'' and (@StartDate<>'' or @EndDate<>'') set @where=@where+'and YuyueTime='+@StartDate+' or YuyueTime='+@EndDate+''
endendif @where<>''
set @where=' where '+@whereif @orderType=1
set @order=' order by id asc'
else
set @order=' order by id desc'exec(@sql+@where+@order)
GO
只是存储过程测试一 :exec sp_yuyue_all'黄萍',1,0,'2011-06-28','2011-06-30',2 这样没错测试二 :exec sp_yuyue_all  null,1,0,'2011-06-28','',2 这样也没错测试三 :exec sp_yuyue_all  null,1,0,'2011-06-28','2011-06-30',2 这样也没错测试四 :exec sp_yuyue_all  '黄萍',1,0,'2011-06-28','',2 这样就报错了 错误提示
第 1 行: 'Zxdate' 附近有语法错误。
仔细检查了 没发现哪里语法有错啊 在查询分析器 里 没提示语法错误