1

I am new in using loop in SQL Server. I just wanted to ask why my code is not working? I am trying to have if statements within a loop and it will continue to process until it gets what it wanted. Thanks for any response!

DECLARE @SubjectCategoryID bigint
DECLARE @ParentID bigint
DECLARE @EntityID bigint
DECLARE @isLocation int
DECLARE @tempTable TABLE (ParentID bigint, isLocation int) 
DECLARE @projectCodesTable TABLE (Contingency nvarchar(max), Provincial nvarchar(max), HQAdmin nvarchar(max))    
DECLARE @count int

Select @SubjectCategoryID = SubjectCategoryID, @EntityID = EntityID  from t_Project WHERE Code = '1000296'
SET @count = 0
SET @isLocation = 0

WHILE (@isLocation = 1)
BEGIN
    DELETE FROM @tempTable
    IF @SubjectCategoryID = 150     -- Village
    BEGIN
        INSERT INTO @tempTable
            SELECT CommunityID, IsLocation from t_Loc_Village WHERE VillageID = @EntityID

        Select @EntityID = ParentID, @isLocation = isLocation From @tempTable
        SET @SubjectCategoryID = 140
    END
    ELSE IF @SubjectCategoryID = 140    --- Community
    BEGIN
        INSERT INTO @tempTable
            SELECT CityTownID, IsLocation from t_Loc_Community WHERE CommunityID = @EntityID

        Select @EntityID = ParentID, @isLocation = isLocation From @tempTable
        SET @SubjectCategoryID = 135
    END
    ELSE IF @SubjectCategoryID = 135    --- City/Town
    BEGIN
        INSERT INTO @tempTable
            SELECT ProvinceID, IsLocation from t_Loc_CityTown WHERE CityTownID = @EntityID

        Select @EntityID = ParentID, @isLocation = isLocation From @tempTable
        SET @SubjectCategoryID = 130
    END
    ELSE IF @SubjectCategoryID = 130    --- Province
    BEGIN
        INSERT INTO @tempTable
            SELECT RegionalOfficeID, IsLocation from t_Loc_Province WHERE ProvinceID = @EntityID

        Select @EntityID = ParentID, @isLocation = isLocation From @tempTable
        SET @SubjectCategoryID = 210
    END
    ELSE IF @SubjectCategoryID = 210    --- Regional Office
    BEGIN
        INSERT INTO @tempTable
            SELECT CountryID, IsLocation from t_RegionalOffice WHERE RegionalOfficeID = @EntityID

        Select @EntityID = ParentID, @isLocation = isLocation From @tempTable
        SET @SubjectCategoryID = 100
    END
    ELSE IF @SubjectCategoryID = 100    --- Country
    BEGIN
        INSERT INTO @tempTable
            SELECT 0, IsLocation from t_Loc_Country WHERE CountryID = @EntityID

        Select @EntityID = ParentID, @isLocation = isLocation From @tempTable
        SET @SubjectCategoryID = 0
    END
END

Select * from @tempTable
4
  • Could you provide some example data from each of your tables: t_Loc_Village etc? Commented Aug 14, 2013 at 7:40
  • Where is @isLocation getting value of 1 for the control to go inside the WHILE loop? Commented Aug 14, 2013 at 7:43
  • @ShaneHaw, all those tables has LocationID, ParentID and isLocation columns. When I found the data in a particular table using the LocationID, the corresponding ParentID would be the new LocationID (@entity value) and will be used to search it to other tables.. Its like hierarchichal level., I am finding the location till it reaches the location where isLocation = 1. Commented Aug 14, 2013 at 8:06
  • @BijuP, I am setting the value of isLocation between each if and else if statements.. Commented Aug 14, 2013 at 8:08

1 Answer 1

2

Your code will not enter the while loop because you have set isLocation=0 in the beginning and checking while(isLocation=1). Try changing your while loop logic. Use GOTO or simulate a do while loop. Please refer:Do while loop in SQL Server 2008 for further information.

Sign up to request clarification or add additional context in comments.

4 Comments

I think I set the isLocation = 0 before the while loop. See my code for reference.
Exactly the reason why your loop won't start execution. If you set isLocation=0 and set while(isLocation=1), it'll find isLocation as 1 and not execute the loop.
Sorry but may I ask if the condition inside while(condition) is the condition wherein it says that continue the loop till it gets isLocation = 1? Am I right?
Yup. It means that till the loop finds isLocation to be 1, it should run the loop.

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.