久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用用戶定義的表類型在 SQL 中插入和更新

Insert and Update in SQL Using User-Defined Table Type(使用用戶定義的表類型在 SQL 中插入和更新)
本文介紹了使用用戶定義的表類型在 SQL 中插入和更新的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

以下是我創(chuàng)建的新數(shù)據(jù)類型.

Following is new data type that I created.

CREATE TYPE [dbo].[UpdateHotelTableType] AS TABLE(
    [ID] [int] NULL,
    [HotelID] [int] NULL,
    [FromDate] [datetime] NULL,

)

以下是我使用上述數(shù)據(jù)類型的存儲(chǔ)過程.

Following is my stored procedure that I used the above datatype.

ALTER PROCEDURE [dbo].[SP_Hotel_Info_Update]
     -- Add the parameters for the stored procedure here
    @XHotelInfoDetails UpdateHotelTableType READONLY,

AS
BEGIN

    Update dbo.HotelInfo
    SET 
    FromDate = r.FromDate,
    from @XHotelInfoDetails r
    Where HotelInfo.ID = r.ID

END

這適用于數(shù)據(jù)庫中的更新結(jié)果.但我想檢查 id 是否存在,如果 id 不存在,則將該行插入表中.否則更新當(dāng)前記錄.在這里,我正在發(fā)送要更新的數(shù)據(jù)列表.

This is working fine for update results in database. But I want to check whether the id is exists and if the id is not exists insert the row in to the table. otherwise update current record. In here I am sending the list of data for update.

誰能幫我重新創(chuàng)建存儲(chǔ)過程,通過檢查ID是否存在來插入數(shù)據(jù).

Can any one help me to recreate the stored procedure to insert the data too by checking the existence of ID.

推薦答案

使用 MERGE:

根據(jù)與源表的聯(lián)接結(jié)果對(duì)目標(biāo)表執(zhí)行插入、更新或刪除操作.例如,您可以通過插入、更新或刪除行來同步兩個(gè)表在一個(gè)表中基于在另一表中發(fā)現(xiàn)的差異.

Performs insert, update, or delete operations on a target table based on the results of a join with a source table. For example, you can synchronize two tables by inserting, updating, or deleting rows in one table based on differences found in the other table.

ALTER PROCEDURE [dbo].[SP_Hotel_Info_Update]
     -- Add the parameters for the stored procedure here
    @XHotelInfoDetails UpdateHotelTableType READONLY,

AS
BEGIN

    MERGE dbo.HotelInfo AS trg
    USING @XHotelInfoDetails AS src
      ON src.ID = trg.ID
     WHEN MATCHED THEN
       UPDATE SET FromDate = src.FromDate
     WHEN NOT MATCHED BY TARGET THEN
       INSERT (col1, col2, ...)
       VALUES (src.col1, src.col2, ...);
END

在我的數(shù)據(jù)表中,可以有新添加的行以及已刪除的行.那么如何比較 id 并從 hotelinfo 表中刪除行?

In my datatable, there can be newly added rows as well as deleted rows. So how can I compare the id and delete rows from hotelinfo table?

您可以添加新子句:

WHEN NOT MATCHED BY SOURCE [ AND <clause_search_condition> ]  
     THEN DELETE;

在特定條件下從目標(biāo)中刪除數(shù)據(jù).

with specific condition to delete data from target.

這篇關(guān)于使用用戶定義的表類型在 SQL 中插入和更新的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Sql server table usage statistics(Sql server 表使用情況統(tǒng)計(jì))
Relative path in t sql?(t sql中的相對(duì)路徑?)
Getting the last record in SQL in WHERE condition(在 WHERE 條件下獲取 SQL 中的最后一條記錄)
Query to get XML output for hierarchical data using FOR XML PATH in SQL Server(在 SQL Server 中使用 FOR XML PATH 查詢以獲取分層數(shù)據(jù)的 XML 輸出)
T-SQL IF statement embedded in a sum() function(嵌入在 sum() 函數(shù)中的 T-SQL IF 語句)
Table vs Temp Table Performance(表與臨時(shí)表性能)
主站蜘蛛池模板: 午夜精品一区二区三区三上悠亚 | 91视频网 | 自拍偷拍第1页 | 免费一级大片 | 亚洲视频一区二区三区四区 | 国产一区二区三区四区五区加勒比 | 亚洲欧美日韩成人在线 | 成人国产在线观看 | 欧美国产日韩一区二区三区 | 91极品欧美视频 | 国产情侣久久 | 精品久久久久久亚洲精品 | 欧美一区二 | 国产成人99久久亚洲综合精品 | 国产精品三级久久久久久电影 | 日韩在线第一 | 亚洲视频不卡 | 91精品成人久久 | 国产伦精品一区二区三区照片91 | 中文字幕人成人 | 国产一区二区三区 | 国产伦精品一区二区三区照片91 | 99精品在线观看 | 天天操夜夜拍 | 日韩成人免费在线视频 | 日日夜夜精品 | 中文字幕一区二区三区乱码图片 | 国内自拍视频在线观看 | 久久精品国产清自在天天线 | 日日噜噜夜夜爽爽狠狠 | 久久av一区二区三区 | 国产精品免费一区二区三区四区 | 在线一区二区三区 | 欧美三级在线 | 国产激情一区二区三区 | 91视频进入 | 成人免费一级视频 | 亚洲天堂精品久久 | av黄色片| 日本福利在线观看 | 成年人在线视频 |