Functions - How to use Stored Proc input/output params from SQL input binding

Elliot Stoner 5 Reputation points
2025-02-13T20:28:45.0633333+00:00

I have a function that calls a Stored Procedure to create a record in my BD. The stored proc has one input parameter and one output parameter:

CREATE PROCEDURE dbo.CREATE_SEASON
@Name varchar(50),
@Season_ID INT OUTPUT
AS 
BEGIN
UPDATE dbo.Seasons SET Is_Active = 0 WHERE Seasons.Is_Active = 1;
INSERT INTO dbo.Seasons (NAME, IS_ACTIVE) values (@Name, 1);
SELECT @Season_ID = SCOPE_IDENTITY();
END

I've set up my Function to utilize the SQL input binding, but I'm trying to figure out how to:

  1. Pass in the @Name input parameter (this example shows it automatically binding to the URL parameter). I will need to pass the input parameter from the code (retrieved from the HTTP body or calculated from logic in the Function code).
  2. Retrieve the value from the @Season_ID output parameter.

Here is the code that I have so far:

import { app, HttpRequest, HttpResponseInit, InvocationContext, input } from "@azure/functions";

const sqlOptions = input.sql({
    commandText: 'dbo.CREATE_SEASON',
    commandType: 'StoredProcedure',
    connectionStringSetting: 'SqlConnectionString',
    parameters: '@Name={calculatedName},@Season_ID={seasonId}'
});

export async function CreateSeason(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
    context.log(`Http function processed request for url "${request.url}"`);

    const name = request.query.get('name') || await request.text() || 'world';
    // calculatedName is the value that I need passed into the stored proc
    const calculatedName = name + '_Temp';

    // seasonId is the value that I need returned from the stored proc output variable
    let seasonId: number;

    context.extraInputs.get(sqlOptions);

    return { body: JSON.stringify({ seasonId }) };
};

app.http('CreateSeason', {
    methods: ['POST'],
    route: 'seasons',
    extraInputs: [sqlOptions],
    authLevel: 'anonymous',
    handler: CreateSeason
});

Currently, I get the error System.Private.CoreLib: Exception while executing function: Functions.CreateSeason. Microsoft.Azure.WebJobs.Host: No value for named parameter 'calculatedName'., but if I change "calculatedName" to "name" in the parameters option (parameters: '@Name={name},@Season_ID={seasonId}), it moves on to an error for seasonId. This makes me think that it's automatically trying to bind the "calculatedName" property to the queryParams, but it's hard to tell (I can't find documentation on it).

Azure Functions
Azure Functions

An Azure service that provides an event-driven serverless compute platform.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    7 deleted comments

    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.