解决方案 »

  1.   

    这样?if OBJECT_ID('temp_cs')is not null
    drop table temp_cs
    go
    create table temp_cs(
    姓名 varchar(8),
    备注 varchar(50),
    )
     
    insert temp_cs
    select '赵','【赵】测试' union all
    select '钱','【孙】测试' union all
    select '孙','【赵】测试' union all
    select '李',''union all
    select '黄','【黄】测试'
    GO--declare @Name varchar(50)
    --set @Name ='赵'
    select * from temp_cs
    where  备注 like '%【'+姓名+'】%'/*
    姓名       备注
    -------- --------------------------------------------------
    赵        【赵】测试
    黄        【黄】测试
    */
      

  2.   

    那只能用你之前的方法,因为单纯把姓名替换的话,满足不了姓名=@name的要求
      

  3.   

    我之前只能手动赋值一个,然后查询,怎么实现批量赋值?
    网上查说是可以这样,但实际用会报错。
    select 姓名 into @Name from temp_cs
      

  4.   


    我的想法这样,以测试数据为例:
    先用
    select distinct(姓名)   as 姓名 from temp_cs
    得到姓名记录集,现在我就不知道怎么把这个记录集的【姓名】这个字段值挨个赋值给@Name,相当于
    declare @Name varchar(50)
    set @Name =【姓名】1
    执行查询
    set @Name =【姓名】2
    执行查询
    ...
    然后挨个把查询结果union all在一起……纯探讨哈,具体到本例这样做肯定是大炮打蚊子。
      

  5.   

    你想这样?if OBJECT_ID('temp_cs')is not null
    drop table temp_cs
    go
    create table temp_cs(
    姓名 varchar(8),
    备注 varchar(50),
    )
     
    insert temp_cs
    select '赵','【赵】测试' union all
    select '钱','【孙】测试' union all
    select '孙','【赵】测试' union all
    select '李',''union all
    select '黄','【黄】测试'
    GO--declare @Name varchar(50)
    --set @Name ='赵'
    SELECT  a.*
    FROM    temp_cs a
            INNER  JOIN ( SELECT DISTINCT
                                ( 姓名 ) AS 姓名
                        FROM    temp_cs
                      ) b ON a.备注 LIKE '%【' + b.姓名 + '】%' AND a.姓名=b.姓名/*
    姓名       备注
    -------- --------------------------------------------------
    黄        【黄】测试
    赵        【赵】测试*/