---
title: "update的其它用法"
author: "icyheart"
date: "2009-07-02T13:52:24Z"
tags: []
categories: ["企业架构"]
slug: "update"
draft: false
lastmod: "2009-07-02T16:55:06Z"
id: 418935
url: "https://sucre.iteye.com/blog/418935"
---

Update除了有基本的语法格式外还有其它的一些常用的格式：

(1)、更新数据为内部变量或者函数，格式为：

    update 表名

    set 字段名1=函数1\[…,字段名n=函数n]

    \[where 条件]

    eg:

update student

       set sbirth=getdata()

       where sno=’990001’

(2)、更新数据为同一记录的其他字段值(将一张表中的同一记录的某个字段值改成另一字段的值)，格式为：

    update 表名

    set 字段名1=字段名m\[…,字段名m=字段名n]

    from 表名1,表名2

    \[where 条件]

eg:  

           update student

set sdept=stu.sname

from student,student as stu

where student.sno='990001'

(3)、更新数据为不同表的字段值，该方式要求更新数据的目标表和源表有相同的字段(将一个表中字段的值赋给另一个表的某个字段)，格式为：

    update 表名1

set 表名1.字段名1=表名2.字段名1\[...,表名1.字段名n=表名2.字段名n]

from 表名1,表名2

\[where 条件]

eg:

      update student

set sdept=stu.sdept

from student,stu

where student.sno='990001' and stu.sno='990002'

(4)、更新数据为同一个表中的某些字段值(将一个表中不同记录之间的字段值进行替换)，格式为：

    update 表名1

set 表名1.字段名1=表别名.字段名1\[...表名1.字段名n=表别名.字段名n]

from 表名1,表名 as 别名

\[where 条件]

eg:

           update student

set sdept=stu.sdept

from student,student as stu

where student.sno='990001' and stu.sno='990002'