一个搜索的存储过程
1.在C#中如果对参数赋值为null,总会提示"存储过程XXX需要参数XXX,但未能提供"之类的信息,不能赋空参数,不得不把原来的if XXX is null改为if XXX=''.
2.存储过程运行时生成语句必须使用存储过程sp_executesql,使用方法可见代码.
3.直接在存储过程里使用模糊查询无法实现,也就是说在里面使用and XXX like %@XXX%会报错,如果有高人知道解决办法麻烦告诉我,只能在参数里赋类似%XXX%的参数.
4.存储过程中的参数如果有bit值的,在使用c#赋值时,应赋int值.
2.存储过程运行时生成语句必须使用存储过程sp_executesql,使用方法可见代码.
3.直接在存储过程里使用模糊查询无法实现,也就是说在里面使用and XXX like %@XXX%会报错,如果有高人知道解决办法麻烦告诉我,只能在参数里赋类似%XXX%的参数.
4.存储过程中的参数如果有bit值的,在使用c#赋值时,应赋int值.
create procedure UP_lvSalClient_GetList
(
@ClientID bigint,@Saler nvarchar(50),@Name nvarchar(50),@Area nvarchar(50),
@City nvarchar(50),@Company nvarchar(100),@Sort1 nvarchar(50),@Sort2 nvarchar(50),
@Sort3 nvarchar(50),@Brand nvarchar(50),@Address nvarchar(50),@Source nvarchar(50),
@LikeMe nvarchar(50),@valid bit
)
AS
declare @SQLStr nvarchar(200)
set @SQLStr=N'select * from lvSalClient where valid=@valid'
if @ClientID<>0
set @SQLStr=@SQLStr+N' and ClientID=@ClientID'
if @Saler<>''
set @SQLStr=@SQLStr+N' and Saler=@Saler'
if @Name<>''
set @SQLStr=@SQLStr+N' and name like @Name'
if @Area<>''
set @SQLStr=@SQLStr+N' and Area=@Area'
if @City<>''
set @SQLStr=@SQLStr+N' and City like @City'
if @Company<>''
set @SQLStr=@SQLStr+N' and Company like @company'
if @Sort1<>''
set @SQLStr=@SQLStr+N' and Sort1=@Sort1'
if @Sort2<>''
set @SQLStr=@SQLStr+N' and Sort2=@Sort2'
if @Sort3<>''
set @SQLStr=@SQLStr+N' and Sort3=@Sort3'
if @Brand<>''
set @SQLStr=@SQLStr+N' and Brand=@Brand'
if @Address<>''
set @SQLStr=@SQLStr+N' and Address like @address'
if @source<>''
set @SQLStr=@SQLStr+N' and source like @source'
if @likeme<>''
set @SQLStr=@SQLStr+N' and likeme like @likeme'
exec sp_executesql @SQLStr,
N'@ClientID bigint,@Saler nvarchar(50),@Name nvarchar(50),@Area nvarchar(50),
@City nvarchar(50),@Company nvarchar(100),@Sort1 nvarchar(50),@Sort2 nvarchar(50),
@Sort3 nvarchar(50),@Brand nvarchar(50),@Address nvarchar(50),@Source nvarchar(50),
@LikeMe nvarchar(50),@valid bit',
@ClientID,@Saler,@Name,@Area,
@City,@Company,@Sort1,@Sort2,
@Sort3,@Brand,@Address,@Source,
@LikeMe,@valid
(
@ClientID bigint,@Saler nvarchar(50),@Name nvarchar(50),@Area nvarchar(50),
@City nvarchar(50),@Company nvarchar(100),@Sort1 nvarchar(50),@Sort2 nvarchar(50),
@Sort3 nvarchar(50),@Brand nvarchar(50),@Address nvarchar(50),@Source nvarchar(50),
@LikeMe nvarchar(50),@valid bit
)
AS
declare @SQLStr nvarchar(200)
set @SQLStr=N'select * from lvSalClient where valid=@valid'
if @ClientID<>0
set @SQLStr=@SQLStr+N' and ClientID=@ClientID'
if @Saler<>''
set @SQLStr=@SQLStr+N' and Saler=@Saler'
if @Name<>''
set @SQLStr=@SQLStr+N' and name like @Name'
if @Area<>''
set @SQLStr=@SQLStr+N' and Area=@Area'
if @City<>''
set @SQLStr=@SQLStr+N' and City like @City'
if @Company<>''
set @SQLStr=@SQLStr+N' and Company like @company'
if @Sort1<>''
set @SQLStr=@SQLStr+N' and Sort1=@Sort1'
if @Sort2<>''
set @SQLStr=@SQLStr+N' and Sort2=@Sort2'
if @Sort3<>''
set @SQLStr=@SQLStr+N' and Sort3=@Sort3'
if @Brand<>''
set @SQLStr=@SQLStr+N' and Brand=@Brand'
if @Address<>''
set @SQLStr=@SQLStr+N' and Address like @address'
if @source<>''
set @SQLStr=@SQLStr+N' and source like @source'
if @likeme<>''
set @SQLStr=@SQLStr+N' and likeme like @likeme'
exec sp_executesql @SQLStr,
N'@ClientID bigint,@Saler nvarchar(50),@Name nvarchar(50),@Area nvarchar(50),
@City nvarchar(50),@Company nvarchar(100),@Sort1 nvarchar(50),@Sort2 nvarchar(50),
@Sort3 nvarchar(50),@Brand nvarchar(50),@Address nvarchar(50),@Source nvarchar(50),
@LikeMe nvarchar(50),@valid bit',
@ClientID,@Saler,@Name,@Area,
@City,@Company,@Sort1,@Sort2,
@Sort3,@Brand,@Address,@Source,
@LikeMe,@valid