views:

36

answers:

1

i am getting the error as shown below :

Msg 156, Level 15, State 1, Procedure Sp_Table1, Line 22
Incorrect syntax near the keyword 'AS'.

while executing this SQL:

WITH myCTE AS
(Select mci.* from
view_name AS si
JOIN merch_catalog_ipt_view_name AS mci
  ON mci.view_id = si.view_id
  AND mci.resolved_view_name_id = si.view_name_id
  AND mci.ctg_ipt_event_id = @ctg_ipt_event_id
  AND mci.accept_flag = 'y')

UPDATE view_name
SET name = (select mci.name from myCTE where myCTE.view_id = view_id)
,view_name_ctg_id = (select mci.resolved_view_name_ctg_id from myCTE where myCTE.view_id = view_id)
,xref_value = (select mci.xref_value from myCTE where myCTE.view_id = view_id)
,availability_start_date = (select mci.availability_start_date from myCTE where myCTE.view_id = view_id)
,availability_end_date = (select mci.availability_end_date from myCTE where myCTE.view_id = view_id)
,status_code = (select mci.status_code from myCTE where myCTE.view_id = view_id)
,last_modified_user_id = (select CASE WHEN mci.last_modified_user_id = 42 THEN @posting_user_id ELSE mci.last_modified_user_id END from myCTE where myCTE.view_id = view_id)
,last_modified_timestamp = CURRENT_TIMESTAMP
WHERE si.shi_flag = 'n'
and exists (select view_id from merch_catalog_ipt_view_name AS mci
  Where mci.view_id = view_name.view_id
  AND mci.resolved_view_name_id = view_name.view_name_id
  AND mci.ctg_ipt_event_id = @ctg_ipt_event_id
  AND mci.accept_flag = 'y')

Please help

+4  A: 

Try putting a semicolon ; just before your WITH, as

When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon.

This has caught me out in the past.

AakashM
Now i am getting this error ..previous error has gone:The multi-part identifier "si.shi_flag" could not be bound.
@SmartestVEGA ok, so now at least it's *syntactically* correct. The problem you have now is that `si` as a name only has meaning *within* the CTE - the main query doesn't have anything by that name. I'm not sure whether that criterion should be in the CTE definition on in each subquery (or something else)...
AakashM
Could u please change the query?
@SmartestVEGA I think to actually work out what's wrong we'll need a new question with full table definitions and what you want the output to be - looking at what you've got here, I don't think a piecemeal approach to fixing it will help.
AakashM
ok i ll do it now!