我有两张桌子,@DETAILS
和@CONTACTDETAILS
。在这两个表中,@DETAILS
是一个静态表,其中contactid
主键,外键@CONTACTDETAILS
以下是包含数据的表格:
DECLARE @DETAILS TABLE (CONTACTID INT IDENTITY(1,1), VALUE VARCHAR(200))
INSERT INTO @DETAILS
SELECT 'PHONENUMBER' UNION ALL
SELECT 'MOBILENUMBER' UNION ALL
SELECT 'ADDRESS1' UNION ALL
SELECT 'ADDRESS2' UNION ALL
SELECT 'EMPLOYEENAME' UNION ALL
SELECT 'EMPLOYEEID'
DECLARE @CONTACTDETAILS TABLE (DETAILID INT IDENTITY(1,1),
DID INT,
CONTACTID INT,
DETAILSVALUE VARCHAR(200)
)
INSERT INTO @CONTACTDETAILS
SELECT 1,5,'SIVA' UNION ALL
SELECT 1,6,'SIA001' UNION ALL
SELECT 1,2,'9344139991' UNION ALL
SELECT 2,1,'245678' UNION ALL
SELECT 2,2,'1686597' UNION ALL
SELECT 2,5,'GANESH' UNION ALL
SELECT 1,3,'ST-ROAD'
SELECT * FROM @DETAILS
输出是:
CONTACTID VALUE
----------------------
1 PHONENUMBER
2 MOBILENUMBER
3 ADDRESS1
4 ADDRESS2
5 EMPLOYEENAME
6 EMPLOYEEID
SELECT * FROM @CONTACTDETAILS
第二个查询的输出是:
DETAILID DID CONTACTID DETAILSVALUE
-----------------------------------
1 1 6 SIVA
2 1 5 SIA001
3 1 2 9344139991
4 2 1 245678
5 2 2 1686597
6 2 5 GANESH
7 1 3 ST-ROAD
在@CONTACTDETAILS
列中,我有一个列的值,DID
用于在我正在使用的存储过程中显示一个特定的员工详细信息
参数@DID int
- 我刚试过这个查询
declare @DID int=1
SELECT
(SELECT VALUE FROM @DETAILS WHERE CONTACTID=A.CONTACTID)AS CONTACTID,
A.DETAILSVALUE
FROM
@CONTACTDETAILS A
WHERE
A.DID=@DID
这将显示这样的结果
CONTACTID DETAILSVALUE
EMPLOYEEID SIVA
EMPLOYEENAME SIA001
MOBILENUMBER 9344139991
ADDRESS1 ST-ROAD
现在我正在尝试这样的输出
EMPLOYEEID EMPLOYEENAME MOBILENUMBER ADDRESS1
SIA001 SIVA 9344139991 ST-ROAD
任何人都可以帮助我吗