Avatar of Jacque Scott
Jacque Scott
Flag for United States of America asked on

Setting a variable to a query

I have a stored procedure where I am setting variables to queries.  This one works.
SET @mcount = (SELECT COUNT(Matter)FROM [tblCP_Matter]);

Open in new window



Why won't this one work with a CTE?
SET @Period_mcount = (WITH cte AS 
(
SELECT DISTINCT cp_matter.MatterId FROM [tblCP_ActivityLog] cp_Act 
JOIN [tblCP_AccountMatter]  cp_AM ON cp_Act.SourceObjectKey = cp_AM.Account 
JOIN [tblCP_Matter] cp_matter ON cp_matter.Matter = cp_AM.Matter 
WHERE ActionType = 'Create Account' AND MONTH(CAST(ActivityDate AS DateTime)) = '04'  AND YEAR(CAST(ActivityDate AS DateTime)) = '2016'
) 
SELECT count(*) FROM cte);

Open in new window

Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Jacque Scott

8/22/2022 - Mon
SOLUTION
Haris Dulic

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Brian Crowe

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jacque Scott

ASKER
Those both worked.  Thank you for the quick response.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck