Link to home
Start Free TrialLog in
Avatar of Jeff S
Jeff SFlag for United States of America

asked on

Incorrect syntax near the keyword 'ON'.

Msg 156, Level 15, State 1, Line 44
Incorrect syntax near the keyword 'ON'.

Not sure how to resolve this error. I am trying to join in the #Tmp.PatientProfileId. Any help is appreciated.
SET NOCOUNT ON

CREATE TABLE #Tmp ( PatientProfileId INT ) 
	
INSERT  INTO #Tmp
        SELECT DISTINCT
                PatientProfileID
        FROM    PatientProfile pp
                JOIN Orders o ON pp.PID = o.PID
                JOIN Orddx ON o.dxgroupid = orddx.dxgroupid
                JOIN DOCUMENT d ON o.SDID = d.SDID
        WHERE   ( orddx.dxCode LIKE ( 'icd-250.%' )
                  OR orddx.dxCode LIKE ( 'icd-251.%' )
                )
                AND o.XID = 1000000000000000000
                AND ( d.Change IS NULL
                      OR d.Change NOT IN ( 10, 11, 12 )
                    )
                    
SELECT  *
FROM    ( SELECT    *,
                    ROW_NUMBER() OVER ( PARTITION BY PatientId ORDER BY obsdate DESC ) rn
          FROM      ( SELECT    o.pid,
                                pp.PatientId,
                                pp.PatientProfileId,
                                dbo.FORMATNAME('', pp.FIRST, pp.Middle,
                                               pp.LAST, pp.Suffix) AS PatientName,
                                MAX(o.OBSDate) AS OBSDate,
                                o.OBSVALUE
                      FROM      OBS AS o
                                JOIN OBSHEAD AS oh ON o.HDID = oh.HDID
                                JOIN PatientProfile AS pp ON o.PID = pp.PId
                      WHERE     o.HDID = 28
                                AND ( o.State IS NULL
                                      OR o.STATE NOT IN ( 'D', 'R', 'I', 'P',
                                                          'S' )
                                    )
                      GROUP BY  o.PID,
                                pp.PatientId,
                                pp.PatientProfileId,
                                dbo.FORMATNAME('', pp.FIRST, pp.Middle,
                                               pp.LAST, pp.Suffix),
                                o.OBSVALUE
                    ) t1 ON #tmp.PatientProfileId = t1.PatientProfileId 
        ) t2 
WHERE   rn = 1
ORDER BY PatientName               
                    
SELECT  *
FROM    #Tmp                   
 
DROP TABLE #Tmp

Open in new window

Avatar of JoeNuvo
JoeNuvo
Flag of Viet Nam image

                    ) t1 ON #tmp.PatientProfileId = t1.PatientProfileId 

Open in new window


I guess you are forget to put #tmp to join with t1
so, keyword ON is error there.
SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial