利用带关联子查询Update语句更新数据

      Update是T-sql中再简单不过的语句了,update table set column=expression  [where condition],我们都会用到。但update的用法不仅于此,真正在开发的时候,灵活恰当地使用update可以达到事半功倍的效果。

      假定有表Table1(a,b,c)和Table2(a,c),现在Table1中有些记录字段c为null,要根据字段a在Table2中查找,取出字段a相等的字段c的值来更新Table1。一种常规的思路,通过游标遍历Table1中字段c为null的所有记录,在循环体内查找Table2并进行更新,即用游标Cursor的形式。测试sql语句如下:

ContractedBlock.gifExpandedBlockStart.gif使用游标遍历方式更新
    --1.创建测试表
    create TABLE Table1
    (
        a 
varchar(10),
        b 
varchar(10),
        c 
varchar(10),
        
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
        (
            a 
ASC
        )
    ) 
ON [PRIMARY]

    
create TABLE Table2
    (
        a 
varchar(10),
        c 
varchar(10),
        
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
        (
            a 
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.通过游标方式更新
    declare @name varchar(10)
    
declare @score varchar(10)
    
declare mycursor cursor for select a from Table1 where c is null
    
open mycursor
    
fetch next from mycursor into @name
    
while(@@fetch_status = 0)
    
BEGIN
        
select @score=from Table2 where a=@name
        
update Table1 set c = @score where a = @name
        
fetch next from mycursor into @name    
    
END
    
close mycursor
    
deallocate mycursor
    
GO
    
--4.显示更新后的结果
    select * from Table1
    
GO
    
--5.删除测试表
    drop TABLE Table1
    
drop TABLE Table2

 
     虽然用游标可以实现,但代码看起来很复杂,其实用Update根据子关联来更新只要一条语句就可以搞定了,测试代码如下:

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

    
create TABLE Table2
    (
        a 
varchar(10),
        c 
varchar(10),
        
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
        (
            a 
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 c = (select c from Table2 where a = Table1.a) where c is null
    
GO

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

 
      参考资料:也许是被忽略的update语句,update 子查询

转载于:https://www.cnblogs.com/freshman0216/archive/2008/08/13/1267437.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379942.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SQL Server 2000数据库移植到SQL Server 2008R2数据库服务器中碰到的”3145错误”及解决办法...

辛苦忙碌了一个星期终于安装配置好了TFS服务器,给每个团队成员分配了账户和邮箱。不过,老机器中的部分数据需要备份到新机器中,其中在移植一个使用DVBBS架设的论坛的时候,出了点问题,记录如下,以备查找&…

web安全----XSS漏洞之基本原理

0x01 概述 XSS为跨站脚本攻击,XSS攻击针对的是用户层面的攻击!类型有反射型XSS、存储型XSS、DOM型XSS,这里的DOM可以理解为页面,或者是所有的标签、内容之和 0x02 反射型XSS 反射型XSS攻击流程为: 即: …

面向对象(匿名内部类与有名字内部类的比较)

A:匿名内部类 就是内部类的简化写法B:前提 这里的类可以是具体类也可以是抽象类C:格式 new 类名或者接口(){ //表示继承这个类或实现这个接口重写方法}D:本质是什么呢? 是一个继承了该类或者实现了该接口的子类匿名对象E:案…

如何在Python中针对一个值检查多个变量?

Given multiple variables and they are assigned some values, we have to test a value with these variables. 给定多个变量并为其分配了一些值,我们必须使用这些变量测试一个值。 Let, there are three variables a, b and c and we have to check whether one…

品析《桃花庵歌》

桃花庵歌 【明】唐寅(YIN) 桃花坞里桃花庵,桃花庵下桃花仙; 桃花仙人种桃树,又摘桃花卖酒钱。 酒醒只在花前坐,酒醉还来花下眠; 半醒半醉日复日,花落花开年复年。 但愿老死花酒间&#xf…

面向对象(匿名内部类重写多个方法调用)

①匿名内部类只针对重写一个方法的时候使用; ②若有多个方法,通过匿名内部类进行调用的时候,需要一个一个进行调用,比较麻烦,所以不建议使用。 ③匿名内部类是无法向下转型的,向下转型需要子类的类名,这里面没有子类…

c++ 取两个链表的交集_使用C ++程序查找两个链表的交集

c 取两个链表的交集Problem statement: Write a C program to find the intersection of two single linked lists. 问题陈述:编写一个C 程序来查找两个单个链表的交集。 Example: 例: Let the first linked list be:6->5->2->9->NULLLet …

只在IE中显示

只在IE中显示div{display:none;display:block;_display:block}转载于:https://www.cnblogs.com/lishenglyx/archive/2008/08/21/1273089.html

web安全---XSS漏洞之标签使用2

所有标签已经测试完并可以使用&#xff0c;测试环境&#xff1a;DVWA的反射型XSS&#xff0c;等级low 0x01 <script>标签 <script>alert(2)</script> <script>alert(2)</script//0x02 <img>标签 <img src"x" onerroralert(1)…

Java——多线程(铁路售票系统案例)

问题&#xff1a;铁路售票&#xff0c;一共100张&#xff0c;通过四个窗口卖完。 要求&#xff1a;分别用 继承Thread类 和 实现Runnable接口 去实现 ①用继承Thread类去实现 package com.yy.syn;public class Demo3_Ticket { /*** 铁路售票&#xff0c;一共100张&#xff…

Java LocalDateTime类| 带示例的getDayOfWeek()方法

LocalDateTime类getDayOfWeek()方法 (LocalDateTime Class getDayOfWeek() method) getDayOfWeek() method is available in java.time package. getDayOfWeek()方法在java.time包中可用。 getDayOfWeek() method is used to get the field value day-of-week that is an enum …

软件测试方法大汇总

软件测试方法大汇总 软件测试方法种类繁多&#xff0c;记忆起来混乱&#xff0c; 如果把软件测试方法进行分类, 就会清晰很多。 我参考一些书籍和网上的资料&#xff0c; 把常用的软件测试方法列出来&#xff0c; 让大家对软件测试行业有个总体的看法。 从测试设计方法分类 测试…

web安全----xss工具使用3

XSSer 0x01 安装 环境&#xff1a;kali、python3&#xff08;必须是python3&#xff0c;kali默认为python2&#xff09; 安装步骤&#xff1a; git clone https://github.com/epsylon/xsser.git cd xsser sudo python3 setup.py install 使用命令&#xff1a; xsser -h查看…

Java——多线程(死锁)

死锁是指&#xff1a;两个或两个以上的进程在执行过程中&#xff0c;由于竞争资源或者由于彼此通信而造成的一种阻塞的现象&#xff0c;若无外力作用&#xff0c;它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁&#xff0c;这些永远在互相等待的进程称为死锁进…

c# 前导0_C#| 用前导零填充整数

c# 前导0To pad an integer number with leading zero, we can use String.Format() method which is library method of String class in C#. 要用前导零填充整数&#xff0c;我们可以使用String.Format()方法&#xff0c;该方法是C&#xff03;中String类的库方法。 using S…

走到尽头的技术-MVC

MVC技术是一种WebApplication设计技术&#xff0c;相比于传统Web应用程序&#xff0c;MVC可以使程序结构更加清晰&#xff0c;他采用 Model&#xff0c;View&#xff0c;Controller 来管理和架构我们的Web资源&#xff0c;将不同的请求&#xff08;request&#xff09;导向不同…

Search Engine -垂直搜索小汇总

FilesTube: 共享文件搜索引擎,文件来自:Rapidshare, MegaUpload, Megashares, YouSendIt, SaveFile, FileFront和Badongo等很多文件储存网站,支持的文件格式包括:AVI, MP3, MPEG, MPG, RAR, WMA, WMV, EXE, ZIP等,主要为媒体格式,不支持中文 Picsearch:专业图片搜索引擎,中文界…

Java——多线程(线程安全问题)

同步为安全&#xff0c;不同步为不安全&#xff1b;也就是有synchronized这个标识符&#xff0c;就为线程安全&#xff0c;反之&#xff0c;为线程不安全。 ①Vector是线程安全的 ②StringBuffer是线程安全的 ③Hashtable是线程安全的 Collections.synchronized(xxx)&#…

web安全---XSS利用平台BLUE-LOTUS安装与使用

0x01 安装 环境&#xff1a;windows、phpstudy 下载地址&#xff1a;https://gitee.com/gid1314/BlueLotus_XSSReceiver-master 下载后将文件解压&#xff0c;重命名为blue&#xff0c;放在www目录下 访问&#xff1a;http://IP/blue 点击安装 这里只需要修改后台登陆密码和…

C#Convert.ToInt32(bool)方法-将bool值转换为int

C&#xff03;Convert.ToInt32(bool)方法 (C# Convert.ToInt32(bool) Method) Convert.ToInt32(bool) Method is used to convert a specific Boolean (bool) value to its equivalent integer (int 32 signed number). Convert.ToInt32(bool)方法用于将特定的布尔值(布尔值)转…