Solved

CTE and IF

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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

688 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