把数据库中的所有记录以一个特定格式的字符显示出来的一个例子

create table tb_other
(
  f_djhmvarchar(17)not null,--单据号码
  f_tablefield varchar(20)            not null,                       --字段
  f_value      varchar(1024)                                   not null,                       --值
)

insert into tb_other select 'd001' as f_djhm,'f_a' as f_tablefield,'aaaaaaaaaaaa' as f_value union all

select 'd001' as f_djhm,'f_b' as f_tablefield,'aaaabbba' as f_value union all
select 'd001' as f_djhm,'f_c' as f_tablefield,'aacccaaaaa' as f_value union all
select 'd001' as f_djhm,'f_d' as f_tablefield,'addddaaaaaaa' as f_value

select ''''+f_value+''' as '+f_tablefield+','  as f_sql from tb_other 得到的结果为
'aaaaaaaaaaaa' as f_a,
'aaaabbba' as f_b,
'aacccaaaaa' as f_c,
'addddaaaaaaa' as f_d,

我想得到
select 'aaaaaaaaaaaa' as f_a,'aaaabbba' as f_b,'aacccaaaaa' as f_c,'addddaaaaaaa' as f_d

这样的语句,就是说全部拼成一行,前面再加个select,最的后面逗号去掉

 

hellowork(一两清风) ( 两星(中级)) 信誉:100    Blog

 declare @str varchar(8000)
set @str = ''
select @str = @str + ',''' + f_value + ''' as ' + f_tablefield from tb_other
select 'select ' + stuff(@str,1,1,'')

/*结果
select 'aaaaaaaaaaaa' as f_a,'aaaabbba' as f_b,'aacccaaaaa' as f_c,'addddaaaaaaa' as f_d
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值