-1

Why does this query run alone ok but I get an error when I try to put the results in a temp table

Works

SELECT DISTINCT
ENCOUNTER_ID        = OP.PAT_ENC_CSN_ID
,NOTE_TYPE          = OP.PROC_CODE
,CREATE_TIME        = OP.ORDER_INST
,OP.ORDER_PROC_ID
,EAP.PROC_NAME
-----INTO #ECHO_ORDERS
FROM PAT_ENC E 
INNER JOIN #SER SER ON SER.PROV_ID = E.VISIT_PROV_ID
INNER JOIN ORDER_PROC OP ON OP.PAT_ENC_CSN_ID = E.PAT_ENC_CSN_ID
INNER JOIN CLARITY_EAP EAP  ON EAP.PROC_ID = OP.PROC_ID
INNER JOIN EDP_PROC_CAT_INFO EDP ON EDP.PROC_CAT_ID = EAP.PROC_CAT_ID
WHERE (EAP.PROC_NAME LIKE '%ECHO%' 
OR EDP.PROC_CAT_NAME LIKE 'ECH%')
AND EDP.PROC_CAT_NAME NOT IN ( 'CHG LABORATORY',    'LAB BLOOD ORDERABLES', 'PR GASTROENTEROLOGY',  'PR INJECTABLE MEDS',   'URINE ORDERABLES')

Doesn't Work

if object_id('tempdb..#ECHO_ORDERS') is not null  begin drop table #ECHO_ORDERS
SELECT DISTINCT
ENCOUNTER_ID        = OP.PAT_ENC_CSN_ID
,NOTE_TYPE          = OP.PROC_CODE
,CREATE_TIME        = OP.ORDER_INST
,OP.ORDER_PROC_ID
,EAP.PROC_NAME
INTO #ECHO_ORDERS
FROM PAT_ENC E 
INNER JOIN #SER SER ON SER.PROV_ID = E.VISIT_PROV_ID
INNER JOIN ORDER_PROC OP ON OP.PAT_ENC_CSN_ID = E.PAT_ENC_CSN_ID
INNER JOIN CLARITY_EAP EAP  ON EAP.PROC_ID = OP.PROC_ID
INNER JOIN EDP_PROC_CAT_INFO EDP ON EDP.PROC_CAT_ID = EAP.PROC_CAT_ID
WHERE (EAP.PROC_NAME LIKE '%ECHO%' 
OR EDP.PROC_CAT_NAME LIKE 'ECH%')
AND EDP.PROC_CAT_NAME NOT IN ( 'CHG LABORATORY',    'LAB BLOOD ORDERABLES', 'PR GASTROENTEROLOGY',  'PR INJECTABLE MEDS',   'URINE ORDERABLES')

I get this error message about the last line of code when I run the code that doesn't work

Msg 102, Level 15, State 1, Line 222
Incorrect syntax near ')'.
JHSQL
  • 11
  • 3
  • 2
    Maybe because you have a `begin` with no `end`? Or is there more to the code that you're not showing us? – Tab Alleman Feb 21 '19 at 16:23
  • I agree with Tab Alleman on this. Sql Server expects an `end` - and if the closing parenthesis is the end of the batch, that's exactly the error message I would expect. – Zohar Peled Feb 21 '19 at 16:26

1 Answers1

0

If you format your code properly, it's clear why your code doesn't work.

if object_id('tempdb..#ECHO_ORDERS') is not null  
begin 
    drop table #ECHO_ORDERS

    SELECT DISTINCT
        ENCOUNTER_ID        = OP.PAT_ENC_CSN_ID
        ,NOTE_TYPE          = OP.PROC_CODE
        ,CREATE_TIME        = OP.ORDER_INST
        ,OP.ORDER_PROC_ID
        ,EAP.PROC_NAME
    INTO #ECHO_ORDERS
    FROM PAT_ENC E 
    INNER JOIN #SER SER ON SER.PROV_ID = E.VISIT_PROV_ID
    INNER JOIN ORDER_PROC OP ON OP.PAT_ENC_CSN_ID = E.PAT_ENC_CSN_ID
    INNER JOIN CLARITY_EAP EAP  ON EAP.PROC_ID = OP.PROC_ID
    INNER JOIN EDP_PROC_CAT_INFO EDP ON EDP.PROC_CAT_ID = EAP.PROC_CAT_ID
    WHERE (EAP.PROC_NAME LIKE '%ECHO%' OR EDP.PROC_CAT_NAME LIKE 'ECH%')
        AND EDP.PROC_CAT_NAME NOT IN ( 
            'CHG LABORATORY',    'LAB BLOOD ORDERABLES', 'PR GASTROENTEROLOGY',  'PR INJECTABLE MEDS',   'URINE ORDERABLES'
        )

You have BEGIN and no END.

Eric
  • 3,165
  • 1
  • 19
  • 25
  • You are correct .if object_id('tempdb..#ECHO_ORDERS') is not null begin drop table #ECHO_ORDERS END – JHSQL Feb 21 '19 at 18:38