Solved

CTE and IF

Posted on 2008-06-10
2
1,006 Views
Last Modified: 2012-08-13
Hi Experts!

I have the following SQL and it error out on:

Incorrect syntax near the keyword 'IF'.

Declare @memid as varchar(13)
    set @memid  = 'QH23455909187';

WITH MEM_CTE AS
(
  SELECT memid
  FROM tbl01 EK WITH(NOLOCK)
  INNER JOIN dbo.tbl02 p WITH(NOLOCK) ON ek.someid = p.someid
  INNER JOIN dbo.tbl03 eo WITH(NOLOCK) ON ek.someid = eo.someid
  WHERE  blah blah.....
)

  IF NOT EXISTS
    (SELECT DISTINCT mem.[memid]
     FROM MEM_CTE mem
     LEFT JOIN tbl04 ag ON mem.someid = ag.someid
     WHERE blah blah ...
     BEGIN
       PRINT 'Member is NOT not listed' + CHAR(13)
     END

Can you please help?

TIA
0
Comment
Question by:allanau20
2 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 250 total points
ID: 21754187
a select statement needs to immediately follow your CTE definition
0
 
LVL 5

Author Comment

by:allanau20
ID: 21754269
I c.

I rearranged my SQL and it works. Thanks!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

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