Update级联更新

来源:互联网 发布:物业台账软件 编辑:程序博客网 时间:2024/05/18 13:09

使用带关联子查询的Update更新
    --1.创建测试表
    create TABLE Table1
    (
        
varchar(10),
        
varchar(10),
        
varchar(10),
        
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
        (
            
ASC
        )
    
ON [PRIMARY]

    
create TABLE Table2
    (
        
varchar(10),
        
varchar(10),
        
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
        (
            
ASC
        )
    
ON [PRIMARY]
    
GO
    
--2.创建测试数据
    Insert into Table1 values('','asds',null)
    
Insert into Table1 values('','asds','100')
    
Insert into Table1 values('','asds','80')
    
Insert into Table1 values('','asds',null)

    
Insert into Table2 values('','90')
    
Insert into Table2 values('','100')
    
Insert into Table2 values('','80')
    
Insert into Table2 values('','95')
    
GO
    
select * from Table1

    
--3.通过Update方式更新
    Update Table1 set = (select from Table2 where = Table1.a) where is null
    
GO

    
--4.显示更新后的结果
    select * from Table1
    
GO
    
--5.删除测试表
    drop TABLE Table1
    
drop TABLE Table2

----两种update的方法
--1.
update Table1 set Table1.c =Table2.c  from Table1inner join Table2 on Table1.a=Table2.a  whereTable1.c is null
--2.
Update Table1 set c = (select c from Table2 where a = Table1.a)where c is null

0 0
原创粉丝点击