jeudi 11 janvier 2018

SQL Server 2016 - WHILE loop inside IF statement still occurring when condition has not been met

Following up from this question, I have used the below code to insert 3 rows of data into table PeopleStatusCodes.

--declare temporary tables
DECLARE @peopleStatus TABLE (peopleStatusID INT)
DECLARE @data TABLE (FirstName VARCHAR (100), LastName VARCHAR (100), Codename VARCHAR (100))

--insert data into @data
INSERT INTO @data(
    [FirstName]
   ,[LastName]
   ,[Codename]
)
VALUES(
    'John'
   ,'Smith'
   ,'02 - Code2'
)

--check if entry exists inside PeopleStatus and insert into @peopleStatus based on that
IF NOT EXISTS (SELECT [ps].[PersonCode] FROM PeopleStatus [ps], People [p], @data [d]
WHERE [ps].[PersonCode] = [p].[PersonCode]
AND [p].[FirstName] = [d].[FirstName]
AND [p].[LastName] = [d].[LastName])
    INSERT INTO PeopleStatus (
           [PersonCode]
          ,[Status]
    )
    OUTPUT inserted.[ID]
    INTO @peopleStatus
    SELECT
           [p].[PersonCode]
          ,1
    FROM [People] [p], @data [d]
    WHERE [p].[FirstName] = [d].[FirstName]
      AND [p].[LastName] = [d].[LastName]
ELSE INSERT INTO @peopleStatus (peopleStatusID)
SELECT [ps].[ID]
FROM PeopleStatus [ps], People [p], @data [d]
WHERE [ps].[PersonCode] = [p].[PersonCode]
AND [p].[FirstName] = [d].[FirstName]
AND [p].[LastName] = [d].[LastName]

--insert into PeopleStatusCodes a row of data for each Code in the Codes table with Result defaulting to 0
IF NOT EXISTS (SELECT [psc].ID
FROM PeopleStatusCodes [psc], @peopleStatus [temp]
WHERE [psc].PeopleStatusID = [temp].peopleStatusID)

    DECLARE @IDColumn VARCHAR (10)
    SELECT @IDColumn = MIN(c.ID)
    FROM Codes [c]
    WHILE @IDColumn IS NOT NULL
    BEGIN
        INSERT INTO [dbo].[PeopleStatusCodes] (
             [PeopleStatusID]
            ,[CodeID]
            ,[Result]
        )
        SELECT
             [temp].[peopleStatusID]
            ,@IDColumn
            ,0
        FROM @peopleStatus [temp]

        SELECT @IDColumn = MIN(c.ID)
        FROM Codes [c]
        WHERE c.ID > @IDColumn
    END

--Checks if the data matching row has not had Result changed to 1 already, and if so, update that row.
IF NOT EXISTS (SELECT [psc].ID
FROM PeopleStatusCodes [psc], @peopleStatus [temp]
WHERE [psc].PeopleStatusID = [temp].peopleStatusID 
AND [psc].CodeID = (SELECT [c].ID FROM Codes [c], @data [d] WHERE [c].CodeNumber + ' - ' + [c].Name = [d].[Codename])
AND [psc].Result = 1)
UPDATE [dbo].[PeopleStatusCodes] SET Result = 1 WHERE CodeID = (SELECT [c].ID FROM Codes [c], @data [d] WHERE [c].CodeNumber + ' - ' + [c].Name = [d].[Codename])

The results are as follows and are what I want:

+----+----------------+--------+--------+
| ID | PeopleStatusID | CodeID | Result |
+----+----------------+--------+--------+
|  1 |              1 |      1 |      0 |
|  2 |              1 |      2 |      1 |
|  3 |              1 |      3 |      0 |
+----+----------------+--------+--------+

I then decide to insert a second set of data: I want person John Smith to also have Code3 with a successful result. So I run the code again, and just change the the [Codename] column of @data to instead have the value '03 - Code3'. The result I want is:

+----+----------------+--------+--------+
| ID | PeopleStatusID | CodeID | Result |
+----+----------------+--------+--------+
|  1 |              1 |      1 |      0 |
|  2 |              1 |      2 |      1 |
|  3 |              1 |      3 |      1 |
+----+----------------+--------+--------+

In the above scenario, only one row has been updated. However, when I run the code, instead I get a new set of rows per Code:

+----+----------------+--------+--------+
| ID | PeopleStatusID | CodeID | Result |
+----+----------------+--------+--------+
|  1 |              1 |      1 |      0 |
|  2 |              1 |      2 |      1 |
|  3 |              1 |      3 |      0 |
|  4 |              1 |      1 |      0 |
|  5 |              1 |      2 |      0 |
|  6 |              1 |      3 |      1 |
+----+----------------+--------+--------+

It seems like the IF statement's condition SELECT [psc].ID FROM PeopleStatusCodes [psc], @peopleStatus [temp] WHERE [psc].PeopleStatusID = [temp].peopleStatusID is being ignored, but I'm not sure why. I tried putting brackets around the WHILE statement so it only runs if the above condition is met, but SQL Server considers the brackets as syntactically wrong.

Aucun commentaire:

Enregistrer un commentaire