这是我的存储过程。它需要INT作为输入,它本质上是一个员工ID。
old = {width=1920, x=5, y=831}, new = {width=1517, x=5, y=693}
我想要相同的行为但不需要手动提供输入。我希望它能够从staff表中获取所有现有的staffId,其中staffdd仍然是唯一的参数。我怎么做?我尝试过使用WHILE循环。但它
CREATE PROCEDURE [dbo].[spMonthlyPayroll]
@StaffId int
AS
BEGIN
SET NOCOUNT ON;
SELECT StaffId, Name, CNICNumber, Salary, BankAccountNumber,[dbo].[totalWorkingDaysScalar](StaffId, datename(month, GETDATE())) as TotalDaysWorked, [dbo].[totalDaysWorkedScalar](StaffId, datename(month, GETDATE())) as TotalDaysWorked,
(CONVERT([date],getdate())) AS PayrollDate,
(CONVERT([date],dateadd(month,(-1),dateadd(month,datediff(month,(0),getdate())+(1),(0))))) AS PayrollFrom,
(CONVERT([date],dateadd(day,(-1),dateadd(month,datediff(month,(0),getdate())+(1),(0))))) AS PayrollTo,
(datename(month,getdate())) + ', ' + (datename(year,getdate())) AS PayrollPeriod,
(Salary/[dbo].[totalWorkingDaysScalar](StaffId, datename(month,getdate()))) * [dbo].[totalDaysWorkedScalar](StaffId, datename(month,getdate())) as GrossSalary,
.10 as IncomeTaxFactor,
((Salary/[dbo].[totalWorkingDaysScalar](StaffId, datename(month,getdate()))) * [dbo].[totalDaysWorkedScalar](StaffId, datename(month,getdate()))) - ((Salary/[dbo].[totalWorkingDaysScalar](StaffId, datename(month,getdate()))) * [dbo].[totalDaysWorkedScalar](StaffId, datename(month,getdate())) * (.10)) as TotalNetSalary
FROM Staff
where Staff.StaffId=@StaffId
END
GO
在set @StaffId = select Staff.StaffId from Staff
请帮忙。我正在使用SQL Server 2014。
答案 0 :(得分:2)
如果您希望每次都返回Staff
表中的所有记录,并且您希望每次执行完全相同的查询,那么您根本不需要任何功能,只需创建观点:
CREATE VIEW dbo.CurrentMonthPayroll
AS
SELECT StaffId,
Name,
CNICNumber,
Salary,
BankAccountNumber,
[dbo].[totalWorkingDaysScalar](StaffId, DATENAME(MONTH, GETDATE())) AS TotalWorkingDays,
[dbo].[totalDaysWorkedScalar](StaffId, DATENAME(MONTH, GETDATE())) AS TotalDaysWorked,
(CONVERT(DATE,GETDATE())) AS PayrollDate,
(CONVERT(DATE,DATEADD(MONTH,(-1),DATEADD(MONTH,DATEDIFF(MONTH,(0),GETDATE())+(1),(0))))) AS PayrollFrom,
(CONVERT(DATE,DATEADD(DAY,(-1),DATEADD(MONTH,DATEDIFF(MONTH,(0),GETDATE())+(1),(0))))) AS PayrollTo,
(DATENAME(MONTH,GETDATE())) + ', ' + (DATENAME(YEAR,GETDATE())) AS PayrollPeriod,
(Salary/[dbo].[totalWorkingDaysScalar](StaffId, DATENAME(MONTH,GETDATE()))) * [dbo].[totalDaysWorkedScalar](StaffId, DATENAME(MONTH,GETDATE())) AS GrossSalary,
.10 AS IncomeTaxFactor,
((Salary/[dbo].[totalWorkingDaysScalar](StaffId, DATENAME(MONTH,GETDATE()))) * [dbo].[totalDaysWorkedScalar](StaffId, DATENAME(MONTH,GETDATE()))) - ((Salary/[dbo].[totalWorkingDaysScalar](StaffId, DATENAME(MONTH,GETDATE()))) * [dbo].[totalDaysWorkedScalar](StaffId, DATENAME(MONTH,GETDATE())) * (.10)) AS TotalNetSalary
FROM Staff
GO
如果您想将这些记录INSERT
放到其他表中,您可以:
INSERT INTO PayrollTable
SELECT StaffId,
Name,
CNICNumber,
Salary,
BankAccountNumber,
TotalWorkingDays,
TotalDaysWorked,
PayrollDate,
PayrollFrom,
PayrollTo,
PayrollPeriod,
GrossSalary,
IncomeTaxFactor,
TotalNetSalary
FROM dbo.CurrentMonthPayroll
答案 1 :(得分:0)
我认为用循环处理每个员工的表现并不好。
您可以修改存储过程以支持处理多个人员 例如,您可以将参数@staffID更改为varchar变量,以获取带有分隔符的combin multliple staffid字符串
FROM Staff
where Staff.StaffId=isnull(@StaffIds,Staff.StaffId) --NULL is process all data
or case when @StaffIds is null then 1 else
charindex(','+convert(varchar,Staff.StaffId)+',',','+@StaffIds+',') end >0