MTHeadss
MTHeadss

Reputation: 235

Sql server temp table

I need to create a temp table who contains the number of employes of each department. If the department have no employer, we need to print a message.

IF (count(*) = 0) 
    BEGIN
    PRINT 'Espace vide'
    END
    else 
    Select deptno,count(*)  
    from emp    
    group by deptno;

this is the query to see how many employes are in each dept, but I don't know how to create a temp table with it.

Upvotes: 7

Views: 7223

Answers (1)

Oleg Dok
Oleg Dok

Reputation: 21776

Choose suitable for you method:

Select deptno,count(*) cnt
INTO #TempTable  
from emp    
group by deptno;

select 
  *,
  CASE cnt WHEN 0 THEN 'Espace vide' ELSE NULL END AS column1
FROM #TempTable

if exists(SELECT * FROM #TempTable WHERE cnt = 0) PRINT 'Espace vide'

Upvotes: 7

Related Questions