Must declare the scalar variable error for stored procedure in nopcommerce 4.3

0

I want to search GTIN option in admin product list. So, for that I am providing GTIN value in ProductLoadAllPaged store procedure. Now, when I search GTIN value from product list at that time throw datatable error and from console application get message that System.Data.SqlClient.SqlException (0x80131904): Must declare the scalar variable "@GTIN"..

Here is store procedure added code,

ALTER PROCEDURE [dbo].[ProductLoadAllPaged]
(
    @GTIN nvarchar(50) = null--AWAZ
)
AS
BEGIN   
    ...........
    
    --SKU (exact match)
    IF @SearchSku = 1
    BEGIN
        SET @sql = @sql + 'OR p.[Sku] = @OriginalKeywords '
    END

    --NEW ADDED CODE FOR GTIN SEARCH
    IF @GTIN is not null
    BEGIN
        SET @sql = @sql + 'AND p.Gtin = @GTIN'
    END

    --localized product name
    SET @sql = @sql + '
    UNION
    SELECT lp.EntityId
    FROM LocalizedProperty lp with (NOLOCK)
    WHERE
        lp.LocaleKeyGroup = N''Product''
        AND lp.LanguageId = ' + ISNULL(CAST(@LanguageId AS nvarchar(max)), '0') + '
        AND ( (lp.LocaleKey = N''Name'''
        
    ..........
END
sql-server
stored-procedures
nopcommerce
scalar
sql-query-store
asked on Stack Overflow Dec 14, 2020 by s.k.Soni • edited Dec 14, 2020 by s.k.Soni

1 Answer

0

In the stored procedure change this line of code:

EXEC sp_executesql @sql, N'@Keywords nvarchar(4000), @OriginalKeywords nvarchar(4000)', @Keywords, @OriginalKeywords

To this:

EXEC sp_executesql @sql, N'@Keywords nvarchar(4000), @OriginalKeywords nvarchar(4000), @GTIN nvarchar(50)', @Keywords, @OriginalKeywords, @GTIN

This will add your new parameter when executing the dynamic SQL query.

answered on Stack Overflow May 19, 2021 by SteveD

User contributions licensed under CC BY-SA 3.0