您能告诉我们如何从表格中查找该雇员的最新更新职位吗?两个表:Employee和EmployeeDetails。员工字段为:EmployeeID,EmployeeName员工详细信息字段为:EmployeeID,指定,PromotionDate。
如果提供EmployeeID,如何查找当前的雇员指定。使用功能。我想创建一个简单的函数来使用EmployeeID根据当前的指定(最新的PromotionDate)获得指定,然后与Employee表联接。
DROP TABLE IF EXISTS Employee
CREATE TABLE Employee
(
EmployeeID INT NOT NULL PRIMARY KEY IDENTITY(1000,1),
EmployeeName VARCHAR(25)
)
INSERT INTO Employee VALUES('AAA');
INSERT INTO Employee VALUES('LAAA');
INSERT INTO Employee VALUES('RSSS');
INSERT INTO Employee VALUES('SEEE');
INSERT INTO Employee VALUES('CFFF');
INSERT INTO Employee VALUES('SEEEW');
INSERT INTO Employee VALUES('MCCC');
INSERT INTO Employee VALUES('DERR');
INSERT INTO Employee VALUES('DERR');
INSERT INTO Employee VALUES('DERW');
SELECT * FROM Employee
DROP TABLE EmployeeDetails
CREATE TABLE EmployeeDetails
(
EmployeeID INT FOREIGN KEY REFERENCES Employee(EmployeeID),
Designation VARCHAR(25),
PromotionDate Date
)
INSERT INTO EmployeeDetails VALUES(1000,'www','2020-11-20');
INSERT INTO EmployeeDetails VALUES(1000,'qqq','2020-01-23');
INSERT INTO EmployeeDetails VALUES(1009,'qqq','2020-09-20');
SELECT * FROM EmployeeDetails
SELECT
E.EmployeeID,
E.EmployeeName,
ED.Designation, ED.PromotionDate
FROM
Employee E
JOIN
EmployeeDetails ED ON E.EmployeeID = ED.EmployeeID
我为此编写了一个函数,但是我不知道如何将其与查询合并:
CREATE FUNCTION GetOnlyTheCurrentPost
( @EmpID INT)
RETURNS DATE
AS
BEGIN
DECLARE @PromoDate DATE
SELECT @PromoDate= MAX(PromotionDate)
FROM EmployeeDetails
WHERE EmployeeID = @EmpID
RETURN(@PromoDate)
END
我如下更改了功能
ALTER FUNCTION [dbo].[GetOnlyTheCurrentPost]
( @PromoDate DATE)
RETURNS DATE
AS
BEGIN
SELECT @PromoDate= MAX(PromotionDate)
FROM EmployeeDetails
RETURN(@PromoDate)
END
SELECT
E.EmployeeID,
ED.Designation,[dbo].[GetOnlyTheCurrentPost](ED.PromotionDate) AS LatestPost
FROM
Employee E
JOIN
EmployeeDetails ED
ON E.EmployeeID = ED.EmployeeID
这不仅会显示所有记录,而且还会显示所有记录。
再次,我更改了功能。如果我给EmployeeID像SELECT [dbo].[GetOnlyTheCurrentDesignation](1011)
,我想得到当前的名称。输出应根据给定的相应EmployeeID输出进行打印:ProjectManager
ALTER FUNCTION GetOnlyTheCurrentDesignation
(@EmpID INT)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @Designation VARCHAR(25)
SELECT @Designation=Designation, MAX(PromotionDate)
FROM EmployeeDetails
WHERE EmployeeID = @EmpID
RETURN(@Designation)
END
请告诉我解决此问题的方法
答案 0 :(得分:1)
尝试这样的事情:
Timer.run