Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T SQL Update statement that is set based


T SQL Update statement that is set based

Author
Message
Dale Cunningham
Dale Cunningham
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 127
I have a table to update. The value to update is an FK to a PK ID on a data table.

The data table has date ranges and the table being updated has Date of Birth fields (Month, Day, Year). My Update statement could loop through all the records RBAR (row by agonizing row) But I was hoping to use a more set based solution. I've tried using the case statement and table joins in the from clause on the update statement but something about this problem is eluding me on how to approach it. Here are the the table schemas and my attempt at an update statement

Table 1 Person:
TABLE [dbo].[TFI_PERSON](
   [PERSON_ID] [int] IDENTITY(3500,1) NOT NULL,
   [HOROSCOPE_SIGN_ID] [int] NULL,
   [DOB_DAY] [int] NOT NULL,
   [DOB_MONTH] [int] NOT NULL,
   [DOB_YEAR] [int] NOT NULL,

Table 2 Horoscope

TABLE [dbo].[TFI_HOROSCOPE_SIGN](
   [HOROSCOPE_SIGN_ID] [int] IDENTITY(1,1) NOT NULL,
   [HOROSCOPE_SIGN] [nvarchar](100) NOT NULL,
   [HOROSCOPE_BEGIN_DATE] [datetime] NOT NULL,
   [HOROSCOPE_END_DATE] [datetime] NOT NULL,

Attempt(s) 1 & 2


UPDATE P
SET P.HOROSCOPE_SIGN_ID = HS.[HOROSCOPE_SIGN_ID]
            FROM dbo.TFI_PERSON AS P JOIN [dbo].[TFI_HOROSCOPE_SIGN] AS HS
               ON P.[HOROSCOPE_SIGN_ID] = HS.[HOROSCOPE_SIGN_ID]
WHERE
CAST(DOB_YEAR AS NVARCHAR)+ '-' + CAST(DOB_MONTH AS NVARCHAR) + '-' + CAST(DOB_DAY AS NVARCHAR) BETWEEN HS.[HOROSCOPE_BEGIN_DATE] AND HS.[HOROSCOPE_END_DATE]

UPDATE dbo.TFI_PERSON
SET HOROSCOPE_SIGN_ID = (SELECT HOROSCOPE_SIGN_ID
   FROM dbo.TFI_HOROSCOPE_SIGN
   WHERE CAST(CAST(DOB_YEAR AS NVARCHAR)+ '/' + CAST(DOB_MONTH AS NVARCHAR) + '/' + CAST(DOB_DAY AS NVARCHAR) AS DATETIME) BETWEEN [HOROSCOPE_BEGIN_DATE] AND [HOROSCOPE_END_DATE] )

Thanks for the assist.
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3443 Visits: 32975
does TFI_PERSON have a date of birth column stored as datetime format?

why this
[DOB_DAY] [int] NOT NULL,
[DOB_MONTH] [int] NOT NULL,
[DOB_YEAR] [int] NOT NULL,

rather than DOB (datetime) ?

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

Dale Cunningham
Dale Cunningham
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 127
No sir.

That is why you see the string concatenation of the month day and year.

It would be easier but alas it wasn't built that way.
TomThomson
TomThomson
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10647 Visits: 11981
Trying to construct the date by concatenating three casts from int to nvarchar() is likely to be somewhat error-prone, and even if you get it right (eliminating spaces, ensuring month and day parts are two characters) it will perform very badly. It's easier to get right and might perform slightly better (not much - not having the the DOB as a datetime value will be a big performance drag) if you use DATEADD to construct the date -
DATEADD(day, DOB_DAY, DATEADD(month, DOB_MONTH, DATEADD(year, DOB_YEAR, 0)))
There's no need for any casting and it avoids the implicit conversion from nvarchar to datetime if you do it like that.

Tom

Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44802 Visits: 39845
Add a calculated column to the table to create a persisted datetime column. It'll make life a lot easier.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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