34,589
社区成员
发帖
与我相关
我的任务
分享
NORNO
1
1
1
1
0
0
0
0
1
1
0
0
0
0
0
1
得到结果111100001100001(结果为VARCHAR类型)
create table tb1(级别 int,NOIS int,工序 varchar(10))
insert into tb1 select 1,1,'demo1'
insert into tb1 select 2,1,'demo1'
insert into tb1 select 2,1,'demo1'
insert into tb1 select 2,0,'demo1'
insert into tb1 select 2,0,'demo1'
insert into tb1 select 2,1,'demo1'
insert into tb1 select 2,1,'demo1'
insert into tb1 select 2,1,'demo1'
insert into tb1 select 2,1,'demo1'
insert into tb1 select 2,0,'demo2'
insert into tb1 select 2,0,'demo2'
insert into tb1 select 2,0,'demo2'
insert into tb1 select 2,1,'demo2'
insert into tb1 select 2,1,'demo2'
insert into tb1 select 2,1,'demo2'
insert into tb1 select 2,1,'demo2'
insert into tb1 select 1,1,'demo2'
insert into tb1 select 2,1,'demo2'
insert into tb1 select 2,1,'demo2'
insert into tb1 select 2,1,'demo2'
create table tb2(工序 varchar(10),NOIS varchar(20))
insert into tb2 select 'demo1',''
insert into tb2 select 'demo2',''
go
update t
set NOIS=(select ''+ltrim(NOIS) from tb1 where 工序=t.工序 and 级别=2 for xml path(''))
from tb2 t
select * from tb2
/*
工序 NOIS
---------- --------------------
demo1 11001111
demo2 0001111111
(2 行受影响)
*/
go
drop table tb1,tb2
declare @c varchar(30)
select @c=isnull(@c,'')+ltrim(norno) from tb
select @c
----------------------------------------------------
/*如何将一列中所有的值一行显示
数据源
a
b
c
d
e
结果
a,b,c,d,e
*/
create table tb(col varchar(20))
insert tb values ('a')
insert tb values ('b')
insert tb values ('c')
insert tb values ('d')
insert tb values ('e')
go
--方法一
declare @sql varchar(1000)
set @sql = ''
select @sql = @sql + t.col + ',' from (select col from tb) as t
set @sql='select result = ''' + left(@sql , len(@sql) - 1) + ''''
exec(@sql)
/*
result
----------
a,b,c,d,e,
*/
--方法二
declare @output varchar(8000)
select @output = coalesce(@output + ',' , '') + col from tb
print @output
/*
a,b,c,d,e
*/
---方法三
declare @s varchar(1000)
select @s=isnull(@s+',' , '')+col from tb
select @s
/*
a,b,c,d,e
*/
drop table tb
-------------
declare @str varchar(8000)
select @str = isnull(@str,'')+ltrim(NORNO
) from tb
select @str
create table tb(NORNO int)
insert into tb select 1
insert into tb select 1
insert into tb select 1
insert into tb select 1
insert into tb select 0
insert into tb select 0
insert into tb select 0
insert into tb select 0
insert into tb select 1
insert into tb select 1
insert into tb select 0
insert into tb select 0
insert into tb select 0
insert into tb select 0
insert into tb select 0
insert into tb select 1
go
declare @c varchar(30)
select @c=isnull(@c,'')+ltrim(norno) from tb
select @c
/*
------------------------------
1111000011000001
(1 行受影响)
*/
go
drop table tb