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 2008 Forums
 Transact-SQL (2008)
 Query Help

Author  Topic 

tooba
Posting Yak Master

224 Posts

Posted - 2013-02-07 : 19:59:18
Here is my sample data table

Table Name = Test
ID,DTypeID,MinDuration,MaxDuration,UpdateField
1,123,0,12
2,345,6,18
3,111,12,24

Here is my SP (Inside SP)
-- I am update above table

Update Test
Set UpdateField = Right
from Test
where MinDuration < 12


Update Test
Set UpdateField = Left
From Test
Where MinDuration >= 12


My Question is :- I want when i run above store procedure if First Update Complete don't go to second update (For E.g if First Creteria meet the
requirement and update "UpdateField to Right" don't go to Next Update Statement.

Any help would be great appreciate.

Thank You.

tooba
Posting Yak Master

224 Posts

Posted - 2013-02-07 : 21:18:48
I change the Query Little bit...

Table Name = Test (Here is my main Table)
ID,DTypeID,MinDuration,MaxDuration,UpdateField
1,123,0,12
2,345,6,18
3,111,12,24

Here is my SP (Inside SP)
-- I am update above table
Select * into #Temp1 from Test

Update #Temp1
Set UpdateField = Low
from Test
where MinDuration = 12


Update #Temp1
Set UpdateField = High
From Test
Where MinDuration >= 12

The End Result Its Depend (Let say its meet first Update requirement) then End result should be
Select * from #Temp1
ID,DTypeID,MinDuration,MaxDuration,UpdateField
1,123,0,12, Low
2,345,6,18
3,111,12,24

If first Update statement doesn't meed the requirement then End result should be

Select * from #Temp1
ID,DTypeID,MinDuration,MaxDuration,UpdateField
1,123,0,12
2,345,6,18, High
3,111,12,24, High

Thank You....
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-07 : 23:09:35
you sample output and logic doesnt match.
your logic looks for value of MinDuration but output shows as if you're considering Maxdurtaion ( see second case, you've no records with MinDuration > 12 and still it shows two rows with High. those rows have MaxDuration > 12 but even in that case i would expect first row also to be High as it has value as 12 and your condition reads >=12).
Can you please clearly restate your requirements?

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

Go to Top of Page

tooba
Posting Yak Master

224 Posts

Posted - 2013-02-08 : 01:00:55
Visakh Thanks for Response. Here is correct Syntax

Update #Temp1
Set UpdateField = Low
from Test
where MinDuration = 12


Update #Temp1
Set UpdateField = High
From Test
Where MinDuration > 12

The End Result Its Depend (Let say its meet first Update requirement) then End result should be
Select * from #Temp1
ID,DTypeID,MinDuration,MaxDuration,UpdateField
1,123,0,12, Low
2,345,6,18
3,111,12,24

If first Update statement doesn't meed the requirement then End result should be

Select * from #Temp1
ID,DTypeID,MinDuration,MaxDuration,UpdateField
1,123,0,12
2,345,6,18, High
3,111,12,24, High


Visakh for Above sample data i can use

Update #Temp1
Set UpdateField = case when MinDuration < 12 then Low else High end
from #Temp1

But i am looking something else. How about
if i use

IF UpdateField is null or IF UpdateField = 0
begin
First Update Statement
end
Else
IF UpdateField = = 0
begin
My Second Update Statement
end

Note:- In actual data i have more then two fields to update. Does this sounds right to you?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-08 : 03:37:02
why do you need to write separate update statements?

so far as filters are same you can accomodate them in same update itself

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

Go to Top of Page

tooba
Posting Yak Master

224 Posts

Posted - 2013-02-08 : 11:50:55
The Reason why i need it because i want to use atleast 12 Update Statements. If one of the criteria match update that column and get out from the loop.

Any advise?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-08 : 23:06:06
quote:
Originally posted by tooba

The Reason why i need it because i want to use atleast 12 Update Statements. If one of the criteria match update that column and get out from the loop.

Any advise?



as far as i can see the only criteria is based on MinDuration column.
Do you mean you've other column also to determine criteria.

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

Go to Top of Page

tooba
Posting Yak Master

224 Posts

Posted - 2013-02-10 : 00:30:31
Yes i have others column to, In real it could be more then 12 Update statements. Any advice?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-02-11 : 00:25:23
in that you should use a set of if else constructs to do update based on your criteria

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

Go to Top of Page
   

- Advertisement -