?
Solved

easy "CASE" update ? -- Microsoft SQL Server

Posted on 2013-12-05
3
Medium Priority
?
462 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 27

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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…
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

809 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