2016-05-17 54 views
0

我有沒有主鍵和外鍵表名錶被C_User_Credentials我附上截圖請到通過更多細節 enter image description here如何插入從自動生成的數據和已經創建的數據

我寫了一個存儲過程的代碼是

ALTER PROCEDURE [dbo].[usp_Duplicate] 
     @inputkey NVARCHAR(36) 
    , @Formkey NVARCHAR(36) output 
AS 
BEGIN 
    DECLARE @Form NVARCHAR(36) 
    SET @Form = NEWID() 

    SELECT * 
    INTO #s 
    FROM [2290_Form] 
    WHERE PK_2290F_key = @inputkey 

    INSERT INTO [2290_Form] 
    VALUES (@Form, (SELECT #s.FK_C_B_key FROM #s), (SELECT #s.is_vin_correction FROM #s), (SELECT #s.is_amendment FROM #s), (SELECT #s.amendment_category FROM #s), (SELECT #s.is_final_return FROM #s), (SELECT #s.first_used_month FROM #s), (SELECT DATEPART(year, GETDATE()) FROM #s), (SELECT #s.tax_amt FROM #s), (SELECT #s.additional_amt FROM #s), (SELECT #s.credit_amt FROM #s), (SELECT #s.payment_mode FROM #s), (SELECT #s.consent_tag FROM #s), (SELECT #s.status FROM #s), GETDATE(), NULL) 

    SELECT NEWID() AS Pk 
     , @Form AS fk 
     , vin 
     , category 
     , is_logging 
     , is_agricultural 
     , is_mileage_exceed 
     , weight_current 
     , weight_old 
     , credit_reason 
     , buyer 
     , effective_date 
     , tax_amt 
     , GETDATE() AS crn_dt 
     , NULL AS upd_dt 
    INTO #v 
    FROM [2290_Vehicles] 
    WHERE FK_2290_F_key = @inputkey 

    INSERT INTO [2290_Vehicles] (PK_2290_V_key, FK_2290_F_key, vin, category, is_logging, is_agricultural, is_mileage_exceed, weight_current, weight_old, credit_reason, buyer, effective_date, tax_amt, crn_dt, upd_dt) 
    SELECT * FROM #v 

    SET @Formkey = @Form 


END 

在上面的存儲過程一種形式鍵將generate.problem是生成表單關鍵我需要合併CFK_C_UP_key的幫助來自C_User_Credentials表的列和生成的form_key都在一張新表中怎麼辦請幫幫我

回答

2

我不知道你在這裏試圖做什麼,但你的程序是複雜的方式。您不需要創建臨時表來存放數據,因此您可以隨後轉向並將其插入另一個表中。另外,你已經將你的guid列定義爲nvarchar(36)。這需要一個隱式轉換。如果他們真的是guid,你應該使用正確的數據類型。

ALTER PROCEDURE [dbo].[usp_Duplicate] 
     @inputkey NVARCHAR(36) 
    , @Formkey NVARCHAR(36) output 
AS 
BEGIN 
    DECLARE @Form NVARCHAR(36) 
    SET @Form = NEWID() 

    INSERT INTO [2290_Form] 
    select @Form 
     , FK_C_B_key 
     , is_vin_correction 
     , is_amendment 
     , amendment_category 
     , is_final_return 
     , first_used_month 
     , DATEPART(year, GETDATE()) 
     , tax_amt 
     , additional_amt 
     , credit_amt 
     , payment_mode 
     , consent_tag 
     , status 
     , GETDATE() 
     ,NULL 
    from [2290_Form] 
    WHERE PK_2290F_key = @inputkey 

    INSERT INTO [2290_Vehicles] 
    (
     PK_2290_V_key 
     , FK_2290_F_key 
     , vin 
     , category 
     , is_logging 
     , is_agricultural 
     , is_mileage_exceed 
     , weight_current 
     , weight_old 
     , credit_reason 
     , buyer 
     , effective_date 
     , tax_amt 
     , crn_dt 
     , upd_dt 
    ) 

    SELECT NEWID() AS Pk 
     , @Form AS fk 
     , vin 
     , category 
     , is_logging 
     , is_agricultural 
     , is_mileage_exceed 
     , weight_current 
     , weight_old 
     , credit_reason 
     , buyer 
     , effective_date 
     , tax_amt 
     , GETDATE() AS crn_dt 
     , NULL AS upd_dt 
    FROM [2290_Vehicles] 
    WHERE FK_2290_F_key = @inputkey 
END 
相關問題