同步a到b.sql

来自「SQL语言常用的一些命令各代码」· SQL 代码 · 共 50 行

SQL
50
字号
名称。单价。数量。变量1。变量2
a      2.1   2
b      2.1   3


create table a(名称 varchar(10),单价 money ,数量 int,日期  datetime)
insert into a select 'a',      2.1,   2, '2006-01-01 08:01:00'
union all      select 'b',      2.2 ,  3 ,'2006-01-02 08:11:00'

create table cc(名称 varchar(10),单价 money ,数量 int,日期  varchar(10))
select * from a
alter trigger update_b on a
for insert
as
declare @b int
select  @b=(select count(0) from inserted)
   declare @x table(id int identity(1,1),名称 varchar(10),单价 money ,数量 int,日期  varchar(10))
      insert into @x( 名称,单价,数量,日期) select 名称,单价,数量,convert(varchar(10),日期,121) from inserted
   declare @a varchar(10),@c int,@d money,@e datetime
      select @c=(select min(id) from @x)
      select @a=(select 名称 from @x where id=@c)
      select @d=(select 单价 from @x where id=@c)
while @b>0
  begin
    if @a in (select 名称 from cc) 
      begin  
        if @d<>(select 单价 from cc where 名称=@a)
          begin
           insert into cc select 名称,单价,数量,日期 from @x where id=@c
          end
        else if @d=(select 单价 from cc where 名称=@a)
           begin
            update cc set 数量=数量+(select 数量 from @x where id=@c )
           end
       end 
    else if @a not  in (select 名称 from cc) 
           begin
           insert into cc select 名称,单价,数量,日期 from @x where id=@c
           end
      select @c=(select min(id) from @x where id>@c)
      select @a=(select 名称 from @x where id=@c)
      select @d=(select 单价 from @x where id=@c)
      select @b=@b-1
   end



select * from cc

drop table cc table a

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?