在论坛找到一帖http://bbs.csdn.net/topics/320049089
6楼回复有几处不解代码如下:
IF(ISNULL(@par1,'')<>'')
SET @S1=@S1+' AND col1 LIKE '''+@par1+'%'''
IF(ISNULL(@par2,0)<>0) --当然:定义的参数如果是整型的话,就用 0
SET @S1=@S1+' AND col2 ='''+@par2+''''
按微软解释ISNULL返回类型是数据类型,这可以放在if的条件里,后面的<>,也没找到资料,下面的SET语句加多加一个'',有什么意义?
我写了一个,给@college参数赋''时执行正常,赋非空时出现语法错误。
create procedure dbo.ab_Student_GetStudents
(
@pageIndex int,
@pageSize int,
@college nvarchar(30)
)
begin
declare @sqlString nvarchar(500)
declare @start int
declare @finish int
set @start=@pageIndex*@pageSize+1
set @finish=(@pageIndex+1)*@pageSize
set @sqlString='select * from
(
select ab_Students.s_id,
ab_Students.s_name,
ab_Students.s_college,
ab_Students.s_grade,
ab_Students.s_field,
ab_Students.s_telphone,
ab_Students.s_mobile,
ab_Students.s_fax,
ROW_NUMBER() OVER (ORDER BY ab_Students.s_grade DESC,ab_Students.s_id) AS RowNum
from ab_Students where 1=1'
if(ISNULL(@college,'')<>'')
set @sqlString=@sqlString+' s_college= '''+@college+''''
set @sqlString=@sqlString+' )Students
where Students.RowNum between @start and @finish'
exec sp_executesql
@sqlString,
N'@start int, @finish int',
@start,
@finish
end
GO
我执行
execute ab_Student_GetStudents 0,6,''能成功但是
执行
execute ab_Student_GetStudents 0,6,'计算机'时提示's_college' 附近有语法错误。
感谢!
组合查询 存储过程 ISNULL
------解决方案--------------------
【请加上连接符,and / or】 s_college= '''+@college+''''
------解决方案--------------------
你拼接完的sql语句是
select *
from(
select ab_Students.s_id, ab_Students.s_name, ab_Students.s_college,
ab_Students.s_grade, ab_Students.s_field, ab_Students.s_telphone,
ab_Students.s_mobile, ab_Students.s_fax,
ROW_NUMBER() OVER (ORDER BY ab_Students.s_grade DESC,ab_Students.s_id) AS RowNum
from ab_Students where 1=1 s_college= '计算机' --此处没有连接关键字 and或者or
)Studentswhere Students.RowNum between @start and @finish
建议lz在存储过程中exec sp_executesql @sqlString,N'@start int, @finish int',@start,@finish执行前加上print @sqlString.查看一下你拼接的sql是什么