下面是我写的一个存储过程演示,可是在
if(@str IS NOT NULL)
select * from student where  + @str
这个地方老报错,请问怎么样解决!
create procedure testquery @name char(10),@age int,@sex char(10),@marry char(10)
as
begin
declare @str varchar(500)
declare @leng int
set @str=''
if(@name IS NOT NULL)
set @str='FName=@name and '
if(@age IS NOT NULL)
set @str=@str+'Age=@age and '
if(@sex IS NOT NULL)
set @str=@str+'Sex=@sex and '
if(@marry IS NOT NULL)
set @str=@str+'IsHunFou=@marry and'
set @leng=len(@str)set @str=substring(@str,0,@leng-3)if(@str IS NOT NULL)
select * from student where  + @str
else
select * from student 
end

解决方案 »

  1.   

    select * from student where  + @str
    这个地方老报错,请问怎么样解决!exec('select * from student where '+@str)
      

  2.   

    declare @str2 varchar(5000)set @str2 ='select * from student where'  + @strexec (@str2)
      

  3.   

    if(@str IS NOT NULL)
    select * from student where  + @str
    这个地方老报错,请问怎么样解决!
    create procedure testquery @name char(10),@age int,@sex char(10),@marry char(10)
    as
    begin  ...
    这位老兄,create 语句必须是批处理语句的第一行,所以应该改为:
    if(@str IS NOT NULL)
    select * from student where  + @str
    go
    create procedure testquery @name char(10),@age int,@sex char(10),@marry char(10)
    as
    begin  ...