We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Problem with sql case statement

rutledgj
rutledgj asked
on
Medium Priority
239 Views
Last Modified: 2012-06-21
Can someone show me the proper way to use the case statement in this example?

EXEC MedscribeSQL.dbo.usp_PatientDemographicsUpdate @FName, @MName, @LName, @DOB, @SSNum, @SEX, @HPhone,
                                                                                                CASE      
                                                                                                      WHEN @CreditCode = null THEN @OriginalCreditCode
                                                                                                      ELSE @CreditCode
                                                                                                END as CreditCode,
                                                                    @Org, @ChartNum, @Dec, @Addr1, @Addr2, @City, @State, @Zip, @Race, 1,
                                                                    @EthnicityId,@SBLocationId,@PhysId,0,null,'ROBOT1',null,1,
                                                                                                @BillType,@AccountNo,@Alert;


For creditcode I'm basically wanting to use the originalvalue if the new value is null.

The editor says incorrect syntax near CASE   and same for AS at the end of the CASE statement.

Is there a better way to do this perhaps?
Comment
Watch Question

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT

Commented:
You will need to do the CASE first, then pass in the result as a parameter:

declare @parmCreditCode varchar(80)
select  @parmCreditCode = CASE    
                                               WHEN @CreditCode = null THEN @OriginalCreditCode
                                               ELSE @CreditCode
                                       END --as CreditCode,

EXEC MedscribeSQL.dbo.usp_PatientDemographicsUpdate @FName, @MName, @LName, @DOB, @SSNum, @SEX, @HPhone,
        @parmCreditCode,
        @Org, @ChartNum, @Dec, @Addr1, @Addr2, @City, @State, @Zip, @Race, 1,
        @EthnicityId,@SBLocationId,@PhysId,0,null,'ROBOT1',null,1,
        @BillType,@AccountNo,@Alert;
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.