2012-04-06 52 views
0

我的情況是在登錄我的網站後,我想顯示有多少員工活躍,&也是部門智慧的,拼貼智慧的員工列表。根據SQL Server 2008中的多個select語句創建表並插入行

爲此,我創建了一個過程來創建臨時表,如果它不存在其他drop table創建臨時表,之後我寫了一些SQL查詢來獲得僱員人數,部門條件&然後我插入記錄到表。

然後我需要插入的行。現在我的問題是執行SQL中的過程執行它,但它不創建插入任何行的&,我不知道爲什麼會發生這種情況。如果有人知道這個問題的解決方案,請幫助我。

我的代碼:

alter proc SP_TEMPRECORDFORCOUNT 
as 
begin 

IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'TEMPRECORDFORCOUNT')) 
BEGIN 
    drop table dbo.TEMPRECORDFORCOUNT 
END 

create table dbo.TEMPRECORDFORCOUNT(totalemployeecount int,activeemployeecount int,inactiveemployeecount int,deptwiseemployeecount int,activeemployeecount int) 

declare @totalemployeecount int 
declare @activeemployeecount int 
declare @inactiveemployeecount int 
declare @deptwiseemployeecount int 
declare @activeemployeecount int 

select @totalemployeecount =COUNT(*) from Employee 
select @activeemployeecount =COUNT(*) from Employee where status=1 
select @inactiveemployeecount =COUNT(*) from Employee where status=0 
select @deptwiseemployeecount = count(*) from Department where e_id !=null 
select @activeemployeecount = count(*) from Department d inner join Employee e on d.e_id =e.e_id where status_id=1 

insert into TEMPRECORDFORCOUNT 
(
totalemployeecount 
,activeemployeecount 
,inactiveemployeecount 
,deptwiseemployeecount 
,activeemployeecount 
) 
values 
(
@totalemployeecount , 
@activeemployeecount , 
@inactiveemployeecount , 
@deptwiseemployeecount , 
@activeemployeecount , 
) 
end 

is it correct way thing i'm doing? if not please correct me. 

回答

0

有了SQL Server 2008 R2您對錶變量的選項。

所以你的說法應改爲如下:

DECLARE @TEMPRECORDFORCOUNT TABLE(totalemployeecount int,activeemployeecount int,inactiveemployeecount int,deptwiseemployeecount int,activeemployeecount int) 

insert into @TEMPRECORDFORCOUNT 
(
totalemployeecount 
,activeemployeecount 
,inactiveemployeecount 
,deptwiseemployeecount 
,activeemployeecount 
) 
values 
(
@totalemployeecount , 
@activeemployeecount , 
@inactiveemployeecount , 
@deptwiseemployeecount , 
@activeemployeecount , 
) 

SELECT * FROM @TEMPRECORDFORCOUNT 
; 
+0

謝謝它的工作很大 – 2012-04-06 07:02:41