Newbie Needs Help with Storeed Proc

  • Good Afternoon,

    I have a very large SQL 2k database with three tables in it.  In each table, I have a field PayorGroup and a separate field PlanCode.

    I need to write a series of if..then...else statements to evaluate the PlanCode field and insert a value in the PayorGroup field dependent upon the PlanCode value. 

    For example, if PlanCode between C01 and C99 then PayorGroup = Cash Pay

    My question: (remember I'm a newbie:laugh

    Can a stored procedure handle this, and if so, what kind of syntax needs to be used?

    Thanks, in advance, for your help

    Sid

  • Only use a stored procedure for actions which you'll need to perform multiple times. If this is a one-time thing, then you don't need to create a procedure for it.

    What you're going to want to do is do an update query and use a CASE statement (look in SQL Server Books OnLine for documentation). It would look something like this:

    UPDATE myTable

    SET PayorGroup =

    CASE

    WHEN PlanCode BETWEEN 'C01' and C99' THEN 'Cash Pay'

    WHEN PlanCode BETWEEN 'Something' and 'SomethingElse' THEN 'Some Other Value'

    ....

    Be careful, however using BETWEEN with character data. You'll probably want to do some research on how SQL Server sorts character data.

    Good luck!

  • Aaron,

    This worked!  Thanks very much for your help!!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply