Solved

CTE and IF

Posted on 2008-06-10
2
1,012 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

749 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