0

I am trying to insert data into a table using a stored procedure with one parameter. The parameter is looking for the name of a club. I want the SP to find all the players that belong to the club and insert them into the ClubNameTable. The problem is when I run the SP it inserts all the data from the join into the table, not just when the case statement is true.

CREATE PROCEDURE SSIS.usp_ClubNameTable 
    -- Add the parameters for the stored procedure here
    @NameOfClub nvarchar(200)
AS
BEGIN
    TRUNCATE TABLE SSIS.ClubNameTable;
    insert into ssis.ClubNameTable ([NameOfClub], [FirstName], [LastName])
        SELECT
            p.FirstName,
            COUNT(p.LastName) as 'Number of Last Names',                
            CASE
                WHEN @NameOfClub = 'Eagle Plate' THEN NameOfClub
                WHEN @NameOfClub = 'Atlanta United FC' THEN NameOfClub
                    ELSE 'Please choose either Eagle Plate or Atlanta United FC'
                end
    FROM [Location].Club as C
            JOIN [Location].ClubDetails as CD on c.ClubID = cd.ClubID
            JOIN [Player].Player as P on CD.PlayerID = P.PlayerID
            GROUP BY c.NameOfClub, p.FirstName, p.LastName
            ORDER BY p.LastName ASC
        declare 
        @RowCount int
            SET @RowCount = (SELECT COUNT(*) FROM Location.Club)
END
GO
6
  • 2
    Please provide the tables schemas and the SP code. Commented Mar 17, 2019 at 20:09
  • It is very hard to tell what the issue is without actually seeing the code of the stored procedure, sample data and expected output, would you please provide those? Commented Mar 17, 2019 at 20:10
  • 1
    Sorry I hit enter without putting the code on, just edited it. Anything else to make it more clear? Commented Mar 17, 2019 at 20:11
  • Don't copy data around between the tables. Each player should be stored only once in the database. Commented Mar 17, 2019 at 20:31
  • This looks like business logic. Would it be easier to do this logic prior to sending it to sql? Commented Mar 17, 2019 at 21:21

1 Answer 1

3

Using case statement on a column does not do any filtering on records, it just applies conditions on the value that is to be assigned to the column. In order to filter records you need to use a where clause with the desired conditions:

 CREATE PROCEDURE SSIS.usp_ClubNameTable 
    -- Add the parameters for the stored procedure here
    @NameOfClub nvarchar(200)
AS
BEGIN
    TRUNCATE TABLE SSIS.ClubNameTable;
    if(@NameOfClub = 'Eagle Plate' or @NameOfClub = 'Atlanta United FC')
    begin
    insert into ssis.ClubNameTable ([NameOfClub], [FirstName], [LastName])
        SELECT
            p.FirstName,
            COUNT(p.LastName) as 'Number of Last Names',                
            NameOfClub
    FROM [Location].Club as C
            JOIN [Location].ClubDetails as CD on c.ClubID = cd.ClubID
            JOIN [Player].Player as P on CD.PlayerID = P.PlayerID
            Where C.NameOfClub = @NameOfClub
            GROUP BY c.NameOfClub, p.FirstName, p.LastName
            ORDER BY p.LastName ASC
        declare 
        @RowCount int
            SET @RowCount = (SELECT COUNT(*) FROM Location.Club)
    end

    else
    begin
    select 'Please choose either Eagle Plate or Atlanta United FC'
    end
END
GO
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but this doesn't seem to work for me.When i run this SP, it doesnt matter what i enter for the parameter, the target table always contains a mixture of both clubs.

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.