Solved

Difficult WITH CTE Question

Posted on 2013-01-02
2
412 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 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 38737536
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
ID: 38737625
Well duh on me...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
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 the fundamental information of how to create a table.

733 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