Return 两个表中两列的数据,并使用存储过程对它们执行计算

Return data for two columns from two tables and perform a calculation on them with stored procedure

一个迷你员工账户系统,我有两个table,employees_salariestable和津贴table,每个都有一列employee_id 连接它们。

我想 return 来自 employees_salaries basic_salary 列中的值 table return allowance_value 列中的值来自津贴 table 并对它们进行求和运算,并将结果插入 employees_salaries table.

中的 Net_salary 列

我想用存储过程Sql服务器来实现。

创建一个标量函数并从津贴table

中return津贴金额
CREATE FUNCTION dbo.GetValue(@EmpID INT)
RETURNS INT
AS 
SELECT allowance_value
FROM [allowance table]
WHERE E_ID = @EmpID

然后更改员工薪水 table 并添加一个新列

通过传入EmployeeId,即可获取到数据

ALTER TABLE dbo.employees_salaries 
ADD NewColumnName AS (dbo.GetValue(employees_salaries.[EmpId Column]) + employees_salaries.[Basic pay Col])