Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8826187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:04:35+00:00 2026-06-14T07:04:35+00:00

Any idea why this code doesn’t work in Sql Server please? CREATE PROCEDURE sum_salaries(OUT

  • 0

Any idea why this code doesn’t work in Sql Server please?

CREATE PROCEDURE sum_salaries(OUT sum INTEGER) 
  LANGUAGE SQL
  BEGIN
    DECLARE p_sum INTEGER;
    DECLARE p_sal INTEGER;
    DECLARE c CURSOR FOR SELECT SALARY FROM EMPLOYEE;
    DECLARE SQLSTATE CHAR(5) DEFAULT '00000';

     SET p_sum = 0;

     OPEN c;

     FETCH FROM c INTO p_sal;

     WHILE(SQLSTATE = '00000') DO
        SET p_sum = p_sum + p_sal;
        FETCH FROM c INTO p_sal; 
     END WHILE;

     CLOSE c;

     SET sum = p_sum;

  END%

It gives all sorts of errors:

Msg 102, Level 15, State 1, Procedure sum_salaries, Line 3 Incorrect
syntax near ‘OUT’. Msg 155, Level 15, State 2, Procedure sum_salaries,
Line 6 ‘INTEGER’ is not a recognized CURSOR option. Msg 155, Level 15,
State 2, Procedure sum_salaries, Line 7 ‘INTEGER’ is not a recognized
CURSOR option. Msg 155, Level 15, State 2, Procedure sum_salaries,
Line 9 ‘CHAR’ is not a recognized CURSOR option. Msg 102, Level 15,
State 1, Procedure sum_salaries, Line 15 Incorrect syntax near
‘p_sal’. Msg 102, Level 15, State 1, Procedure sum_salaries, Line 17
Incorrect syntax near ‘DO’. Msg 102, Level 15, State 1, Procedure
sum_salaries, Line 19 Incorrect syntax near ‘p_sal’. Msg 102, Level
15, State 1, Procedure sum_salaries, Line 24 Incorrect syntax near
‘=’.

Alternatively if you can point me to an ample article/site that teaches the use of cursors, that would be great too. This example was taken from: http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/index.jsp?topic=%2Fcom.ibm.db2.luw.apdv.routines.doc%2Fdoc%2Fc0024361.html&resultof=%22%63%75%72%73%6f%72%22%20

Thank you very much in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:04:36+00:00Added an answer on June 14, 2026 at 7:04 am

    you don’t need cursor to do that

    create procedure sum_salaries
    (
        @sum int output
    ) 
    as
    begin
        select @sum = sum(salary) from employee
    end
    

    But if you want to use cursor, try that

    create procedure sum_salaries
    (
        @sum int output
    ) 
    as
    begin
        declare @p_sum int, @p_sal int
    
        declare c cursor for
            select salary from employee
    
        set @p_sum = 0
    
        open c
    
        fetch from c into @p_sal
    
        while @@fetch_status = 0
        begin
            set @p_sum = @p_sum + @p_sal
            fetch from c into @p_sal
        end
    
        close c
        deallocate c
    
        set @sum = @p_sum
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm unable to get this code to work. Any idea how to change that?
http://pastebin.com/hGGGUL3a Is there any idea why this code doesn't show the marker? Reason i
Any idea why this code: extern C __declspec(dllexport) void Transform(double x[], double y[], int
any idea how i can get the code below to produce this output? 1
Any idea why this won't work? I m getting tags a user has set
Has anyone got any idea how this google code works? i got the following:
string jSFile = ResolveUrl(~/MyProject/JavaScripts/dir/test.js); if (!System.IO.File.Exists(jSFile)) { ... } This code doesn't work and
Any idea why this code is failing? I'm doing the exact same thing somewhere
Any idea why this works: [[[[[self tabBarController] tabBar] items] objectAtIndex:2] setBadgeValue:@Hello]; But this doesn't?
I have problem with mutexes I have this code and I dont any idea

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.