Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Condtional Update

Author  Topic 

MichaelP
Jedi Yak

2489 Posts

Posted - 2003-04-23 : 12:13:49
I searched the forum, but I could not find what I was looking for. I know I've seen it on here before. What I want to do is if "IsRecurring = 1" then calculate the NextExecution, and update the Next Execution field. I have the calculation stuff figured out. If IsRecurring = 0 then I want to set IsDisabled = 1.

I know I can do this with some If statements etc, but I think there's a way to do this in a single UPDATE statement and maybe make it easier to read and understand.

Here is what I have so far:

CREATE TABLE [#Task] (
[TaskID] [int] IDENTITY (1, 1) NOT NULL ,
[TaskName] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[TaskInterval] [varchar] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[IntervalUnit] [int] NOT NULL ,
[NextExecution] [datetime] NULL ,
[LastExecution] [datetime] NULL ,
[IsDisabled] [bit] NOT NULL CONSTRAINT [DF_Task_IsDisabled] DEFAULT (0),
[IsRecurring] [bit] NOT NULL CONSTRAINT [DF_Task_IsRecurring] DEFAULT (1),
[HasErrored] [bit] NOT NULL CONSTRAINT [DF_Task_HasErrored] DEFAULT (0),
[ErrorText] [varchar] (7000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Cre_Date] [datetime] NOT NULL CONSTRAINT [DF_Task_Cre_Date] DEFAULT (getdate()),
[Cng_Date] [datetime] NOT NULL CONSTRAINT [DF_Task_Cng_Date] DEFAULT (getdate()),
)
INSERT INTO #Task(TaskName, TaskInterval, IntervalUnit, NextExecution, IsRecurring)
values('One Time Task', 'dd', 0, '4/23/2003', 0)

DECLARE @TaskID INT

SELECT @TaskID = @@IDENTITY

select * from task

--START PSUDEO CODE
UPDATE TASK
WHEN IsRecurring = 1 THEN SET NextExecution = '5/1/2003', Cng_Date = GETDATE()
WHEN IsRecurring = 0 THEN SET IsDisabled = 1, Cng_Date = GetDate()
WHERE TaskID = @TaskID
--END PSUDEO CODE

select * from task

drop table #task



Michael


<Yoda>Use the Search page you must. Find the answer you will.</Yoda>

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-04-23 : 12:19:02
something like:


UPDATE TASK SET
NextExecution = CASE WHEN IsRecurring = 1 THEN '5/1/2003' ELSE NextExecution END,
Cng_Date = GetDate(),
IsDisabled = CASE WHEN IsRecurring = 0 THEN 1 ELSE IsDisabled END,
WHERE TaskID = @TaskID


- Jeff
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2003-04-23 : 12:26:08
Yep, that was perfect man!!

Thanks!@!

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page
   

- Advertisement -