0

我将如何修改存储过程以运行两个 SQL 语句

CREATE procedure [dbo].[hms_GetEmployeeSalaryRecordsByContractId]
(
@Id int
)
as
SELECT c.*
FROM  contract c
where c.emp_no =  @Id AND c.leave_date='1900-01-01 00:00:00.000' and c.main_contract=1

select * from salary s where s.contract_id = firstquery.contract_id

以上可以根据一份合同找到两条工资记录

如果找到多个薪水,那么我需要sum(s.salary)在存储过程中将其作为十进制值返回。

到目前为止我已经这样做了

 USE [pamsv83x]
GO
/****** Object:  StoredProcedure [dbo].[hms_GetEmployeeSalaryRecordsByContractId]    Script Date: 08/29/2013 10:45:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create procedure [dbo].[hms_GetEmployeeSalaryRecordsByContractEmpNo]
(
@Id int,
@sallaryresult decimal(8,2) OUTPUT
)
as
DECLARE @contract_id int
DECLARE @totalsallary decimal(8,2)
set @contract_id=(SELECT c.contract_id
FROM  contract c
where c.emp_no =  @Id AND c.leave_date='1900-01-01 00:00:00.000' and c.main_contract=1)


SELECT *,SUM(salary)
from salary s 
where s.emp_no=@contract_id
return 

修改 2----- 他们可以有多个合同,但可以有多个薪水

 Create procedure [dbo].[hms_GetEmployeeSalaryRecordsByContractEmpNo]
 (
@Id int,
@sallaryresult decimal(8,2) OUTPUT
)
as
DECLARE @contract_id int
DECLARE @totalsallary decimal(8,2)
set @contract_id=(SELECT c.contract_id
FROM  contract c
where c.emp_no =  @Id AND c.leave_date='1900-01-01 00:00:00.000')


SELECT *,SUM(salary + old_salary)
from salary s 
where s.contract_id=@contract_id
return 
4

1 回答 1

0

加入工资联系将是这样做的更好方法。就像是:

CREATE procedure [dbo].[hms_GetEmployeeSalaryRecordsByContractId]
(
@Id int
)
as
SELECT 
     c.contract_id
    ,SUM(s.salary + s.old_salary)       
FROM  
    contract c
INNER JOIN
    salary s
    ON c.contract_id = s.contract_id    
WHERE
        c.emp_no = @Id 
    AND c.leave_date = '1900-01-01 00:00:00.000' 
    AND c.main_contract = 1

GROUP BY
    c.contract_id 

上面将接受一个员工 ID,并通过 contact_id 返回一个汇总的工资值。

于 2013-08-29T18:35:03.867 回答