Link to home
Start Free TrialLog in
Avatar of chicho12
chicho12

asked on

SQL - Comparing the value returned by a Select against a Parameter in a Store Procedure

Hi
IN the Store Procedure below Im trying to compare the value returned by the Select statement against the parameter @SalesRepFullName.  I am always getting a  'NotMatch'   even when the value of the  parameter passed is the same as what the Select should be returning.  Can someone let me know what am I missing?   THanks

ALTER procedure [LFO].[testSP]
@SalesRepFullName varchar(100),          
@ContractNr nvarchar(50),
@StatusFlag varchar(4000) OUT
AS          
       
BEGIN            
BEGIN TRY    
    DECLARE @FullName varchar(100);
    SELECT @FullName = LTRIM(RTRIM([dbo].Account.SalesRepFullName))      
        FROM [dbo].Account
        WHERE [dbo].Account.ContractNr = @ContractNr
      IF @FullName = LTRIM(RTRIM(@SalesRepFullName))              
             select @StatusFlag = 'Match'  
      ELSE           
           select @StatusFlag = 'NotMatch'  
           
END TRY          
BEGIN CATCH            
 select @StatusFlag = ERROR_MESSAGE()      
END CATCH          
END
Avatar of dsacker
dsacker
Flag of United States of America image

I'm curious whether the ContractNr/SalesRepFullName are both matching. Taking it from trying to match the SalesrepFullName, are you able to return anything if you run this query in the proc?

SELECT LTRIM(RTRIM(Account.SalesRepFullName)),
       Account.ContractNr,
       @ContractNr AS ParmContractNr
FROM   Account
WHERE  LTRIM(RTRIM(Account.SalesRepFullName)) = LTRIM(RTRIM(@SalesRepFullName)

Open in new window

Avatar of chicho12
chicho12

ASKER

yes, it returned values.

i just added this line below in the ELSE section and its updating the expected row.  So its finding the record but some how the comparison of values is never returning true.
I am testing the Stored pROcedure in SQL server management.

Update [dbo].Account set SalesRepFullName =LTRIM(RTRIM(@SalesRepFullName)) ,UpdatedOn = getdate() where ContractNr = @ContractNr
ASKER CERTIFIED SOLUTION
Avatar of dsacker
dsacker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You were right the contract had a character a the end that didnt see before.  THanks for the help!!