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)
 Multiserver update query

Author  Topic 

eldroch
Starting Member

1 Post

Posted - 2012-06-30 : 00:56:35
Okay, I'm a little stumped on how to (efficiently) do this. I need to update a table, setting a date field to a "Date of previous visit" that basically looks at a record, takes a "Date of Service", and finds the patient's most recent Date of Service that occured prior to it. The first issue I'm having is the error:
"hsd.ITSVCDAT could not be bound" which I understand is tied to the HAVING clause. Commenting this out (for testing) causes it to spin for a really really long time...I finally stopped it after about an hour of running and there are only 81 records to update. I tried writing his using joins instead of a subquery, but it still takes forever.

It seems like a pretty straightforward query...could the fact that most of the tables reside on a different server than the one I'm connected to? The code is pasted below. Please help!



UPDATE hsd
SET
hsd.LASTSEENDT = lsd.LastSeenDate
FROM
Demographics hsd
INNER JOIN
(
SELECT MAX(ds.act_date) As LastSeenDate,
pt.PtMrn
FROM dim_patient pt
INNER JOIN
fact_transac ft ON pt.PtPK = ft.TrnFKPtPk
INNER JOIN
dim_date ds ON ft.TrnServiceDtId = ds.date_id
INNER JOIN
fact_sched fs ON pt.PtPK = fs.SchFkPtPk AND ds.date_id = fs.SchDateId
GROUP BY pt.PtMrn
HAVING MAX(ds.act_date) < hsd.ITSVCDAT
) lsd
ON hsd.MRN = lsd.PtMrn

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-01 : 00:45:41
see scenario 2

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html



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

Go to Top of Page

namman
Constraint Violating Yak Guru

285 Posts

Posted - 2012-07-01 : 22:19:07
You have to use linked server to access data in other servers different from the connection's one
Go to Top of Page
   

- Advertisement -