数据库表行列转换

use pubs
go

declare @table_name varchar(35), -- if the table have too much fields ,this proc may be malfunction
    @column_list nvarchar(4000) -- field list, separated with ,
select @table_name = 'jobs'
select @column_list = '' --'job_id, job_desc'

declare @declare_var_section varchar(8000), @init_var_section varchar(8000),
    @gen_select_stmt varchar(8000), @print_stmt varchar(8000), @select_title varchar(8000),
    @print_final varchar(8000), @combine_all varchar(8000), @exec_stmt  varchar(8000)
select
    @declare_var_section = '', @init_var_section = '', @gen_select_stmt = '', @print_stmt = '', @select_title = '',
    @print_final = '', @combine_all = '', @exec_stmt = ''

select
    @declare_var_section = @declare_var_section + ',@' + COLUMN_NAME + ' NVARCHAR(4000)',
    @init_var_section = @init_var_section + ',@' + COLUMN_NAME + '='''''''+COLUMN_NAME +'''''''',
    @gen_select_stmt = @gen_select_stmt + 'SELECT @' + COLUMN_NAME + '=@' + COLUMN_NAME +
        '+'',''''''+ISNULL(RTRIM(REPLACE(CAST(' + COLUMN_NAME +' AS VARCHAR), '''''''', '''''''''''')), '''') + '''''''' FROM ' +
        @table_name + char(13),
    @print_stmt = @print_stmt +'PRINT @' + COLUMN_NAME + char(13),
    @combine_all = @combine_all + '+ '' UNION SELECT ALL ''+ @' +COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = @table_name and
    (isnull(@column_list, '') = '' or charindex(COLUMN_NAME, @column_list) > 0)
order by ORDINAL_POSITION

select
    @declare_var_section = 'DECLARE @sqlstr VARCHAR(8000)' + @declare_var_section + char(13) + char(13),
    @init_var_section = 'SELECT @sqlstr=''''' + @init_var_section + char(13) + char(13),
    @print_final = 'PRINT (@title' + replace(@combine_all, '+', '+ CHAR(13) +') + ')' + char(13) + char(13),
    @exec_stmt = 'EXEC (@title' + @combine_all + ')' + char(13) + char(13),
    @select_title =
    'declare @count int, @title nvarchar(4000)
    select @count = 1, @title = ''''
    select  @title = @title + '',['' +cast(@count as varchar) + '']='''''''''', @count = @count+1 from ' + @table_name +'
    select @title = ''select [0]='''''''''' + @title + '' where 1=2''' + char(13) + char(13)

print '-- BEGIN DUMP SQL STATEMENT --'
print '-- @declare_var_section'
print @declare_var_section
print '-- @init_var_section'
print @init_var_section
print '-- @gen_select_stmt'
print @gen_select_stmt
print '-- @select_title'
print @select_title
print '-- @print_final'
print @print_final
print '-- @exec_stmt'
print @exec_stmt
print '-- END OF DUMP SQL STATEMENT --'
-- print '-- @combine_all'
-- print @combine_all
-- print '-- @print_stmt'
-- print @print_stmt
print ''

exec(@declare_var_section+
    @init_var_section+
    @gen_select_stmt+
    @select_title+
    @print_final+
    @exec_stmt)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值