Solved

easy "CASE" update ? -- Microsoft SQL Server

Posted on 2013-12-05
3
434 Views
Last Modified: 2013-12-19
How can I default "Test #2"
to "set DR = ZONE picked + 001"
if no value already exists in MAINT_WORK_REQ ?

Maybe something like the below "Test #3" ?
----------------------------------------------------------------------------
Test #1
 1. user creates new MAINT_WORK_REQ database record,
    selecting ActivitySequence = 100030670 and ZONE = 4
 2. below code works since there is already an
    ActivitySequence = 100030670 and ZONE = 4
    in the database

  update MAINT_WORK_REQ
  set DR =
  (
    select MAX(DR) + 1
    from MAINT_WORK_REQ
    where ActivitySequence = '100030670'
    and ZONE = 4
   )
----------------------------------------------------------------------------
Test #2
 1. user creates another new MAINT_WORK_REQ database record,
    selecting ActivitySequence = 100030685 and ZONE = 3
 2. below code fails since there is "NOT" already an
    ActivitySequence = 100030685 and ZONE = 3
    in the database
 3. need it to do "set DR = 3001"
    since nothing already exists

  update MAINT_WORK_REQ
  set DR =
  (
    select MAX(DR) + 1
    from MAINT_WORK_REQ
    where IFSActivitySequence = '100030685'
    and ZONE = 3
   )
----------------------------------------------------------------------------
Test #3
  update MAINT_WORK_REQ
  set DR =
  (
    case when
      select MAX(DR)
      from MAINT_WORK_REQ
      where ActivitySequence = '100030670'
      and ZONE = 4 > 0
    then
      select MAX(DR) + 1
      from MAINT_WORK_REQ
      where ActivitySequence = '100030670'
    else
      ZONE + 001
   )
0
Comment
Question by:finance_teacher
3 Comments
 
LVL 25

Expert Comment

by:Shaun Kline
ID: 39698136
Try this:
update MAINT_WORK_REQ
  set DR =
  (
    select ISNULL(MAX(DR), <ZONE value queried> * 1000) + 1
    from MAINT_WORK_REQ
    where IFSActivitySequence = '100030685'
    and ZONE = 3
   )

Open in new window

0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 39699238
I added a where-clause to the update because I don't think you want to update every row in MAINT_WORK_REQ:
update	MAINT_WORK_REQ
set	DR =	ISNULL(	(	select	MAX(DR) + 1
				from	MAINT_WORK_REQ
				where	ActivitySequence = '100030685'
				and	ZONE = 3
			) , ZONE + 001)
where	ActivitySequence = '100030685'
and	ZONE = 3

Open in new window

Note: this will still alter other rows that match the where-clause (i.e., this is not a single-row update).  You'd be better off enhancing the insert-statement to populate DR, instead of issuing an insert followed by an update.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 39725444
Provide some sample data from your table and how exactly you want to update the DR.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now