2

In the below code i have pass multiple values with comma Separated to @i_CustomerGroupID and one value to @i_LocationID.In which i face a issue "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.".Pls help me to solve the issue.

ALTER PROCEDURE [dbo].[spInsertCustomerGroupLocationMap]
    -- Add the parameters for the stored procedure here
    @i_LocationID int,
    @i_CustomerGroupID varchar(100)

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

    -- Insert statements for procedure here
    IF NOT EXISTS (SELECT 1 FROM CustomerGroupLocationMap WHERE LocationID = @i_LocationID AND CustomerGroupID = @i_CustomerGroupID)
    BEGIN

        INSERT INTO CustomerGroupLocationMap (LocationID, CustomerGroupID) VALUES (@i_LocationID, (SELECT * FROM dbo.CSVToTable(@i_CustomerGroupID)));

    END
END

3 Answers 3

2

You want to use insert . . . select:

    INSERT INTO CustomerGroupLocationMap(LocationID, CustomerGroupID) 
        SELECT @i_LocationID, t.*
        FROM dbo.CSVToTable(@i_CustomerGroupID) t;

Your function dbo.CSVToTable() returns more than one value (I assume there is only one column). The correct syntax is the insert . . . select.

As a note, insert . . . values is really not needed. You can use insert . . . select even when you only have constants.

Sign up to request clarification or add additional context in comments.

Comments

0
IF NOT EXISTS (SELECT TOP 1 FROM CustomerGroupLocationMap WHERE LocationID = @i_LocationID AND CustomerGroupID = @i_CustomerGroupID)
BEGIN

    INSERT INTO CustomerGroupLocationMap (LocationID, CustomerGroupID) VALUES (@i_LocationID, (SELECT * FROM dbo.CSVToTable(@i_CustomerGroupID)));

END

Comments

0

You would need to rewrite both your exists query and the insert query-

IF NOT EXISTS (SELECT 1 FROM CustomerGroupLocationMap WHERE LocationID = @i_LocationID AND CustomerGroupID IN (SELECT *
    FROM dbo.CSVToTable(@i_CustomerGroupID) ))
BEGIN

    INSERT INTO CustomerGroupLocationMap (LocationID, CustomerGroupID) 
    SELECT @i_LocationID, i.* FROM dbo.CSVToTable(@i_CustomerGroupID) i;

END

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.