?
Solved

easy "CASE" update ? -- Microsoft SQL Server

Posted on 2013-12-05
3
Medium Priority
?
450 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
[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 26

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 2000 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 41

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

771 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