Solved

CTE and IF

Posted on 2008-06-10
2
1,002 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
Comment Utility
a select statement needs to immediately follow your CTE definition
0
 
LVL 5

Author Comment

by:allanau20
Comment Utility
I c.

I rearranged my SQL and it works. Thanks!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now