我有一个表没有主键,外键表名是 C_User_Credentials 我附加了屏幕截图请详细了解
我写了一个存储过程代码是
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
在上面的存储过程中,一个表单键将生成.problem是在生成表单键的帮助下我需要合并来自C_User_Credentials表的 CFK_C_UP_key 列和生成的form_key 两者一张新表怎么做请帮帮我
答案 0 :(得分: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