1

我有两张桌子,@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 

任何人都可以帮助我吗

4

1 回答 1

4

如果您提前知道该值,则基本设置将是:

select *
from
(
  select c.did,
    d.value,
    c.detailsvalue
  from DETAILS d
  inner join CONTACTDETAILS c
    on d.CONTACTID = c.CONTACTID
) src
pivot
(
  max(detailsvalue)
  for value in (EMPLOYEEID, EMPLOYEENAME, ADDRESS1,
                MOBILENUMBER, PHONENUMBER)
) piv;

请参阅带有演示的 SQL Fiddle

以上为您提供了所有值,但如果您想实现动态 SQL 以根据 过滤结果@did,那么您将使用:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX),
    @did int

set @did = 1

select @cols = STUFF((SELECT ',' + QUOTENAME(d.value) 
                    from DETAILS d
                    inner join CONTACTDETAILS c
                      on d.CONTACTID = c.CONTACTID
                    where c.did = @did
                    group by d.value, c.DETAILID
                    order by c.DETAILID
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT ' + @cols + ' 
             from 
             (
                select 
                  d.value,
                  c.detailsvalue
                from DETAILS d
                inner join CONTACTDETAILS c
                  on d.CONTACTID = c.CONTACTID
                where c.did = '+cast(@did as varchar(10))+'
            ) x
            pivot 
            (
                max(detailsvalue)
                for value in (' + @cols + ')
            ) p '

execute(@query);

请参阅带有演示的 SQL Fiddle

于 2013-04-18T11:33:47.507 回答