Home Database Mysql Tutorial SQL Server 2005中导入或覆盖数据

SQL Server 2005中导入或覆盖数据

Jun 07, 2016 pm 02:55 PM
server sql use company import data cover

公司 A 使用 SQL Server 2005 数据库。来自贸易伙伴的客户数据每天晚上都要导入到客户表。你要确保,在导入的过程中,对于存在的客户数据进行更新、对于不存在的客户数据进行插入。 无 /*INSTEAD OF 触发器的主要优点是可以使不能更新的视图支持更新。包含多

公司 A 使用 SQL Server 2005 数据库。来自贸易伙伴的客户数据每天晚上都要导入到客户表。你要确保,在导入的过程中,对于存在的客户数据进行更新、对于不存在的客户数据进行插入。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

/*

INSTEAD OF 触发器的主要优点是可以使不能更新的视图支持更新。包含多个基表的视图必须使用 INSTEAD OF 触发器来支持引用表中数据的插入、更新和删除操作。INSTEAD OF 触发器的另一个优点是使您得以编写这样的逻辑代码:可以拒绝批处理中的某些部分同时允许批处理的其它部分成功。

INSTEAD OF 触发器可以进行以下操作:

忽略批处理中的某些部分。

不处理批处理中的某些部分并记录有问题的行。

如果遇到错误情况则采取备用操作。

说明  在含有用 DELETE 或 UPDATE 操作定义的外键的表上,不能定义 INSTEAD OF DELETE 和 INSTEAD OF UPDATE 触发器。

将此逻辑作为 INSTEAD OF 触发器的一部分进行编码,可避免所有访问数据的应用程序必须重新执行该逻辑。

在下列 Transact-SQL 语句序列中,INSTEAD OF 触发器更新视图中的两个基表。另外,显示两种处理错误的方法:

忽略对 Person 表的重复插入,并且插入的信息将记录在 PersonDuplicates 表中。

将对 EmployeeTable 表的重复插入转变为 UPDATE 语句,该语句将当前信息检索至 EmployeeTable,而不会产生重复键侵犯。 Transact-SQL 语句创建两个基表、一个视图、一个记录错误表和视图上的 INSTEAD OF 触发器。下面的这些表将个人数据和业务数据分开并且是视图的基表:

*/

CREATE TABLE Person

   (

    SSN         char(11) PRIMARY KEY,

    Name        nvarchar(100),

    Address     nvarchar(100),

    Birthdate   datetime

   )

 

CREATE TABLE EmployeeTable

   (

    EmployeeID       int PRIMARY KEY,

    SSN              char(11) UNIQUE,

    Department       nvarchar(10),

    Salary           money,

    CONSTRAINT FKEmpPer FOREIGN KEY (SSN)

    REFERENCES Person (SSN)

   )

 

--下面的视图使用某个人的两个表中的所有相关数据建立报表:

 

CREATE VIEW Employee AS

SELECT P.SSN as SSN, Name, Address,

       Birthdate, EmployeeID, Department, Salary

FROM Person P, EmployeeTable E

WHERE P.SSN = E.SSN

 

--可记录对插入具有重复的社会安全号的行的尝试。PersonDuplicates 表记录插入的值、尝试插入操作的用户的用户名和插入的时间:

 

CREATE TABLE PersonDuplicates

   (

    SSN           char(11),

    Name          nvarchar(100),

    Address       nvarchar(100),

    Birthdate     datetime,

    InsertSNAME   nchar(100),

    WhenInserted  datetime

   )

 

--INSTEAD OF 触发器在单独视图的多个基表中插入行。将对插入具有重复社会安全号的行的尝试记录在 PersonDuplicates 表中。将 EmployeeTable 中的重复行更改为更新语句。

 

CREATE TRIGGER IO_Trig_INS_Employee ON Employee

INSTEAD OF INSERT

AS

BEGIN

SET NOCOUNT ON

-- Check for duplicate Person. If no duplicate, do an insert.

IF (NOT EXISTS (SELECT P.SSN

      FROM Person P, inserted I

      WHERE P.SSN = I.SSN))

   INSERT INTO Person

      SELECT SSN,Name,Address,Birthdate,Comment

      FROM inserted

ELSE

-- Log attempt to insert duplicate Person row in PersonDuplicates table.

   INSERT INTO PersonDuplicates

      SELECT SSN,Name,Address,Birthdate,SUSER_SNAME(),GETDATE()

      FROM inserted

-- Check for duplicate Employee. If no duplicate, do an insert.

IF (NOT EXISTS (SELECT E.SSN

      FROM EmployeeTable E, inserted

      WHERE E.SSN = inserted.SSN))

   INSERT INTO EmployeeTable

      SELECT EmployeeID,SSN, Department, Salary,Comment

      FROM inserted

ELSE

--If duplicate, change to UPDATE so that there will not

--be a duplicate key violation error.

   UPDATE EmployeeTable

      SET EmployeeID = I.EmployeeID,

          Department = I.Department,

          Salary = I.Salary,

          Comment = I.Comment

   FROM EmployeeTable E, inserted I

   WHERE E.SSN = I.SSN

END

Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Apr 03, 2024 pm 12:04 PM

0.What does this article do? We propose DepthFM: a versatile and fast state-of-the-art generative monocular depth estimation model. In addition to traditional depth estimation tasks, DepthFM also demonstrates state-of-the-art capabilities in downstream tasks such as depth inpainting. DepthFM is efficient and can synthesize depth maps within a few inference steps. Let’s read about this work together ~ 1. Paper information title: DepthFM: FastMonocularDepthEstimationwithFlowMatching Author: MingGui, JohannesS.Fischer, UlrichPrestel, PingchuanMa, Dmytr

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Apr 01, 2024 pm 07:46 PM

The performance of JAX, promoted by Google, has surpassed that of Pytorch and TensorFlow in recent benchmark tests, ranking first in 7 indicators. And the test was not done on the TPU with the best JAX performance. Although among developers, Pytorch is still more popular than Tensorflow. But in the future, perhaps more large models will be trained and run based on the JAX platform. Models Recently, the Keras team benchmarked three backends (TensorFlow, JAX, PyTorch) with the native PyTorch implementation and Keras2 with TensorFlow. First, they select a set of mainstream

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks Apr 29, 2024 pm 06:55 PM

I cry to death. The world is madly building big models. The data on the Internet is not enough. It is not enough at all. The training model looks like "The Hunger Games", and AI researchers around the world are worrying about how to feed these data voracious eaters. This problem is particularly prominent in multi-modal tasks. At a time when nothing could be done, a start-up team from the Department of Renmin University of China used its own new model to become the first in China to make "model-generated data feed itself" a reality. Moreover, it is a two-pronged approach on the understanding side and the generation side. Both sides can generate high-quality, multi-modal new data and provide data feedback to the model itself. What is a model? Awaker 1.0, a large multi-modal model that just appeared on the Zhongguancun Forum. Who is the team? Sophon engine. Founded by Gao Yizhao, a doctoral student at Renmin University’s Hillhouse School of Artificial Intelligence.

See all articles