Insert or update data using cte_results in SQL Server

Aura

I have a query having cte with number of columns, I want to insert a record if ID from the results of that query does not exist in table that I am inserting, or if the ID exists I want to update data using that ID.

So far I have tried this:

WITH cte_base as(
SELECT DISTINCT ID, statusID
FROM testtable
)

SELECT *
FROM cte_base

IF EXISTS(SELECT * FROM Newtable WHERE EXISTS (SELECT ID FROM cte_base))
UPDATE newtable
SET statusID = 2
WHERE Newtable.ID = cte_base.ID

ELSE
INSERT INTO newtable(ID, statusID)
SELECT ID, statusID 
FROM cte_base
WHERE Newtable.ID <> cte_base.ID

I have to run this query against live data, hence I would like to know if my logic is correct.

Anthony Hancock

Basic merge example based on your provided code.

MERGE INTO NewTable AS T
USING
(
    SELECT DISTINCT ID,statusID
    FROM testtable
) AS S
ON S.ID = T.ID
WHEN MATCHED THEN SET
    T.StatusID = 2
WHEN NOT MATCHED INSERT (ID,statusID)
    VALUES (S.ID,S.statusID)
;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Update Temp table to Insert node in XML data using SQL

分類Dev

SQL Server INSERT Command doesn't insert data

分類Dev

SQL Server:INSERT、UPDATE後にトリガー

分類Dev

How to insert data into Microsoft Sql using powershell?

分類Dev

SQL Server - Using joins in Update statement

分類Dev

SQL Insert multiple record while using ON DUPLICATE KEY UPDATE

分類Dev

how to insert and update data into postgresql from node js using if condition

分類Dev

How to insert multiple rows into SQL Server Parallel Data Warehouse table

分類Dev

Insert data in Sql Server datable from Datagridview c#

分類Dev

SQL Server insert table data script with new Primary Key

分類Dev

Select table from Sql server and insert data to Mysql table

分類Dev

how to insert sql dump into remote mysql server using PuTTY?

分類Dev

how to insert sql dump into remote mysql server using PuTTY?

分類Dev

Pandas insert into SQL Server

分類Dev

Using jquery to insert data into an input box, but it does not POST to server

分類Dev

Using the SQL Server Geography data type

分類Dev

Insert data row wise in a SQL Table from a CSV using PowerShell

分類Dev

Insert and update a datetime into SQL database

分類Dev

Can't update then insert in SQL

分類Dev

How to update row if exist and meet more than one condition and insert it otherwise in SQL Server

分類Dev

Edit SQL Server Database (INSERT, UPDATE, etc) in ASP.NET (VB)

分類Dev

UPDATE SQL SERVER

分類Dev

Syntax for SQL Trigger to Insert Data in another DB and also to update any field in another db adfter a field is edited

分類Dev

SQL server calculate and insert with loops

分類Dev

SQL Server:INSERT INTO SELECT MAX

分類Dev

How to Insert value to sql server database from data table vb.net

分類Dev

How to insert large amount of data into SQL Server 2008 with c# code?

分類Dev

Sql query to insert datetime in SQL Server

分類Dev

How to fill a Select with data from SQL Server? Using ASP Classic

Related 関連記事

  1. 1

    Update Temp table to Insert node in XML data using SQL

  2. 2

    SQL Server INSERT Command doesn't insert data

  3. 3

    SQL Server:INSERT、UPDATE後にトリガー

  4. 4

    How to insert data into Microsoft Sql using powershell?

  5. 5

    SQL Server - Using joins in Update statement

  6. 6

    SQL Insert multiple record while using ON DUPLICATE KEY UPDATE

  7. 7

    how to insert and update data into postgresql from node js using if condition

  8. 8

    How to insert multiple rows into SQL Server Parallel Data Warehouse table

  9. 9

    Insert data in Sql Server datable from Datagridview c#

  10. 10

    SQL Server insert table data script with new Primary Key

  11. 11

    Select table from Sql server and insert data to Mysql table

  12. 12

    how to insert sql dump into remote mysql server using PuTTY?

  13. 13

    how to insert sql dump into remote mysql server using PuTTY?

  14. 14

    Pandas insert into SQL Server

  15. 15

    Using jquery to insert data into an input box, but it does not POST to server

  16. 16

    Using the SQL Server Geography data type

  17. 17

    Insert data row wise in a SQL Table from a CSV using PowerShell

  18. 18

    Insert and update a datetime into SQL database

  19. 19

    Can't update then insert in SQL

  20. 20

    How to update row if exist and meet more than one condition and insert it otherwise in SQL Server

  21. 21

    Edit SQL Server Database (INSERT, UPDATE, etc) in ASP.NET (VB)

  22. 22

    UPDATE SQL SERVER

  23. 23

    Syntax for SQL Trigger to Insert Data in another DB and also to update any field in another db adfter a field is edited

  24. 24

    SQL server calculate and insert with loops

  25. 25

    SQL Server:INSERT INTO SELECT MAX

  26. 26

    How to Insert value to sql server database from data table vb.net

  27. 27

    How to insert large amount of data into SQL Server 2008 with c# code?

  28. 28

    Sql query to insert datetime in SQL Server

  29. 29

    How to fill a Select with data from SQL Server? Using ASP Classic

ホットタグ

アーカイブ