SQL Server 2016 - 当IF语句中的WHILE循环仍未在条件未满足时发生

时间:2018-01-11 23:43:49

标签: sql-server if-statement while-loop nested duplicates

this question开始,我使用以下代码将3行数据插入表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])

结果如下,是我想要的:

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

然后我决定插入第二组数据:我希望John Smith的人也拥有成功结果的Code3。所以我再次运行代码,只需更改[Codename]的{​​{1}}列,而不是更改值' 03 - Code3'。我想要的结果是:

@data

在上面的场景中,只更新了一行。但是,当我运行代码时,我会为每个代码获得一组新的行:

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

似乎IF语句的条件+----+----------------+--------+--------+ | 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 | +----+----------------+--------+--------+ 被忽略了,但我不确定原因。我尝试在WHILE语句周围放置括号,以便它只在满足上述条件时运行,但SQL Server认为括号在语法上是错误的。

1 个答案:

答案 0 :(得分:1)

你需要使用BEGIN ... END作为IF条件

如果有什么东西 BEGIN

...

END

否则IF仅适用于IF条件

之后的第一个语句