Solved

SQL server stored procedure

Posted on 2014-12-10
5
120 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 49

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 49

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:Simon
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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

691 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