Solved

SQL server stored procedure

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS 2013 - Overlapping reports 2 20
T-SQL: Do I need CLUSTERED here? 13 41
MS SQL + Insert Into Table - If Doesnt Exist 9 33
Sql Query 6 64
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

776 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