Solved

Change a field value within Update Select

Posted on 2008-06-17
3
708 Views
Last Modified: 2012-08-13
I'm wondering if I can combine the following two queries to run as one query.  The first query is an Insert/select:  The second query is an update based on one of the fields being inserted.  The field is StatusCode.  It gets inserted with an initial value of 1 but then it needs to be changed based on the values of some other fields in the query.  Is it possible to do this all in one query?  

----- 1st query

insert into FileOne
    (CNumber,
    ID,
    DCode,
    PCode,
    b.Indicator,
    b.Discount,
    StatusCode)
SELECT
    cast(a.CNumber as char (5)),
    cast(a.ID as varchar (16)),
    cast(a.DCode as varchar (5)),
    b.PCode,
    b.Indicator,
    b.Discount,
    1
FROM PFILE a
    INNER JOIN TFile b
        ON a.ID = b.ID
WHERE SUBSTRING(b.CNumber,1,1)<>'S'

---- 2nd query

UPDATE FileOne
SET StatusCode = 0
WHERE
 (indicator = 0
OR Pcode <> 'D'
OR Dcode <>'C'
OR discount = 1)
0
Comment
Question by:dkcoop03
3 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21805737
No, you cannot.  In 2008, a MERGE statement can do something similar to what you're doing, but for right now you have to use seperate statements....INSERT and UPDATE are distinct operations.
0
 
LVL 13

Accepted Solution

by:
rickchild earned 250 total points
ID: 21806293
Try this:
insert into FileOne
    (CNumber,
    ID,
    DCode,
    PCode,
    b.Indicator,
    b.Discount,
    StatusCode) 
SELECT
    cast(a.CNumber as char (5)),
    cast(a.ID as varchar (16)),
    cast(a.DCode as varchar (5)),
    b.PCode,
    b.Indicator,
    b.Discount,
    CASE
        WHEN (b.indicator = 0 OR b.Pcode <> 'D' OR cast(a.Dcode as varchar(5)) <>'C' OR discount = 1) THEN 0
    ELSE 1
    END as StatusCode
FROM PFILE a
    INNER JOIN TFile b
        ON a.ID = b.ID
WHERE SUBSTRING(b.CNumber,1,1)<>'S'

Open in new window

0
 

Author Closing Comment

by:dkcoop03
ID: 31468072
Thanks, that works great!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question