Solved

SQL server stored procedure

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL 2012 AOG and SQL2014 AOG 76 60
MSSQL join different row from other table 14 66
SQL Distinct Question 3 14
Need age at date of document 5 17
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

749 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