SQL Server 2008 R2 存储过程中带有子 IF 语句的 WHILE 条件?
WHILE condition with sub IF statements in SQL Server 2008 R2 stored procedure?
我在 SQL Server 2008 R2 的存储过程中得到了以下代码。
..
@currentAccID char(21),
@currentUserID char(21),
@UserID1 char(21),
@UserID2 char(21),
@UserID3 char(21),
@Case smallint,
...
SELECT @UserID1 = UserID1, @UserID2 = UserID2, @UserID3 = UserID3 FROM OLD_Users WHERE oldAccID = @currentAccID
WHILE (@userid1 = @userid or @userid2 = @userid or @userid3 = @userid)
BEGIN
IF (@Case = 10 OR @Case = 20)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_NormalUser'
END
ELSE IF (@Case = 12 OR @Case = 22)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_MediumUser'
END
ELSE IF (@Case = 13 OR @Case = 23)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_HighUser'
END
ELSE IF (@Case = 14 OR @Case = 24)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_UltraUser'
END
END
WHILE (@userid1 != @userid or @userid2 != @userid or @userid3 != @userid)
BEGIN
IF (@Case = 10 OR @Case = 20)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'NormalUser'
END
ELSE IF (@Case = 12 OR @Case = 22)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'MediumUser'
END
ELSE IF (@Case = 13 OR @Case = 23)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'HighUser'
END
ELSE IF (@Case = 14 OR @Case = 24)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'UltraUser'
END
END
...
INSERT INTO NewUser (UserID,Case,Equipment ......) VALUES (@UserID,@Case,@Equipment .......)
以上代码是否正确?此外,还有第三种可能性,其中 @currentAccID
与 OLD_Users
table 中的 oldAccID
与 SELECT
查询完全不匹配,但它应该没问题因为第二个 WHILE
语句将被执行,对吗?性能不应该有问题吗?我很确定使用 WHILE
语句等可以做得更好,但我不擅长 SQL 存储过程。预先感谢您花时间帮助我!
那么你不应该使用 while
循环。它可以通过简单的 if-else
语句来实现。请使用以下代码执行相同的操作:
..
@currentAccID char(21),
@currentUserID char(21),
@UserID1 char(21),
@UserID2 char(21),
@UserID3 char(21),
@Case smallint,
...
-- this condition checks whether @userid is present in any of the columns UseID1 or UserID2 or UserID3
IF EXISTS(SELECT NULL FROM OLD_Users WHERE oldAccID = @currentAccID AND (UserID1 = @userid OR UserID2 = @userid OR UserID3 = @userid))
BEGIN
SET @OldORNew = 1
IF (@Case = 10 OR @Case = 20)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_NormalUser'
END
ELSE IF (@Case = 12 OR @Case = 22)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_MediumUser'
END
ELSE IF (@Case = 13 OR @Case = 23)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_HighUser'
END
ELSE IF (@Case = 14 OR @Case = 24)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_UltraUser'
END
END
ELSE
BEGIN
SET @OldORNew = 0
IF (@Case = 10 OR @Case = 20)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'NormalUser'
END
ELSE IF (@Case = 12 OR @Case = 22)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'MediumUser'
END
ELSE IF (@Case = 13 OR @Case = 23)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'HighUser'
END
ELSE IF (@Case = 14 OR @Case = 24)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'UltraUser'
END
END
...
INSERT INTO NewUser (UserID,Case,Equipment ......) VALUES (@UserID,@Case,@Equipment .......)
我在 SQL Server 2008 R2 的存储过程中得到了以下代码。
..
@currentAccID char(21),
@currentUserID char(21),
@UserID1 char(21),
@UserID2 char(21),
@UserID3 char(21),
@Case smallint,
...
SELECT @UserID1 = UserID1, @UserID2 = UserID2, @UserID3 = UserID3 FROM OLD_Users WHERE oldAccID = @currentAccID
WHILE (@userid1 = @userid or @userid2 = @userid or @userid3 = @userid)
BEGIN
IF (@Case = 10 OR @Case = 20)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_NormalUser'
END
ELSE IF (@Case = 12 OR @Case = 22)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_MediumUser'
END
ELSE IF (@Case = 13 OR @Case = 23)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_HighUser'
END
ELSE IF (@Case = 14 OR @Case = 24)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_UltraUser'
END
END
WHILE (@userid1 != @userid or @userid2 != @userid or @userid3 != @userid)
BEGIN
IF (@Case = 10 OR @Case = 20)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'NormalUser'
END
ELSE IF (@Case = 12 OR @Case = 22)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'MediumUser'
END
ELSE IF (@Case = 13 OR @Case = 23)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'HighUser'
END
ELSE IF (@Case = 14 OR @Case = 24)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'UltraUser'
END
END
...
INSERT INTO NewUser (UserID,Case,Equipment ......) VALUES (@UserID,@Case,@Equipment .......)
以上代码是否正确?此外,还有第三种可能性,其中 @currentAccID
与 OLD_Users
table 中的 oldAccID
与 SELECT
查询完全不匹配,但它应该没问题因为第二个 WHILE
语句将被执行,对吗?性能不应该有问题吗?我很确定使用 WHILE
语句等可以做得更好,但我不擅长 SQL 存储过程。预先感谢您花时间帮助我!
那么你不应该使用 while
循环。它可以通过简单的 if-else
语句来实现。请使用以下代码执行相同的操作:
..
@currentAccID char(21),
@currentUserID char(21),
@UserID1 char(21),
@UserID2 char(21),
@UserID3 char(21),
@Case smallint,
...
-- this condition checks whether @userid is present in any of the columns UseID1 or UserID2 or UserID3
IF EXISTS(SELECT NULL FROM OLD_Users WHERE oldAccID = @currentAccID AND (UserID1 = @userid OR UserID2 = @userid OR UserID3 = @userid))
BEGIN
SET @OldORNew = 1
IF (@Case = 10 OR @Case = 20)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_NormalUser'
END
ELSE IF (@Case = 12 OR @Case = 22)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_MediumUser'
END
ELSE IF (@Case = 13 OR @Case = 23)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_HighUser'
END
ELSE IF (@Case = 14 OR @Case = 24)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'OLD_UltraUser'
END
END
ELSE
BEGIN
SET @OldORNew = 0
IF (@Case = 10 OR @Case = 20)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'NormalUser'
END
ELSE IF (@Case = 12 OR @Case = 22)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'MediumUser'
END
ELSE IF (@Case = 13 OR @Case = 23)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'HighUser'
END
ELSE IF (@Case = 14 OR @Case = 24)
BEGIN
SELECT TOP 1 @Equipment = Equipment FROM Users WHERE UserID = 'UltraUser'
END
END
...
INSERT INTO NewUser (UserID,Case,Equipment ......) VALUES (@UserID,@Case,@Equipment .......)