简体   繁体   中英

C# & SQL Server stored procedure - Must Declare The Scalar Variable @SellerLocationID

My first attempt at creating a stored procedure to be run from C# has produced the following result.

However, on invocation, I get the error

Must declare scalar @SellerLocationID

I have tried including the statement

declare @SellerLocationID int = 0;

Both within and outside of the stored procedure.

I receive the same error in either case.

What do I need to do to resolve this error?

C# (Invoking Code)

 var sqlUserName = new SqlParameter("@SellerLocationID", SellerLocationID);

var results = unitofwork.DBContext().Database
                                .SqlQuery<dynamic>
                                ("Exec GetClientOrdersForSupplier @SellerLocationID", SellerLocationID).ToList();

T-SQL:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetClientOrdersForSupplier]
    @SellerLocationID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT 
        l.LocationName, u.UserID, u.FirstName, u.LastName, 
        MAX(o.OrderSubmittedDate) as LastOrderPlaced
    FROM
        sw.Locations l
    JOIN 
        sw.Users u ON u.locationid = l.locationid 
    LEFT JOIN
        sw.locationproviders lp ON u.LocationID = lp.BuyerLocationID 
    LEFT JOIN
        sw.orders o ON o.CreatedUserID = u.UserID
    WHERE
        lp.sellerlocationid = @SellerLocationID
    GROUP BY 
        l.LocationName, u.UserID, u.FirstName, u.LastName
    ORDER BY
        UserID
END

The call to .Query needs a SqlParameter object when you are specifying a named parameter:

var sqlUserName = new SqlParameter("@SellerLocationID", SellerLocationID);

var results = unitofwork.DBContext().Database
                                .SqlQuery<dynamic>
                                ("Exec GetClientOrdersForSupplier @SellerLocationID", sqlUserName).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM