Last active
December 11, 2017 21:18
-
-
Save janosorcsik/e7abcaf8010f4903bbfefad0bd9f3a18 to your computer and use it in GitHub Desktop.
SQL WHILE sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @Temp TABLE | |
( | |
RowID INT IDENTITY (1,1), | |
InstituteID INT, | |
WorkerID INT | |
) | |
DECLARE @iMax INT, | |
@i INT | |
INSERT INTO @Temp (InstituteID, WorkerID) | |
SELECT [w].[InstituteID], [ww].[Workers_ID] | |
FROM [Workplaces] w | |
INNER JOIN [WorkplaceWorker] ww ON [w].[ID] = [ww].[Workplaces_ID] | |
WHERE [w].[WorkplaceTypeID] IN (4, 13) | |
SET @iMax = @@ROWCOUNT | |
SET @i = 1 | |
WHILE (@i <= @iMax) | |
BEGIN | |
DECLARE @InstID INT, | |
@WorkID INT | |
SELECT @InstID = InstituteID, @WorkID = WorkerID | |
FROM @Temp | |
WHERE [RowID] = @i | |
IF (@InstID = 9) | |
BEGIN | |
IF NOT EXISTS(SELECT TOP 1 * | |
FROM [WorkplaceWorker] | |
WHERE [Workplaces_ID] = 652 AND [Workers_ID] = @WorkID) | |
INSERT INTO [WorkplaceWorker]([Workers_ID],[Workplaces_ID]) | |
VALUES(@WorkID, 652) | |
END | |
SET @i = @i + 1 | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment