0

i got

Msg 156, Level 15, State 1, Procedure Flag_Account, Line 21 Incorrect syntax near the keyword 'while'. Msg 156, Level 15, State 1, Procedure Flag_Account, Line 23 Incorrect syntax near the keyword 'select'.

when i try to run

    USE [hesabat2]
    GO

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    create  PROCEDURE [dbo].[Flag_Account]


    AS
begin
DECLARE @Acc_link int, @Result int,@inc int,@Result2 int
SET @inc = 0
set @Result =0
DECLARE A_Cursor CURSOR FOR
   Select account_id FROM ACCOUNTS
OPEN A_Cursor

FETCH NEXT FROM A_Cursor INTO @Acc_link

WHILE (@@FETCH_STATUS =0)
BEGIN

   set @Result= (select a.account_link from ACCOUNTS a where account_id= @Acc_link
   while(@Result = 0)
   begin
   set @Result = select a.account_link from ACCOUNTS a where account_id=@Result
   SET @inc= @inc+1
   end
    update ACCOUNTS set flag_color=@inc where account_id= @Acc_link



   FETCH NEXT FROM A_Cursor INTO  @Acc_link

   END

   CLOSE A_Cursor
   DEALLOCATE A_Cursor
   end

3 Answers 3

2

You're missing a ) on the line

set @Result= (select a.account_link from ACCOUNTS a where account_id= @Acc_link

Could be other things too, but try that first.

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

Comments

2

Two problems.

This is missing a closing paren:

  set @Result= (select a.account_link from ACCOUNTS a where account_id= @Acc_link

And this is missing opening and closing parens:

  set @Result = select a.account_link from ACCOUNTS a where account_id=@Result

Comments

0

The correct procedure is as follows:

create PROCEDURE [dbo].[Flag_Account]
AS 
begin 
DECLARE @Acc_link int, @Result int,@inc int,@Result2 int 
SET @inc = 0 
set @Result =0 
DECLARE A_Cursor CURSOR FOR Select account_id FROM ACCOUNTS 
OPEN A_Cursor
FETCH NEXT FROM A_Cursor INTO @Acc_link
WHILE @@FETCH_STATUS =0 
BEGIN
set @Result= (select a.account_link from ACCOUNTS a where account_id= @Acc_link 
while  @Result = 0
   begin 
      set @Result = select a.account_link from ACCOUNTS a where account_id=@Result 
     SET @inc= @inc+1 end update ACCOUNTS set flag_color=@inc where account_id= @Acc_link
     FETCH NEXT FROM A_Cursor INTO @Acc_link
   END
end
CLOSE A_Cursor 
DEALLOCATE A_Cursor 
end

Comments

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.