【问题标题】:Stord procedure to filter staff in terms of paygrade and mode of employment根据工资等级和雇佣方式筛选员工的 Stord 程序
【发布时间】:2015-09-15 23:25:18
【问题描述】:

我正在尝试创建一个存储过程,以允许我将我的员工分为 3 个不同的类别,然后可以将它们配对到数据库中的相关帐户。我是编写存储过程的新手,所以我不知道这在语法上是否正确。基本上,在运行时,我希望存储过程检查员工是否全职以及他们的工作角色,然后将它们分配给以下帐户 - HighIncomeC00CCW、LowIncomeC0ECCW 和 PartTimeC0ECCX。这就是我目前所拥有的 -

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[spStaffPay]
@Isfulltime = bit 
@Jobrole = varchar(20)


BEGIN

IF   (@Isfulltime = 1
      AND @Jobrole = 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10)

      BEGIN 
      Account = HighIncomeC00CCW 
      END

ELSE IF (@Isfulltime = 1
      AND @Jobrole = 11 or 12 or 13 or 14 or 15 or 16 or 17 or 18 or 19 or 20)

      BEGIN 
      Account = LowIncomeC0ECCW
      END

ELSE (@Isfulltime = 0)
      Then

      BEGIN 
      Account = PartTimeC0ECCX
      END
END

【问题讨论】:

  • 如果员工在表中,那么您可以从表中选择并使用case 语句 - 这将比编写程序代码更快。
  • 哦,顺便说一句,当您运行该 SP 时,您会收到错误吗?如果是这样,那是什么?如果你想知道它是否正确......运行它!
  • 假设 EmployeeType (Full/Part time) 和 JobRole 是 Staff 表中的一列是否正确?您要查询的表的架构是什么?
  • @NoSaidTheCompiler,是的,EmployeeType(全职/兼职)和 JobRole 是 Staff 表中的一列。架构是什么意思?
  • @Nick.McDermaid 抱歉,我是 SP 的新手,案例陈述看起来如何?

标签: asp.net sql-server stored-procedures account


【解决方案1】:

以下是您可以用来找出解决方案的示例。您可以了解有关 Case 声明 here 的更多信息(msdn 文档)。希望它可以帮助您找出所需的解决方案。

      --This is to drop the temp table if it already exists.
if OBJECT_ID('tempdb..#tempStaff') is not null
begin 
    drop table #tempStaff;
end;

create table #tempStaff 
(StaffId int primary key, 
StaffName varchar(100), 
StaffType varchar(100), 
StaffRole varchar(100))

insert into #tempStaff
values
(1,'james1', 'full', 'role1')
,(2,'james2', 'full', 'role2')
,(3,'james3', 'part', 'role4')
,(4,'james4', 'part', 'role4')

--the code above is to create a sample table
--the code below is an example that you need to learn from to get 
--your stored proc do what you are trying.

select StaffId
,StaffName
,StaffType 
,StaffRole  
, case 
when (StaffType = 'full' and StaffRole in ('role1', 'role2')) then 'HighIncome' 
when (StaffType = 'part' and StaffRole in ('role3', 'role4')) then 'LowIncome'
--you could add as many 'when' as you need depending on your scenario.
end as AccountType
from #tempStaff

如果您需要更多帮助,请说明您卡在哪一部分(您可能需要对您的原始帖子发表评论)。

【讨论】:

  • 感谢您的回复。这会按照它必须的方式工作吗?我需要三种账户类型 1 用于全职 = 是和高收入,1 用于全职 = 是和低收入,一种用于兼职
  • 嗯,我已尽我所能回答您的问题。这不会是一个精确的解决方案,您可能必须修改“when”子句中的条件。希望这可以为您提供足够的基础。用您尝试过的方法和无效的方法更新问题,我会帮助您。
猜你喜欢
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
  • 2012-01-05
  • 2022-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多