--> 测试数据: [A] if object_id('[A]') is not null drop table [A] create table [A] (cc int,name varchar(2),count varchar(20)) insert into [A] select 1,'AA',null union all select 2,'BB',null union all select 3,'CC',null union all select 4,'DD',nulldeclare @t varchar(100) select @t=isnull(@t+',','')+name from a update a set [count]=@t select * from [A]
--> 测试数据: [A] if object_id('[A]') is not null drop table [A] create table [A] (cc int,name varchar(2),count varchar(20)) insert into [A] select 1,'AA',null union all select 2,'BB',null union all select 3,'CC',null union all select 4,'DD',nulldeclare @str varchar(100)select @str=isnull(@str+',','')+name from [A]update A set [count]=@strselect * from [A]
update A set count= name
谢谢回复,不对噢~我要查询出来的count的值是 AA,BB,CC,DD,按您现在的这样,count的值是等于name的,如下: cc name count 1 AA AA 2 BB BB 3 CC CC 4 DD DD 这是错的
update a set count=name
if object_id('[A]') is not null drop table [A] create table [A] (cc int,name varchar(2),count varchar(20)) insert into [A] select 1,'AA',null union all select 2,'BB',null union all select 3,'CC',null union all select 4,'DD',nulldeclare @str varchar(100)select @str=isnull(@str+',','')+name from [A]update A set [count]=@str WHERE [COUNT] IS NULLselect * from [A]
declare @strs varchar(500) set @strs='' select @strs = @strs +','+name from A12 print @strsselect A12.cc,A12.name,@strs as count from A12
--> 测试数据: [A]
if object_id('[A]') is not null drop table [A]
create table [A] (cc int,name varchar(2),count varchar(20))
insert into [A]
select 1,'AA',null union all
select 2,'BB',null union all
select 3,'CC',null union all
select 4,'DD',nulldeclare @t varchar(100)
select @t=isnull(@t+',','')+name from a
update a set [count]=@t
select * from [A]
if object_id('[A]') is not null drop table [A]
create table [A] (cc int,name varchar(2),count varchar(20))
insert into [A]
select 1,'AA',null union all
select 2,'BB',null union all
select 3,'CC',null union all
select 4,'DD',nulldeclare @str varchar(100)select @str=isnull(@str+',','')+name from [A]update A set [count]=@strselect * from [A]
update A set count= name
cc name count
1 AA AA
2 BB BB
3 CC CC
4 DD DD
这是错的
create table [A] (cc int,name varchar(2),count varchar(20))
insert into [A]
select 1,'AA',null union all
select 2,'BB',null union all
select 3,'CC',null union all
select 4,'DD',nulldeclare @str varchar(100)select @str=isnull(@str+',','')+name from [A]update A set [count]=@str WHERE [COUNT] IS NULLselect * from [A]
declare @strs varchar(500)
set @strs=''
select @strs = @strs +','+name from A12
print @strsselect A12.cc,A12.name,@strs as count from A12