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
 General SQL Server Forums
 New to SQL Server Programming
 stored procedure

Author  Topic 

anjali5
Posting Yak Master

121 Posts

Posted - 2011-11-04 : 11:08:45
I have two table Timeline table and reports table

Timeline table has this data

Timeline UNIT Date number closing date CNumber 2 lastMessageDate Last Code

2010-03-01 00:00:00.000 21623 201001080710 5 2010-04-01 10:47:00.000 201345 2010-02-25 08:27:00.000 2 9
2010-03-01 00:00:00.000 45679 200907030907 9 2010-04-22 13:55:00.000 2009026 2010-02-25 09:19:00.000 2 10



Reports

Code UNIT DATE Number UpateNumber
3 45679 2010-04-22 13:55:00.000 9 121
5 123456 2010-04-22 13:55:00.000 8 131

I want to write something like this


if convert(varchar(10),Date,111) = convert(varchar(10),closing date,111)and convert(varchar(10),Date,111)= Timeline
BEGIN
UPDATE Timeline
set Code = (select max(Code)
from reports r
where r.unit = timeline.unit
and r.date = timeline.date
and (r.updateNumber = 121 or r.updateNumber = 101)
)

where r.unit = Timeline.unit and r.date = timeline.date and r.number = timeline.number

else if closing date is null or closing date > dateadd(hour,23, dateadd(minute ,59, CONVERT(datetime,CONVERT(VARCHAR(23),Timeline, 112) ,112)))

-- I have two update statements here



I solved this using cursore, but I was wondering if there is any way, i can eliminate cursors.

Thanks in advance.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-04 : 12:43:30
if all updates are on same table you can reduce them to single update with case expressions within each column and eliminate cursor altogether

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -