• 用触发器实现动态新增列


    create table ta(number int)

    create trigger test_tr on ta
    for insert
    as
    begin
    DECLARE @number int,@sql varchar(4000),@sql1 varchar(4000),@i int
    DECLARE roy CURSOR
    FOR SELECT * from inserted
    OPEN roy
    FETCH next FROM roy into @number
    WHILE @@FETCH_STATUS = 0
    begin
    begin
      if exists(select 1 from sysobjects where name='tb' and xtype='U')
        begin
             select @i=max(colid)+1 from syscolumns where id=object_id('tb')
             set @sql=N'alter table tb add C'+cast(@i as varchar)+' int null'
             set @sql1=N'update tb set C'+cast(@i as varchar)+'='+cast(@number as varchar)
    --print @sql
    --print @sql1
             exec(@sql)
             exec(@sql1)
        end
    else
        begin
             set @sql=N'create table tb(C1 int)'
             set @sql1=N' insert tb select C1='+cast(@number as varchar)
             exec(@sql)
             exec(@sql1)
        end
    end
    FETCH NEXT FROM roy INTO @number
    end
    CLOSE roy
    DEALLOCATE roy
    end
    测试:
    insert ta
    select 10 union all
    select 13 union all
    select 18 union all
    select 26
    查询:
    select * from tb

    C1          C2          C3          C4         
    ----------- ----------- ----------- -----------
    10          13          18          26

    (所影响的行数为 1 行)

    --drop table ta,tb
     

  • 相关阅读:
    /pentest/enumeration/0trace/0trace.sh
    2.2synchronized同步语句块
    2.1synchronized同步方法
    第二章:对象及变量的并发序言
    第一章总结
    1.11守护线程
    1.10线程的优先级
    1.9yield方法
    1.8暂停线程
    微信自动回复
  • 原文地址:https://www.cnblogs.com/Roy_88/p/5463117.html
Copyright © 2020-2023  润新知