我在存储SQL过程中的语法有问题

时间:2018-06-25 16:22:28

标签: sql sql-server

我试图在存储过程中添加三列(我在存储过程中没有很多经验),这些列是Family ID,Address和PhoneNo。尽管我已经运行了查询并确认我的select语句的语法正确,当我将该语句添加到存储的proc时,出现以下错误:'capPrograms'附近的语法不正确(这将是第14行)。我知道这可能与我试图在更新程序的过程中执行我的语句有关,但是我不确定由于尝试了一些具有类似或更广泛错误的更改而导致的问题。感谢您的协助,我在下面提供了我正在使用的代码块:

SELECT  capCase.Id AS capCase_Id,
            capCase.DateApplied AS capCase_DateApplied,
            CASE 
                WHEN ISNUMERIC(#cases.origCaseYear)=1 THEN CAST(origCaseYear + 2004 AS VARCHAR) + ' - ' + CAST(capCase.IdYear + 2004 AS VARCHAR)
                WHEN #cases.maxRolloverYear IS NOT NULL THEN CAST(capCase.IdYear + 2004 AS VARCHAR) + ' - ' + CAST(#cases.MaxRolloverYear + 2004 AS VARCHAR)
                ELSE CAST(capCase.IdYear + 2004 AS VARCHAR) 
            END AS capCase_IdYear,
      Person.id as Person_id,
            Person.LastName + ', ' + Person.FirstName AS PersonPrint_Name,
person.idFamily AS Family_ID, person.homePhone AS Phone_No,  (SELECT family.physicalAddress1+ ', ' + family. physicalAddressCity+ ' ' +family. physicalAddressZip) AS Address
 FROM Family
LEFT JOIN person ON family.Id = person.idFamily;

capPrograms.Program AS capPrograms_Program,
            capStatus.Status AS capStatus_Status,
            #lastFollowup.followupDate AS capCaseFollowup_Date,
            CASE
                WHEN capCase.IdCaseWorker IS NULL THEN 'No Worker Assigned'
                ELSE caseWorker.LastName + ', ' + caseWorker.FirstName + ISNULL('<' + caseWorker.EmailWork + '>', '')
            END AS capCase_IdCaseWorker, 
            capcaseDenialReason.reason AS capCase_idDenialReason, 

1 个答案:

答案 0 :(得分:0)

评论太久,因此我在您的代码中添加了很多评论。您有很多问题。

SELECT  
    capCase.Id AS capCase_Id,
    capCase.DateApplied AS capCase_DateApplied,
    CASE 
        --here you are referencing a temp table, which isn't joined below
        WHEN ISNUMERIC(#cases.origCaseYear)=1 THEN CAST(origCaseYear + 2004 AS VARCHAR) + ' - ' + CAST(capCase.IdYear + 2004 AS VARCHAR)
        WHEN #cases.maxRolloverYear IS NOT NULL THEN CAST(capCase.IdYear + 2004 AS VARCHAR) + ' - ' + CAST(#cases.MaxRolloverYear + 2004 AS VARCHAR)
        ELSE CAST(capCase.IdYear + 2004 AS VARCHAR) 
    END AS capCase_IdYear,
    Person.id as Person_id,
    Person.LastName + ', ' + Person.FirstName AS PersonPrint_Name,
    person.idFamily AS Family_ID, 
    person.homePhone AS Phone_No,  

    --this subquery as no from clause... that's an issue... you probably want to remove it completly and replace it with the one below
    (SELECT family.physicalAddress1+ ', ' + family. physicalAddressCity+ ' ' +family. physicalAddressZip) AS Address,
    --this is likely what you want
    family.physicalAddress1 + ', ' + family. physicalAddressCity + ' ' + family. physicalAddressZip AS AddressCorrected

FROM Family
LEFT JOIN person ON 
family.Id = person.idFamily;   --you have a ; here, which terminates the statement. Everything else below isn't included

--Here you need to join the capPrograms and #lastFollowup table..

--here, you listed some columns, without a table reference. You need to join the table above, and move these columns above the FROM clause
            capPrograms.Program AS capPrograms_Program,
            capStatus.Status AS capStatus_Status,
            #lastFollowup.followupDate AS capCaseFollowup_Date,
            CASE
                WHEN capCase.IdCaseWorker IS NULL THEN 'No Worker Assigned'
                ELSE caseWorker.LastName + ', ' + caseWorker.FirstName + ISNULL('<' + caseWorker.EmailWork + '>', '')
            END AS capCase_IdCaseWorker, 
            capcaseDenialReason.reason AS capCase_idDenialReason,