存储过程更新工作正常,但未插入数据

时间:2015-08-12 10:51:11

标签: asp.net sql-server-2008

USE [Database]
GO    
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [INF_SP_GET_INSERTFIELDS2]  
    @Menu_Id INT= NULL,  
    @Created_By  int,
    @dtInsert INF_UDTT_Menu_Key_Rights2 Readonly 
As  
BEGIN  
    Declare @dtTR_Local INF_UDTT_Menu_Key_Rights2  

    INSERT INTO @dtTR_Local  
       SELECT 
           isnull(DTL.Key_ID,0) as pkid,
           dtc.Key_Name, dtc.Description, 
           dtc.Module_Id, dtc.Key_Enabled, dtc.Menu_Id 
       FROM 
           @dtInsert dtc  
       LEFT JOIN 
           INF_LIB_Module_Keys DTL ON dtc.PKID = DTL.Key_ID 
                                    AND DTL.Menu_Id = @Menu_Id  

    SET IDENTITY_INSERT INF_LIB_Module_Keys ON

    UPDATE TBL1 
    SET TBL1.pkid = (SELECT ISNULL(MAX(pkid), 0) 
                     FROM @dtTR_Local) + ROWNUM 
    FROM
        (SELECT 
             ROW_NUMBER() OVER (ORDER BY TBL.pkid) ROWNUM, * 
         FROM @dtTR_Local TBL 
         WHERE pkid = 0) TBL1  

    MERGE INF_LIB_Module_Keys TRG  
    USING (SELECT * FROM @dtTR_Local) SRC ON SRC.PKID = TRG.Key_Id 

    WHEN MATCHED THEN  
       UPDATE 
           SET TRG.Module_Id = SRC.Module_Id,
               TRG.Key_Name = SRC.Key_Name,
               TRG.Description = SRC.Description,
               TRG.Key_enabled = SRC.Key_enabled

    WHEN NOT MATCHED THEN  
       INSERT (Key_Id, Key_Name, Description, Module_Id, Key_enabled, Menu_Id)  
       VALUES ((SELECT ISNULL(MAX(Key_Id), 0) 
                FROM INF_LIB_Module_Keys) + SRC.pkid, SRC.Key_Name,
               SRC.Description, SRC.Module_Id, SRC.Key_enabled, SRC.Menu_Id);     

    select @Menu_Id   
END  

1 个答案:

答案 0 :(得分:-1)

您似乎正在插入主要ID(pkid),但是没有为INF_UDTT_Menu_Key_Rights2打开Indentity_Insert?