Declare @ID  int,@SQL varchar(2000)
Set @ID ='78330'
Select 单号 From table Where id =@IDSelect @SQL = '
Select * From table1 Where 内容 like '''(Select 单号 From table Where id =@IDnpID)''''  --当然这句肯定会错,就是求这句怎么写
Print @SQL
Exec(@SQL)像这样的LIKE语句怎么写,求解

解决方案 »

  1.   

    单号唯一?
    要是保证唯一Declare @ID  int,@SQL varchar(2000)
    Set @ID ='78330'
    declare @dh varchar(10)Select top 1 @dh = 单号 From table Where id =@IDSelect @SQL = '
    Select * From table1 Where 内容 like ''%'+@dh +'%'''  --当然这句肯定会错,就是求这句怎么写
    Print @SQL
    Exec(@SQL)
      

  2.   


    Declare @ID  int,@SQL varchar(2000)
    Set @ID ='78330'
    Select 单号 From table Where id =@IDSelect @SQL = 'Select * From table1 Where 内容 like ''%'+(Select top 1 单号 From table Where id =@IDnpID)+'%'''  --当然这句肯定会错,就是求这句怎么写
    Print @SQL
    Exec(@SQL)
      

  3.   

    Declare @ID  int,@SQL varchar(2000)
    Set @ID ='78330'
    ----------------
    错了,@id是int型,不能赋字符串呀。
      

  4.   

    看看下面代码:
    好象like后面变量才行。只接放语句我也没成功use northwind
    Declare @ID  int,@SQL varchar(2000)
    declare @myid varchar(5)
    Set @id =10248
    Select top 1 @myid=customerid From orders Where orderid =@IDSelect @SQL = 'Select * From customers Where customerid like ''%'+ @myid + '%'''
    Print @SQL
    Exec(@SQL)
      

  5.   


    select t1.* from table1 t1 inner join table t on charindex(t.单号,t1.内容)>0 where t.id=@IDnpId
      

  6.   

    @id 是int类型吧?
    Set @ID =78330