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
Solved

CTE and IF

Posted on 2008-06-10
2
1,009 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

860 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