Solved

Change a field value within Update Select

Posted on 2008-06-17
3
715 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL trigger 5 25
Need return values from a stored procedure 8 23
changing page verifacation 1 33
Help with my first SQL CLR table value function 2 13
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

821 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