惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - 卡车司机

macOS系统下修改hosts文件,安装homebrew & nvm How to see log files in MySQL? git 设置和取消代理 使用本地下载和管理的免费 Windows 10 虚拟机测试 IE11 和旧版 Microsoft Edge 在Microsoft SQL SERVER Management Studio下如何完整输出NVARCHAR(MAX)字段或变量的内容 windows 10 x64系统下在vmware workstation pro 15安装macOS 10.15 Catelina, 并设置分辨率为3840x2160 在Windows 10系统下将Git项目签出到磁盘分区根目录的方法 群晖NAS(Synology NAS)环境下安装GitLab, 并在Windows 10环境下使用Git windows 10 专业版安装VMware虚拟机碰到的坑 PDF.js实现个性化PDF渲染(文本复制) Java平台下利用aspose转word为PDF实现文档在线预览 Razor Page Library:开发独立通用RPL(内嵌wwwroot资源文件夹) 杂记 Code First Migrations in Team Environments visual studio 使用正则表达式实现代码批量查找和替换 AngularJs - Calling Directive Method from Controller Entity Framework Power Tools 执行数据库反向工程时报错.... SVN-无法查看log,提示Want to go offline,时间显示1970问题 windows server安装dotnet-sdk-2.2.108-win-x64.exe时报dll找不到
SQL Server UPDATE JOIN
卡车司机 · 2019-12-20 · via 博客园 - 卡车司机

2019-12-20 09:39  卡车司机  阅读(6897)  评论()    收藏  举报

Summary: in this tutorial, you will learn how to use the SQL Server UPDATE JOIN statement to perform a cross-table update.

SQL Server UPDATE JOIN syntax

To query data from related tables, you often use the join clauses, either inner join or left join. In SQL Server, you can use these join clauses in the UPDATE statement to perform a cross-table update.

The following illustrates the syntax of the UPDATE JOIN clause:

UPDATE

    t1

SET

    t1.c1 = t2.c2,

    t2.c2 = expression,

    ...  

FROM

    t1

    [INNER | LEFT] JOIN t2 ON join_predicate

WHERE

    where_predicate;

In this syntax:

  • First, specify the name of the table (t1) that you want to update in the UPDATE clause.
  • Next, specify the new value for each column of the updated table.
  • Then, again specify the table from which you want to update in the FROM clause.
  • After that, use either INNER JOIN or LEFT JOIN to join to another table (t2) using a join predicate specified after the ON keyword.
  • Finally, add an optional WHERE clause to specify rows to be updated.

SQL Server UPDATE JOIN examples

Let’s take a look at some examples of using the UPDATE JOIN statement.

Setting up sample tables

First, create a new table named sales.targets to store the sales targets:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

DROP TABLE IF EXISTS sales.targets;

CREATE TABLE sales.targets

(

    target_id  INT PRIMARY KEY,

    percentage DECIMAL(4, 2)

        NOT NULL DEFAULT 0

);

INSERT INTO

    sales.targets(target_id, percentage)

VALUES

    (1,0.2),

    (2,0.3),

    (3,0.5),

    (4,0.6),

    (5,0.8);

If sales staffs achieved the target 1, they will get the ratio of 0.2 or 20% sales commission and so on.

Second, create another table named sales.commissions to store the sales commissions:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

CREATE TABLE sales.commissions

(

    staff_id    INT PRIMARY KEY,

    target_id   INT,

    base_amount DECIMAL(10, 2)

        NOT NULL DEFAULT 0,

    commission  DECIMAL(10, 2)

        NOT NULL DEFAULT 0,

    FOREIGN KEY(target_id)

        REFERENCES sales.targets(target_id),

    FOREIGN KEY(staff_id)

        REFERENCES sales.staffs(staff_id),

);

INSERT INTO

    sales.commissions(staff_id, base_amount, target_id)

VALUES

    (1,100000,2),

    (2,120000,1),

    (3,80000,3),

    (4,900000,4),

    (5,950000,5);

The sales.commissions table stores sales staff identification, target_idbase_amount, and commission. This table links to the sales.targets table via the target_id column.

Our goal is to calculate the commissions of all sales staffs based on their sales targets.

A) SQL Server UPDATE INNER JOIN example

The following statement uses the UPDATE INNER JOIN to calculate the sales commission for all sales staffs:

UPDATE

    sales.commissions

SET

    sales.commissions.commission =

        c.base_amount * t.percentage

FROM

    sales.commissions c

    INNER JOIN sales.targets t

        ON c.target_id = t.target_id;

Here is the output:

If you query the sales.commissions table again, you will see that the values in the commission column are updated:

SELECT

    *

FROM

    sales.commissions;

SQL Server UPDATE JOIN - INNER JOIN example

B) SQL Server UPDATE LEFT JOIN example

Suppose we have two more new sales staffs that have just joined and they don’t have any target yet:

INSERT INTO

    sales.commissions(staff_id, base_amount, target_id)

VALUES

    (6,100000,NULL),

    (7,120000,NULL);

We assume that the commission for the new sales staffs is 0.1 or 10%, we can update the commission of all sales staffs using the UPDATE LEFT JOIN as follows:

UPDATE

    sales.commissions

SET  

    sales.commissions.commission =

        c.base_amount  * COALESCE(t.percentage,0.1)

FROM  

    sales.commissions c

    LEFT JOIN sales.targets t

        ON c.target_id = t.target_id;

In this example, we used COALESCE() to return 0.1 if the percentage is NULL.

Note that if you use the UPDATE INNER JOIN clause, just the five rows of the table whose targets are not NULL will be updated.

Let’s examine the data in the sales.commissions table:

SELECT

  *

FROM

    sales.commissions;

The result set is as follows:

SQL Server UPDATE JOIN - LEFT JOIN example

In this tutorial, you have learned how to use the SQL Server UPDATE JOIN statement to perform a cross-table update.