子查询返回的值超过1。子查询遵循=,!=,<,< =,>,> =

时间:2016-04-29 10:27:25

标签: sql-server stored-procedures

我的存储过程代码:

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 #s.tax_year 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)

INSERT INTO [2290_Vehicles]
VALUES (NEWID(), @Form, 
    (SELECT vin FROM [2290_Vehicles] WHERE FK_2290_F_key = @inputkey),
    (SELECT category FROM [2290_Vehicles] WHERE FK_2290_F_key = @inputkey),
    (SELECT is_logging FROM [2290_Vehicles] WHERE FK_2290_F_key = @inputkey),
    (SELECT is_agricultural FROM [2290_Vehicles] WHERE FK_2290_F_key = @inputkey),
    (SELECT is_mileage_exceed FROM [2290_Vehicles] WHERE FK_2290_F_key = @inputkey),
    (SELECT weight_current FROM [2290_Vehicles] WHERE FK_2290_F_key = @inputkey),
    (SELECT weight_old FROM [2290_Vehicles] WHERE FK_2290_F_key = @inputkey),
    (SELECT credit_reason FROM [2290_Vehicles] WHERE FK_2290_F_key = @inputkey),
    (SELECT buyer FROM [2290_Vehicles] WHERE FK_2290_F_key = @inputkey),
    (SELECT effective_date FROM [2290_Vehicles] WHERE FK_2290_F_key = @inputkey),
    (SELECT tax_amt FROM [2290_Vehicles] WHERE FK_2290_F_key = @inputkey), GETDATE(), NULL)

SET @Formkey = @Form

我收到的错误是:

  

子查询返回的值超过1。这是不允许的   子查询跟随=,!=,<,< =,>,> =或当子查询用作   表达。

2 个答案:

答案 0 :(得分:0)

DECLARE @Form NVARCHAR(36)
SET @Form = NEWID()

INSERT INTO dbo.[2290_Form]
SELECT @Form, FK_C_B_key, is_vin_correction, ..., GETDATE(), NULL
FROM dbo.[2290_Form]
WHERE PK_2290F_key = @inputkey

INSERT INTO dbo.[2290_Vehicles]
SELECT NEWID(), @Form, vin, category, ..., tax_amt, GETDATE(), NULL
FROM dbo.[2290_Vehicles]
WHERE FK_2290_F_key = @inputkey

答案 1 :(得分:0)

2290_Vehicles的一个子查询返回多行。要找到可能的内容,请尝试以下方法:

select FK_2290_F_key
from [2290_Vehicles]
group by FK_2290_F_key
having count(FK_2290_F_key) > 1