0

我的情况是在登录我的网站后,我想显示有多少员工活跃、不活跃以及部门明智、拼贴明智的员工人数列表。

为此,如果临时表不存在,我创建了一个创建临时表的过程,否则删除表创建临时表,之后我编写了一些 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.
4

1 回答 1

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
;
于 2012-04-06T06:49:56.340 回答