Solved

Difficult WITH CTE Question

Posted on 2013-01-02
2
406 Views
Last Modified: 2013-01-02
How would I go about getting the following select into a Declared @TEMP  TABLE?

Notice that the ;with cte also is joined with another select in a  union all statement


;WITH CTE (
			[Client ID] ,
			[Contact ID] ,
			ContactType ,
			fullName ,
			[Last Name] ,
			Email ,
			Phone ,
			[Phone ext] ,
			Role ,
			Title ,
			ur ,
			Submitted ,
			Locked ,
			ConPCT ,
			Assignment ,
			Working ,
			Pending ,
			AddRemove ,
			ContactVisitSort ,
			id )
AS
	(
	SELECT	vw.[Client ID],
			vw.[Contact ID],
			vw.ContactType,
			vw.fullName,
			vw.[Last Name],
			vw.Email,
			vw.Phone,
			vw.[Phone ext],
			vw.Role,
			vw.Title,
			vw.ur, 
			vw.Submitted,
			vw.Locked,
			vw.ConPCT,
			vw.Assignment,
			vw.Working,
			vw.Pending,
			(	
				CASE WHEN ISNULL(ContactV.id, 0)= 0 AND ISNULL(vw.[Contact ID],0) > 0 THEN 'Add'
				WHEN ISNULL(ContactV.id, 0)> 0 AND ISNULL(vw.[Contact ID],0) > 0 
						AND NOT Exists (Select 1 from dbo.ClientVisitTrackingContacts WHERE vw.[Contact ID] = ContactV.[Contact ID] AND visitID = A.id ) THEN 'Add'
				WHEN ISNULL(vw.[Contact ID],0) = 0 THEN ''
				ELSE 'Remove' END
			) AS AddRemove,
			(
				CASE WHEN ISNULL(ContactV.id, 0) = 0 
				THEN 0 
				ELSE 1 END
			) AS ContactVisitSort,
			A.id
	FROM	dbo.vw_MarketingVisitationContacts AS vw 
	LEFT OUTER JOIN
			 dbo.ClientVisitTrackingContacts AS ContactV ON vw.[Client ID] = ContactV.[Client ID] AND vw.[Contact ID] = ContactV.[Contact ID]
	OUTER APPLY 
		   ( 
		   SELECT id FROM ClientVisitTracking CVT
		   WHERE CVT.[Client ID] = vw.[Client ID]
		   ) A 
	WHERE		(LEN(vw.fullName) > 0)
	)

	SELECT * FROM cte WHERE id = @VisitID
	
	UNION all
	SELECT  [Client ID] ,
			[Contact ID] ,
			'Contact' ContactType,
			ISNULL([First Name],'') + ' ' + [Last Name] FullName,
			[Last Name],
			Email,
			Phone,
			[Phone ext],
			Role ,
			Title ,
			2 ur,
			0 Submitted,
			0 Locked,
			0 ConPCT,
			0 Assignment,
			0 Working,
			0 Pending,
			'Add' AddRemove,
			0 ContactVisitSort,
			@VisitID id
	FROM	dbo.[Client Contacts] C
	WHERE NOT EXISTS (
						SELECT	1
						FROM	 CTE C1
						WHERE	C.[Client ID] = C1.[Client ID] 
							AND C.[Contact ID] = C1.[Contact ID]
						)
	AND C.[Client ID] = @ClientID
	ORDER BY ur,AddRemove DESC, Assignment DESC,[Last Name]

Open in new window

0
Comment
Question by:lrbrister
2 Comments
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
You would do as you would without a CTE:
with cte (...) as (...)
insert into @temp
select  * FROM cte WHERE id = @VisitID
union all
...

Open in new window

0
 

Author Closing Comment

by:lrbrister
Comment Utility
Well duh on me...
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

772 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

10 Experts available now in Live!

Get 1:1 Help Now