SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to script an conditional update


how to script an conditional update

Author
Message
Edvard Korsbæk
Edvard Korsbæk
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1257 Visits: 369
Hi all clever scripters out there!

I am going to make a one time update after having made a new function.

I have one table

[dbo].[savedduty](
[NAME] [varchar](20) NULL,
[STARTTIME] [int] NULL,
[DAYS] [int] NULL,
[ENDTIME] [int] NULL,
[DAEKBEMAND] [tinyint] NULL,
[EMPLOYEEGROUPID] [int] NULL,
[ID] [int] IDENTITY(1,1) NOT NULL,
[Selected_Record] [tinyint] NULL,
[Name_Day_2] [char](20) NULL,
[InstitutionsId] [int] NULL,
[Color] [int] NULL,
[Color_R] [tinyint] NULL,
[Color_B] [tinyint] NULL,
[Color_G] [tinyint] NULL,

The important part here is [NAME] and [ID], which is the two components i should use to update this table:

[dbo].[dutyrostershift](
[DATO] [datetime] NULL,
[STD] [tinyint] NULL,
[SPECIALVAGT] [tinyint] NULL,
[DAEKBEMAND] [tinyint] NULL,
[EXTRATIMER] [real] NULL,
[OVERARBTIMER] [real] NULL,
[MANUEL] [tinyint] NULL,
[BESKYTTET] [tinyint] NULL,
[CONFIRMED] [tinyint] NULL,
[VACATIONTYPE] [varchar](50) NULL,
[BREAKSWISH] [tinyint] NULL,
[DUTYROSTERID] [int] NULL,
[EMPLOYEEID] [int] NULL,
[EMPLOYEEGROUPID] [int] NULL,
[CHILDFORCAREDAYID] [int] NULL,
[ORIGINATINGSTAFFINGREQUIREMENTID] [int] NULL,
[SHIFTTYPE] [int] NULL,
[FROMTIME] [int] NULL,
[TOTIME] [int] NULL,
[ID] [int] IDENTITY(1,1) NOT NULL,
[LeaveType_Id] [int] NULL,
[LoginID] [int] NULL,
[StatusNo] [int] NULL,
[Time_Stamp] [datetime] NULL,
[Comment] [char](120) NULL,
[Is_Free_sat] [tinyint] NULL,
[Is_Center_Opening] [tinyint] NULL,
[is_fo_day] [tinyint] NULL,
[SavedDuty_Id] [int] NULL,

The rule is Pretty simple:

If dbo.dutyrostershift.vacationtype = dbo.savedduty.name then dbo.dutyrostershift.savedduty_id = dbo.savedduty.id.
i.e. if vacationtype exists in name from savedduty, update saveddutyroster_id with ID from savedduty

But, how to script this?

Best regards

Edvard Korsbæk
Ed Wagner
Ed Wagner
SSC-Forever
SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)

Group: General Forum Members
Points: 49431 Visits: 10844
Is this what you're looking for?

update dutyrostershift
set dutyrostershift.savedduty_id = savedduty.id
from savedduty
where dutyrostershift.vacationtype = savedduty.name;



One thing worth pointing out is that savedduty.name is a varchar(20) and dutyrostershift.vacationtype is a varchar(50), meaning that one field could have values longer than the other and won't be updated. The above update statement will perform correctly, but it might miss some because the values can't match if dutyrostershift.vacationtype is longer than 20.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Edvard Korsbæk
Edvard Korsbæk
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1257 Visits: 369
Thanks!

so easy - I learned that its a good idea to use [table name].[FieldName] in queries here.

Thanks for that too.

Best regards

Edvard Korsbæk
Ed Wagner
Ed Wagner
SSC-Forever
SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)SSC-Forever (49K reputation)

Group: General Forum Members
Points: 49431 Visits: 10844
No problem. Glad I could help.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search