Solved

SQL server stored procedure

Posted on 2014-12-10
5
98 Views
Last Modified: 2015-01-07
I have a stored procedure (below) and I get an incorrect syntax error.  The error is near the keyword 'WHERE',  I don't know what the problem is, so would appreciate any help.
Thank you.

ALTER PROCEDURE [dbo].[gt_Obs]
      @ObsID int
AS
SELECT OBS.*, NO.ItemTitle AS NumObs, PP.F_Name AS Mgr_F_name, PP.L_Name AS Mgr_L_name, PP.Grp_Name AS Mgr_Grp_Name, PP2.F_Name AS Sch_Mgr_F_Name, PP2.L_Name AS Sch_Mgr_L_Name, PP2.Grp_Name AS Sch_Mgr_Grp_Name,
    CASE OBS.IsOutside
      WHEN 1 THEN 'Yes'
      WHEN 0 THEN 'No'
      ELSE ''
    END AS Outside,
    CASE OBS.TimeObs
      WHEN 1 THEN 'AM'
      WHEN 2 THEN 'PM'
      WHEN 3 THEN 'Ot/Wknd'
    END AS TimeObsText,
    CASE OBS.IsTargeted
      WHEN 1 THEN 'Yes'
      ELSE 'No'
    END AS Targeted
FROM Obstb OBS LEFT JOIN
    RefListItem NO ON OBS.NumObID=NO.ItemID AND NO.RefListID=3 LEFT JOIN
    Pers.dbo.Person PP ON OBS.MgrID=PP.PID LEFT JOIN
        Pers.dbo.Person PP2 ON OBS.ScdMgrID=PP2.PID LEFT JOIN
WHERE OBS.ObsID=@ObsID

GO
0
Comment
Question by:newtoperlpgm
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40492805
LINE 22 ends with  LEFT JOIN

i.e. just before the WHERE there is "LEFT JOIN"

this is not allowed syntax
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40492811
I know some folks place joins at the end of lines, but I find that to be an awkward convention and strongly prefer to see each join on a new line, starting with the join. e.g. I would format your code as, see line 31 that is the problem
ALTER PROCEDURE [dbo].[gt_Obs] @ObsID int
AS
      SELECT
            OBS.*
          , NO.ItemTitle AS NUMOBS
          , PP.F_Name AS MGR_F_NAME
          , PP.L_Name AS MGR_L_NAME
          , PP.Grp_Name AS MGR_GRP_NAME
          , PP2.F_Name AS SCH_MGR_F_NAME
          , PP2.L_Name AS SCH_MGR_L_NAME
          , PP2.Grp_Name AS SCH_MGR_GRP_NAME
          , CASE OBS.IsOutside
                  WHEN 1 THEN 'Yes'
                  WHEN 0 THEN 'No'
                  ELSE ''
            END AS OUTSIDE
          , CASE OBS.TimeObs
                  WHEN 1 THEN 'AM'
                  WHEN 2 THEN 'PM'
                  WHEN 3 THEN 'Ot/Wknd'
            END AS TIMEOBSTEXT
          , CASE OBS.IsTargeted
                  WHEN 1 THEN 'Yes'
                  ELSE 'No'
            END AS TARGETED
      FROM Obstb OBS
            LEFT JOIN RefListItem NO ON OBS.NumObID = NO.ItemID
            AND NO.RefListID = 3
            LEFT JOIN Pers.dbo.Person PP ON OBS.MgrID = PP.PID
            LEFT JOIN Pers.dbo.Person PP2 ON OBS.ScdMgrID = PP2.PID
          --LEFT JOIN
      WHERE OBS.ObsID = @ObsID

GO

Open in new window

0
 
LVL 18

Expert Comment

by:SimonAdept
ID: 40492816
You have an incomplete JOIN

       Pers.dbo.Person PP2 ON OBS.ScdMgrID=PP2.PID LEFT JOIN -- something is missing here!!!
WHERE OBS.ObsID=@ObsID
0
 

Author Comment

by:newtoperlpgm
ID: 40494194
Thank you so much!  I am an Oracle developer, so struggle with correct syntax in SQL Server.  The LEFT JOINS that were on the right hand side of the code misled me.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now