测试数据:
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
go查询要求:
凡是【备注】字段 like 【姓名】字段的列出,本例查询结果就是:
我只会单个赋值查询,比如这样能查询出备注 like '%【赵】%'的结果:
declare @Name varchar(50)
set @Name ='赵'
select * from temp_cs
where 姓名=@Name and 备注 like '%【'+@Name+'】%'
具体到本例,就是怎么批量给@Name赋值(每一行)……求教。
------解决方案--------------------
这样?
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 '%【'+姓名+'】%'
/*
姓名 备注
-------- --------------------------------------------------
赵 【赵】测试
黄 【黄】测试
*/------解决方案--------------------
你想这样?
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.姓名
/*
姓名 备注
-------- --------------------------------------------------
黄 【黄】测试
赵 【赵】测试
*/