Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 12890

Common Table Expression Error

$
0
0

I have a requirement to rewrite some of our simple select statement queries using a CTE. And I have had no problems doing so until I encountered one from a query like the one below:

USE AdventureWorks2012
GO
DECLARE @TotalRows INT 
SELECT @TotalRows = COUNT(CustomerID)
FROM Sales.Customer
WHERE CustomerID IN (SELECT CustomerID FROM Sales.SalesOrderHeader);

SELECT @TotalRows AS 'T'

And I rewrote it like the one below but I am getting error.

DECLARE @TotalRows INT 
;WITH TotalRows
(
SELECT @TotalRows = COUNT(CustomerID)
FROM Sales.Customer
WHERE CustomerID IN (SELECT CustomerID FROM Sales.SalesOrderHeader))
SELECT * FROM TotalRows

How do I make this work using a CTE?



Zionlite


Viewing all articles
Browse latest Browse all 12890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>