Q
Q
Qairat2017-03-10 06:43:23
SQL Server
Qairat, 2017-03-10 06:43:23

How to create a procedure in Ms Sql?

Hello!
Please see the code:

alter PROCEDURE SUMOFNUMBERS
  @FirstNumber INT,
  @SecondNumber INT,
  @Answer INT OUT
AS
  SET @Answer = @FirstNumber + @SecondNumber;
RETURN @Answer;
GO

exec dbo.SUMOFNUMBERS(5,6);
There is an error: Incorrect syntax near the construction "5".

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2017-03-10
@Qairat

In your answer, you already wrote how to use the procedure correctly.
If you need to call like "dbo.SUMOFNUMBERS(5,6)". Use a function.

CREATE FUNCTION SUMOFNUMBERS 
(
  @FirstNumber INT,
  @SecondNumber INT
)
RETURNS int
AS
BEGIN
  -- Declare the return variable here
  DECLARE @Answer int

  -- Add the T-SQL statements to compute the return value here
  SET @Answer = @FirstNumber + @SecondNumber;

  -- Return the result of the function
  RETURN @Answer
END

We use it: select dbo.SUMOFNUMBERS(5,6)
But if the soul asks for a procedure, I can offer this option:
CREATE PROCEDURE SUMOFNUMBERS
  @FirstNumber INT,
  @SecondNumber INT,
AS
BEGIN
  SET @Answer = @FirstNumber + @SecondNumber;
  RETURN @Answer;
END



EXEC     @Answer =  SUMOFNUMBERS 5, 6

Q
Qairat, 2017-03-10
@Qairat

Found this:

DECLARE @Answer int

EXEC SUMOFNUMBERS 
    @FirstNumber = 5,
    @SecondNumber = 6,
    @Answer = @Answer OUTPUT
    
SELECT @Answer

Works.
What should be used like this?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question