使用用户定义的表类型在SQL中插入和更新

时间:2022-07-01 12:56:14

Following is new data type that I created.

以下是我创建的新数据类型。

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

)

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

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.

这适用于数据库中的更新结果。但我想检查id是否存在,如果id不存在,请将行插入表中。否则更新当前记录。在这里,我发送更新数据列表。

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

任何人都可以通过检查ID的存在来帮助我重新创建存储过程以插入数据。

2 个解决方案

#1


6  

Use MERGE:

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

EDIT:

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?

在我的数据表中,可以添加新添加的行以及删除的行。那么如何比较hotelinfo表中的id和delete行呢?

You could add new clause:

你可以添加新的子句:

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

with specific condition to delete data from target.

具体条件从目标中删除数据。

#2


4  

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

你可以简单地做2个查询:1。“更新”命令2.“插入”命令。

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

#1


6  

Use MERGE:

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

EDIT:

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?

在我的数据表中,可以添加新添加的行以及删除的行。那么如何比较hotelinfo表中的id和delete行呢?

You could add new clause:

你可以添加新的子句:

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

with specific condition to delete data from target.

具体条件从目标中删除数据。

#2


4  

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

你可以简单地做2个查询:1。“更新”命令2.“插入”命令。

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