Search code examples
sql-serverssmsssms-2017

Declare Scalar Variable error in SQL - SSMS


When I execute the below SQL statement, I get an error

Must declare the scalar variable "@AuditKey"

I feel like this is so simple and I am making a really dumb mistake. Thanks in advance!

ALTER PROCEDURE [ProjectSpecific].[spTarget]
    -- Parameter is AuditKey
    @Key VARCHAR(100)
AS
BEGIN
    SET @Key = 'Key'
    SET NOCOUNT ON;

    CREATE TABLE #Audit
    (   
        AccountID INT,
        Acct VARCHAR(50),
        RevCode SMALLINT,
        HCPCS VARCHAR(5)
    )

    INSERT INTO #Audit
        SELECT    
            II.AccountID, II.Acct, IB.RevCode, IB.HCPCS
        FROM 
            Trakker.InsuranceInfo II
        INNER JOIN
            Trakker.ItemizedBill IB ON II.AccountID = IB.AccountID
        WHERE 
            Rate.Key = @Key
END

Solution

  • Should be:

    DECLARE @Key varchar(100)