用3GL变量读写数据表记录

use pubs
go

declare
    @table nvarchar(100),
    @target_cols nvarchar(4000),
    @include bit -- include or exclude target columns ?
select @table = 'jobs', @target_cols = '', @include = 1

-- START
declare @declaration nvarchar(4000), @read_record nvarchar(4000), @write_record nvarchar(4000), @print_result nvarchar(4000)
select @declaration = '', @read_record = '', @write_record = '', @print_result = ''

-- generate declaration list and assignment list
select
    @declaration = @declaration + declaration,
    @read_record = @read_record + read_record,
    @write_record = @write_record + write_record,
    @print_result = @print_result + print_stmt
from
(select
        declaration = variable + ' ' + upper(DATA_TYPE) collate chinese_prc_bin + case
            when charindex('char',  DATA_TYPE) > 0 then  + '(' + convert(varchar, CHARACTER_MAXIMUM_LENGTH) + ')'
            when charindex('decimal',  DATA_TYPE) > 0 then '(' + convert(varchar, NUMERIC_PRECISION) + ',' + convert(varchar, NUMERIC_SCALE) + ')'
            else '' end + ',',
        read_record = variable + ' = ' + COLUMN_NAME + ',',
        write_record = COLUMN_NAME + ' = ' + variable + ',',
        print_stmt = 'print ''' + variable + '      = '' + isnull(cast(' + variable + ' as varchar), ''null'')' + char(13)
from
    (select variable =
            case
            when charindex('text',  DATA_TYPE) > 0 then null
            when charindex('image',  DATA_TYPE) > 0 then null
            else
                '@' + case
                when charindex('char',  DATA_TYPE) > 0 then  + 'c'
                when charindex('int',  DATA_TYPE) > 0 then  + 'i'
                when charindex('decimal',  DATA_TYPE) > 0 then 'd'
                when charindex('real',  DATA_TYPE) > 0 then 'd'
                when charindex('float',  DATA_TYPE) > 0 then 'f'
                when charindex('bit',  DATA_TYPE) > 0 then  + 'b'
                when charindex('datetime',  DATA_TYPE) > 0 then  + 'd'
                when charindex('money',  DATA_TYPE) > 0 then  + 'm'
                else ''
                end + COLUMN_NAME
            end, *
    from INFORMATION_SCHEMA.COLUMNS
    where TABLE_NAME = @table and
        (isnull(@target_cols, '') = '' or @include = case when charindex(COLUMN_NAME, @target_cols) > 0 then 1 else 0 end)
    ) T
    where variable is not null) T2

-- modify the declaration list and assignment list
select
    @declaration = 'declare ' + left(@declaration, len(@declaration)-1) + char(13),
    @read_record = 'select top 1 ' + left(@read_record, len(@read_record)-1) + ' from ' + @table + ' where 1=2' + char(13),
    @write_record = 'update ' + @table + ' set ' + left(@write_record, len(@write_record)-1) + ' where 1=2' + char(13)

print @declaration
print @read_record
print @print_result
print '-- ' + @write_record
print '--------------------------------------------------------------------------------------'
exec(@declaration + @read_record + @print_result)

/*
declare @ijob_id SMALLINT,@cjob_desc VARCHAR(50),@imin_lvl TINYINT,@imax_lvl TINYINT^M
select top 1 @ijob_id = job_id,@cjob_desc = job_desc,@imin_lvl = min_lvl,@imax_lvl = max_lvl from jobs where 1=2^M
print '@ijob_id     = ' + isnull(cast(@ijob_id as varchar), 'null')^Mprint '@cjob_desc       = ' + isnull(cast(@cjob_desc as varchar), 'null')^Mprint '@imin_lvl      = ' + isnull(cast(@imin_lvl as varchar), 'null')^Mprint '@imax_lvl       = ' + isnull(cast(@imax_lvl as varchar), 'null')^M
-- update jobs set job_id = @ijob_id,job_desc = @cjob_desc,min_lvl = @imin_lvl,max_lvl = @imax_lvl where 1=2^M
--------------------------------------------------------------------------------------
@ijob_id        = null
@cjob_desc      = null
@imin_lvl       = null
@imax_lvl       = null

*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值