16

我正在尝试使用 SP 将记录插入到一​​张表中。

CREATE TABLE [dbo].[SHARE_AD_GROUP](
    [SHARE_AD_GROUP_ID] [int] IDENTITY(1,1) NOT NULL,
    [SHARE_ID] [int] NOT NULL,
    [AD_GROUP] [varchar](200) NOT NULL,
    [SHARE_PERMISSIONS] [varchar](65) NULL,

以下最佳方法是什么:

1-

ALTER PROCEDURE [dbo].[PROC_INSERT_SHARE_AD_GROUP]


@shareID int,
@ownerId varchar(200),
@sharePermissions varchar(65)

AS

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    UPDATE [dbo].SHARE_AD_GROUP
               SET 
                   [SHARE_PERMISSIONS] = @sharePermissions
             WHERE SHARE_ID = @shareID and [AD_GROUP] = @ownerId
    if @@ROWCOUNT =0 
    begin
    INSERT INTO [dbo].SHARE_AD_GROUP
                       (SHARE_ID,[AD_GROUP],[SHARE_PERMISSIONS])
                 VALUES
                       (@shareID,@ownerId,@sharePermissions)
    end

end

2-

   BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.

    declare @id int
    SET NOCOUNT ON;
    set @id = (select top 1 SHARE_AD_GROUP_ID from SHARE_AD_GROUP where SHARE_ID = @shareID and [AD_GROUP] = @ownerId)
        if @id is null
            begin
                INSERT INTO [dbo].SHARE_AD_GROUP
                       (SHARE_ID,[AD_GROUP],[SHARE_PERMISSIONS])
                 VALUES
                       (@shareID,@ownerId,@sharePermissions)
                 set @id = SCOPE_IDENTITY()
            end
        else
            begin
            UPDATE [dbo].SHARE_AD_GROUP
               SET 
                   [SHARE_PERMISSIONS] = @sharePermissions
             WHERE SHARE_ID = @shareID and [AD_GROUP] = @ownerId
            end
End

3-合并==>不知道怎么写。

你认为最快的方法是什么?

4

2 回答 2

13

我认为合并语句比您以两种方式演示的更快。如果您需要了解更多关于如何在 sql server 中编写 MERGE INTO Query 的信息,请点击链接。

http://blog.sqlauthority.com/2008/08/28/sql-server-2008-introduction-to-merge-statement-one-statement-for-insert-update-delete/

https://www.simple-talk.com/sql/learn-sql-server/the-merge-statement-in-sql-server-2008/

你的合并块看起来像这样,你需要把它放在你的存储过程或任何其他地方。

MERGE INTO SHARE_AD_GROUP A
USING (
    SELECT SHARE_AD_GROUP_ID,
        SHARE_ID,
        AD_GROUP,
        SHARE_PERMISSIONS
    FROM SHARE_AD_GROUP
    WHERE SHARE_ID = @shareID AND AD_GROUP = @ownerId
) B ON (A.SHARE_AD_GROUP_ID = B.SHARE_AD_GROUP_ID)
WHEN MATCHED THEN
    UPDATE SET A.SHARE_PERMISSIONS = B.SHARE_PERMISSIONS
WHEN NOT MATCHED THEN
    INSERT (SHARE_PERMISSIONS) VALUES(@sharePermissions);
于 2013-09-18T13:45:51.067 回答
6

我很确定你已经想通了。但有人可能会发现它很有用。

ALTER PROCEDURE [dbo].[PROC_INSERT_SHARE_AD_GROUP]
    @shareID int,
    @ownerId varchar(200),
    @sharePermissions varchar(65)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    MERGE [dbo].SHARE_AD_GROUP AS T
    USING
    (
        SELECT @shareID,@ownerId,@sharePermissions
    ) AS S
    (
        SELECT Share_ID,AD_Group,Share_Permissions
    )
    ON (T.SHARE_ID = S.Share_ID and T.AD_GROUP = S.AD_Group)

    WHEN MATCHED THEN
        UPDATE SET SHARE_PERMISSIONS = S.Share_Permissions

    WHEN NOT MATCHED THEN
        INSERT
            (SHARE_ID,AD_GROUP,SHARE_PERMISSIONS)
        VALUES
            (S.Share_ID,S.AD_Group,S.Share_Permissions)
    ;
END
于 2015-02-26T12:47:22.193 回答