Solved

Change a field value within Update Select

Posted on 2008-06-17
3
721 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

623 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