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
Solved

SQL server stored procedure

Posted on 2014-12-10
5
116 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: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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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