Insert and Update in SQL Using User-Defined Table Type

后端 未结 2 1480
悲&欢浪女
悲&欢浪女 2021-02-08 09:05

Following is new data type that I created.

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


        
2条回答
  •  后悔当初
    2021-02-08 09:25

    You can Simply do 2 Queries: 1. "Update" command 2. "Insert" command.

    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 dbo.HotelInfo 
        JOIN @XHotelInfoDetails X ON X.Id = HotelInfo.Id
    
        INSERT INTO dbo.HotelInfo (Col1,Col2)
        SELECT X.Col1,
               X.Col2
        FROM @XHotelInfoDetails X
        WHERE NOT EXISTS
                       (
                         SELECT 1
                         FROM dbo.HotelInfo InnerTable
                         WHERE X.Id = InnerTable.Id
                       )
    
    END
    

提交回复
热议问题