执行的时候我给的数据是@cType=中文名  @word1=李
ALTER PROCEDURE dbo.sp_tChemSearch
(@cType nvarchar(20)='',
 @word1 nvarchar(20)='',
 @word2 nvarchar(20)='',
 @word3 nvarchar(20)=''
 --@count int output
)
 
AS
-- 创建临时表,存储每次检索返回的结果
Declare @tempTable Table
(     RowNumber int primary key  not null identity(1,1),
  cID int ,
  cCnName nvarchar(max),
  cEnName nvarchar(max),
  cFrm nvarchar(max),
  cCAS nvarchar(max),
  cUN nvarchar(max),
  cDangerNumber nvarchar(max)
)

If(@word2 != '') --关键字有值
If(@cType != '') -- 有值
Insert Into @tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber)
Select cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号
From tChem
Where @cType Like '%' + @word2 + '%'

select * from @tempTable

解决方案 »

  1.   

    ALTER   PROCEDURE   dbo.sp_tChemSearch 
    (@cType   nvarchar(20)='', 
      @word1   nvarchar(20)='', 
      @word2   nvarchar(20)='', 
      @word3   nvarchar(20)='' 
      --@count   int   output 

      
    AS 
    begin
    --   创建临时表,存储每次检索返回的结果 
    create table  #tempTable    
    (           RowNumber   int   primary   key     not   null   identity(1,1), 
        cID   int   , 
        cCnName   nvarchar(max), 
        cEnName   nvarchar(max), 
        cFrm   nvarchar(max), 
        cCAS   nvarchar(max), 
        cUN   nvarchar(max), 
        cDangerNumber   nvarchar(max) 
    ) If(@word2   !=   '')   --关键字有值 
    If(@cType   !=   '')   --   有值 
    exec('Insert   Into   #tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
    Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
    From   tChem 
    Where '+@cType+' Like   ''%'+@word2+'''%' )select   *   from   #tempTable
    end
      

  2.   

    ALTER   PROCEDURE   dbo.sp_tChemSearch 
    (@cType   nvarchar(20)='', 
      @word1   nvarchar(20)='', 
      @word2   nvarchar(20)='', 
      @word3   nvarchar(20)='' 
      --@count   int   output 

      
    AS 
    begin
    --   创建临时表,存储每次检索返回的结果 
    create table  #tempTable    
    (           RowNumber   int   primary   key     not   null   identity(1,1), 
        cID   int   , 
        cCnName   nvarchar(max), 
        cEnName   nvarchar(max), 
        cFrm   nvarchar(max), 
        cCAS   nvarchar(max), 
        cUN   nvarchar(max), 
        cDangerNumber   nvarchar(max) 
    ) If(@word2   !=   '')  and (@cType   !=   '')
    begin
     exec('Insert   Into   #tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
    Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
    From   tChem 
    Where '+@cType+' Like   ''%'+@word2+'''%' )
    endselect   *   from   #tempTable
    end
      

  3.   

    执行的时候我给的数据是@cType='中文名'     @word1='李' ALTER   PROCEDURE   dbo.sp_tChemSearch 
    (@cType   nvarchar(20)='', 
      @word1   nvarchar(20)='', 
      @word2   nvarchar(20)='', 
      @word3   nvarchar(20)='' 
      --@count   int   output 

      
    AS 
    --   创建临时表,存储每次检索返回的结果 
    create table #tempTable 
    (           RowNumber   int   primary   key     not   null   identity(1,1), 
        cID   int   , 
        cCnName   nvarchar(max), 
        cEnName   nvarchar(max), 
        cFrm   nvarchar(max), 
        cCAS   nvarchar(max), 
        cUN   nvarchar(max), 
        cDangerNumber   nvarchar(max) 
    ) If(@word1   !=   '')   --关键字有值 
    If(@cType   !=   '')   --   有值 
    exec('
    Insert   Into   #tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
    Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
    From   tChem 
    Where   '+@cType+' Like   ''%'   +   @word1   +   '%''
    ')
     
    If(@word2   !=   '')   --关键字有值 
    If(@cType   !=   '')   --   有值 
    exec('
    Insert   Into   #tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
    Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
    From   tChem 
    Where   '+@cType+' Like   ''%'   +   @word2   +   '%''
    ')If(@word3   !=   '')   --关键字有值 
    If(@cType   !=   '')   --   有值 
    exec('
    Insert   Into   #tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
    Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
    From   tChem 
    Where   '+@cType+' Like   ''%'   +   @word3   +   '%''
    ')
    select   *   from   #tempTable
      

  4.   

    '%' 附近有语法错误。
    顺便说一下,我是SQL2005
      

  5.   

    ALTER   PROCEDURE   dbo.sp_tChemSearch 
    (@cType   nvarchar(20)='', 
      @word1   nvarchar(20)='', 
      @word2   nvarchar(20)='', 
      @word3   nvarchar(20)='' 
      --@count   int   output 

      
    AS 
    begin
    --   创建临时表,存储每次检索返回的结果 
    create table  #tempTable    
    (           RowNumber   int   primary   key     not   null   identity(1,1), 
        cID   int   , 
        cCnName   nvarchar(max), 
        cEnName   nvarchar(max), 
        cFrm   nvarchar(max), 
        cCAS   nvarchar(max), 
        cUN   nvarchar(max), 
        cDangerNumber   nvarchar(max) 
    ) set @word2 = isnull(@word2,'')
    if len(@word2)=0
       set @word2 = '%'
    else
       set @word2 = '%'+@word2+'%'
     
    If(@cType   !=   '')   --   有值 
    exec('Insert   Into   #tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
    Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
    From   tChem 
    Where '+@cType+' Like   '''+@word2+'''' )select   *   from   #tempTable
    end
      

  6.   

    ALTER   PROCEDURE   dbo.sp_tChemSearch 
    (@cType   nvarchar(20)='', 
      @word1   nvarchar(20)='', 
      @word2   nvarchar(20)='', 
      @word3   nvarchar(20)='' 
      --@count   int   output 

      
    AS 
    begin
    --   创建临时表,存储每次检索返回的结果 
    create table  #tempTable    
    (           RowNumber   int   primary   key     not   null   identity(1,1), 
        cID   int   , 
        cCnName   nvarchar(max), 
        cEnName   nvarchar(max), 
        cFrm   nvarchar(max), 
        cCAS   nvarchar(max), 
        cUN   nvarchar(max), 
        cDangerNumber   nvarchar(max) 
    ) If(@word2   !=   '')  and (@cType   !=   '')
    begin
     exec('Insert   Into   #tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
    Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
    From   tChem 
    Where '+@cType+' Like   ''%'+@word2+'%''' )
    endselect   *   from   #tempTable
    end
      

  7.   

    ALTER   PROCEDURE   dbo.sp_tChemSearch 
    (@cType   nvarchar(20)='', 
      @word1   nvarchar(20)='', 
      @word2   nvarchar(20)='', 
      @word3   nvarchar(20)='' 
      --@count   int   output 

      
    AS 
    begin
    If(@word2   !=   '' and @cType <> '')   --关键字有值 --   创建临时表,存储每次检索返回的结果 
    declare @sql varchar(8000)
    set @sql = '
    Declare   @tempTable   Table 
    (           RowNumber   int   primary   key     not   null   identity(1,1), 
        cID   int   , 
        cCnName   nvarchar(max), 
        cEnName   nvarchar(max), 
        cFrm   nvarchar(max), 
        cCAS   nvarchar(max), 
        cUN   nvarchar(max), 
        cDangerNumber   nvarchar(max) 
    ) Insert   Into   @tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
    Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
    From   tChem 
    Where ' +  @cType + '  Like   ''%' +  @word2   +   '%''
    select   *   from   @tempTable

    exec(@sql)end
    go
      

  8.   


    ALTER   PROCEDURE   dbo.sp_tChemSearch 
    (@cType   nvarchar(20)='', 
      @word1   nvarchar(20)='', 
      @word2   nvarchar(20)='', 
      @word3   nvarchar(20)='' 
      --@count   int   output 

      
    AS 
    --   创建临时表,存储每次检索返回的结果 
    Declare   @tempTable   Table 
    (           RowNumber   int   primary   key     not   null   identity(1,1), 
        cID   int   , 
        cCnName   nvarchar(max), 
        cEnName   nvarchar(max), 
        cFrm   nvarchar(max), 
        cCAS   nvarchar(max), 
        cUN   nvarchar(max), 
        cDangerNumber   nvarchar(max) 
    ) If(@word2   !=   '')   --关键字有值 
    begin
      If(@cType   !=   '')   --   有值 
        exec('Insert   Into   @tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
        Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
         From   tChem 
        Where  '+ @cType +'  Like   ''%'''   +   @word2   +   '''%''') 
    end
    select   *   from   @tempTable
      

  9.   


    --如果用临时表的话,每次只create而没有drop的话,会出问题吧--还是用表变量
    ALTER   PROCEDURE   dbo.sp_tChemSearch 
    (@cType   nvarchar(20)='', 
      @word1   nvarchar(20)='', 
      @word2   nvarchar(20)='', 
      @word3   nvarchar(20)='' 
      --@count   int   output 

      
    AS 
    begin
    If(@word2   !=   '' and @cType <> '')   --关键字有值 --   创建临时表,存储每次检索返回的结果 
    declare @sql varchar(8000)
    set @sql = '
    Declare   @tempTable   Table 
    (           RowNumber   int   primary   key     not   null   identity(1,1), 
        cID   int   , 
        cCnName   nvarchar(max), 
        cEnName   nvarchar(max), 
        cFrm   nvarchar(max), 
        cCAS   nvarchar(max), 
        cUN   nvarchar(max), 
        cDangerNumber   nvarchar(max) 
    ) Insert   Into   @tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
    Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
    From   tChem 
    Where ' +  @cType + '  Like   ''%' +  @word2   +   '%''
    select   *   from   @tempTable

    exec(@sql)end
    go
      

  10.   

    ALTER   PROCEDURE   dbo.sp_tChemSearch 
    (@cType   nvarchar(20)='', 
      @word1   nvarchar(20)='', 
      @word2   nvarchar(20)='', 
      @word3   nvarchar(20)='' 
      --@count   int   output 

      
    AS 
    begin
    --   创建临时表,存储每次检索返回的结果 
    create table  #tempTable    
    (           RowNumber   int   primary   key     not   null   identity(1,1), 
        cID   int   , 
        cCnName   nvarchar(max), 
        cEnName   nvarchar(max), 
        cFrm   nvarchar(max), 
        cCAS   nvarchar(max), 
        cUN   nvarchar(max), 
        cDangerNumber   nvarchar(max) 
    ) If(@word2   !=   '')  and (@cType   !=   '')
    begin
     exec('Insert   Into   #tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
    Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
    From   tChem 
    Where '+@cType+' Like   ''%'+@word2+'%''' )
    endselect   *   from   #tempTable
    end
      

  11.   

    Declare       @tempTable       Table   和create table #tempTable 有什么区别????
      

  12.   

    --如果用临时表的话,每次只create而没有drop的话,会出问题吧coolingpipe 冷箫轻笛
    -----------------------------
    不会出问题  在存储过程内建的临时表 他的会话就在 这个存储过程内 
    当存储过程执行完毕后 自动删除掉。如果多个存储过程同时运行的话也不会互相干涉
      

  13.   

    ALTER   PROCEDURE   dbo.sp_tChemSearch 
    (@cType   nvarchar(20)='', 
      @word1   nvarchar(20)='', 
      @word2   nvarchar(20)='', 
      @word3   nvarchar(20)='' 
      --@count   int   output 

      
    AS 
    --   创建临时表,存储每次检索返回的结果 
    Declare   @tempTable   Table 
    (           RowNumber   int   primary   key     not   null   identity(1,1), 
        cID   int   , 
        cCnName   nvarchar(max), 
        cEnName   nvarchar(max), 
        cFrm   nvarchar(max), 
        cCAS   nvarchar(max), 
        cUN   nvarchar(max), 
        cDangerNumber   nvarchar(max) 
    ) If(@word2 <> '')  and (@cType <> '')   --   有值 
    Insert   Into   @tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
    Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
    From   tChem 
    Where   @cType   Like   '%'   +   @word2   +   '%' select   *   from   @tempTable
      

  14.   


    ALTER   PROCEDURE   dbo.sp_tChemSearch 
    (@cType   nvarchar(20)='', 
      @word1   nvarchar(20)='', 
      @word2   nvarchar(20)='', 
      @word3   nvarchar(20)='' 
      --@count   int   output 

      
    AS 
    begin--参数是否有值
    If(@word2 = '' or @cType = '')
        return
    --列名是否有效
    if not exists(select 1 from syscolumns where object_name(id) = 'tChem' and name = @cType)
        return--   创建临时表,存储每次检索返回的结果 
    declare @sql varchar(8000)
    set @sql = '
    Declare   @tempTable   Table 
    (           RowNumber   int   primary   key     not   null   identity(1,1), 
        cID   int   , 
        cCnName   nvarchar(max), 
        cEnName   nvarchar(max), 
        cFrm   nvarchar(max), 
        cCAS   nvarchar(max), 
        cUN   nvarchar(max), 
        cDangerNumber   nvarchar(max) 
    ) Insert   Into   @tempTable(cID,cCnName,cEnName,cFrm,cCAS,cUN,cDangerNumber) 
    Select   cID,中文名,英文名,分子式,CAS号,UN编号,危险货物编号 
    From   tChem 
    Where ' +  @cType + '  Like   ''%' +  @word2   +   '%''
    select   *   from   @tempTable

    exec(@sql)end
    go
      

  15.   

    --如果用临时表的话,每次只create而没有drop的话,会出问题吧 coolingpipe   冷箫轻笛 
    ----------------------------- 
    不会出问题 在存储过程内建的临时表   他的会话就在   这个存储过程内   
    当存储过程执行完毕后 自动删除掉。如果多个存储过程同时运行的话也不会互相干涉
    =====================================================================高!
    学习了
      

  16.   

    Declare @tempTable Table       
    和 
    create   table   #tempTable   有什么区别????
    =====================================
    前者是表变量,就是跟declare @i int 这样一个int型的变量一个概念,只是他是一个表后者是临时表,创建以后会存储在系统的tempdb中表变量是在内存中的,所以如果你的数据量不算太大的话,用表变量肯定比临时表效率好一些
    但是如果数据量相对于内存来讲有点大的话,就应该选择用临时表表变量跟普通的变量一样,使用完以后可以自动释放的
    但是临时表不会不过刚才小虫已经说过了,在存储过程中的临时表也是会自动释放的