Must declare scalar variable in sql procedure

11,362

It seems that you have confused things by first posting a piece of code that gives the error Msg 137, Level 15, State 2, Line 11 Must declare the scalar variable "@branch". and then later adding a complete procedure that gives the error Msg 156, Level 15, State 1, Procedure insertion, Line 13 Incorrect syntax near the keyword 'from'.

Please make sure that you post the real code you're using and the full error message too, otherwise people cannot help you.

Anyway, I ignored the code snippet and looked only at the procedure and as ABFORCE said, the problem is where you populate @result because your syntax is wrong. This procedure code parses without error in SQL Server 2008:

   CREATE PROCEDURE insertion @id varchar(50), @brch varchar(50)
    -- Add the parameters for the stored procedure here

   AS
    BEGIN
   DECLARE  @result int 
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    select @result  = COUNT(*) from populate

    if (@result > 1)
    Begin
        insert into populate (brch, terminal_id) values(@id, @brch)
    end
    END
    GO

You might want to review the documentation for assigning values to variables and the SET keyword.

Share:
11,362

Related videos on Youtube

mayowa ogundele
Author by

mayowa ogundele

I am currently a software developer in one of the leading Banks in Africa with experience in MVC, Entity Framework, LINQ, SQL Server, ASP.Net and other web technologies.

Updated on September 26, 2022

Comments

  • mayowa ogundele
    mayowa ogundele over 1 year

    Please what is wrong with the procedure statement below

        DECLARE  @result int 
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        -- Insert statements for procedure here
        set @result = (select COUNT(*) from populate)
    
        if (@result > 1)
        Begin
            insert into populate (brch, terminal_id) values(@branch, @atmid)
        end
    

        SET ANSI_NULLS ON
        GO
         SET QUOTED_IDENTIFIER ON
        GO
    
        CREATE PROCEDURE insertion @id varchar(50), @brch varchar(50)
        -- Add the parameters for the stored procedure here
    
       AS
        BEGIN
       DECLARE  @result int 
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        -- Insert statements for procedure here
        set @result  = (COUNT(*) from populate)
    
        if (@result > 1)
        Begin
            insert into populate (brch, terminal_id) values(@id, @brch)
        end
        END
        GO
    
    • ErikE
      ErikE over 11 years
      Do not get a Count(*) just to find out if there is more than one row! Do SELECT Count(*) FROM (SELECT TOP 2 * FROM Populate). Also, you haven't posted enough of the procedure--if truly a stored procedure, where is the CREATE PROCEDURE statement?
  • mayowa ogundele
    mayowa ogundele over 11 years
    IT gives the error Msg 156, Level 15, State 1, Procedure populate, Line 17 Incorrect syntax near the keyword 'from'.
  • SWeko
    SWeko over 11 years
    We need way more information to give a more meaningful answer. Please edit the question, and give information about the schema of the data, about what the procedure does, about error messages etc. The SO community is great for solving problems, but still the problems must be correctly described.
  • SWeko
    SWeko over 11 years
    No, you did not :) What is populate?