CREATE PROCEDURE tt 
@a varchar(100),
@b varchar(100)
 AS
declare @c varchar(100),@d varchar(100)
select @c ='select * from tensile where '
if @a != ''
select @c=@c + 'tensile_unit = ' +@a
exec @c
if @b !=''
select * from tensile
GO
exec tt @a ='质检中心',@b=''服务器: 消息 2812,级别 16,状态 62,行 9
未能找到存储过程 'select * from tensile where tensile_unit = 质检中心'。
服务器: 消息 2812,级别 16,状态 62,行 13
未能找到存储过程 'select * from tensile where tensile_unit = 质检中心'。

解决方案 »

  1.   

    select @c ='select * from tensile where ' 
    if @a != '' 
    select @c=@c + 'tensile_unit = ' +@a 
    exec @c 这样构造??如果@a=''的时候..
    那@c='select * from tensile where'
    再用exec 执行就出错.exec (@c) 
      

  2.   

    CREATE PROCEDURE tt @a varchar(100), @b varchar(100) 
    AS
    begin 
      declare @c as varchar(100)
      declare @d as varchar(100) 
      select @c ='select * from tensile where ' 
      if @a <> ''
         begin 
           select @c=@c + 'tensile_unit = ' +@a 
           exec @c
         end 
      if @b <> '' 
         select * from tensile 
    end
    GO exec tt @a ='质检中心',@b='' 
      

  3.   

    select @c=@c + 'tensile_unit = ''' +@a + ''''
      

  4.   

    select @c=@c + 'tensile_unit = ''' +@a + ''''
      

  5.   

    --上面为何只发了一半?
    CREATE PROCEDURE tt @a varchar(100), @b varchar(100) 
    AS
    begin 
      declare @c as varchar(100)
      declare @d as varchar(100) 
      select @c ='select * from tensile where ' 
      if @a <> ''
         begin 
           select @c=@c + 'tensile_unit = ' +@a 
           exec @c
         end 
      if @b <> '' 
         select * from tensile 
    end
    GO exec tt @a ='质检中心',@b='' 
      

  6.   

    select @c=@c + 'tensile_unit = ''' +@a + ''''
      

  7.   


    select @c=@c + 'tensile_unit = ''' +@a + '''' 
    exec (@c) --> 括号
      

  8.   

    select @c=@c + 'tensile_unit = ''' +@a + '''' 在SQL中传入字符串时要注意要'''呀!
      

  9.   

    动态SQL 一般要注意'''' 楼主好好检查一下咯
      

  10.   

    o ,my god !谢谢了,搞定了!
      

  11.   

    CREATE PROCEDURE tt @a varchar(100), @b varchar(100) 
    AS
    begin 
      declare @c as varchar(100)
      declare @d as varchar(100) 
      select @c ='select * from customer where ' 
      if @a <> ''
         begin 
           select  @c=@c + 'id = ' +@a 
           print @c
           exec (@c)
         end 
      if @b <> '' 
         select * from tensile 
    end
    GO  ID          CustomerName                                       Address                                            Diector                                            Phone                re                                                                                                                                                                                                                                                           
    ----------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
    1434        三阳                                                 待输入!                                               待输入!                                               待输入!                 待输入!(所影响的行数为 1 行)  
      

  12.   

    @c放的是SQL语句应该使用exec(系统)执行
    exec(@c)