Solved

easy "CASE" update ? -- Microsoft SQL Server

Posted on 2013-12-05
3
438 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 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 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

839 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