0

您好,我有一个表格,其中包含员工打卡和计时系统中的打卡数据。我正在寻找动态旋转打孔数据列并总计员工轮班跨度。但是,我遇到了动态枢轴的问题。我检查了其他几个帖子,但我似乎没有得到任何地方。

这是代码:

USE wfcdb
DECLARE @cols as NVARCHAR(MAX),
        @query AS NVARCHAR(MAX);

SELECT @cols = STUFF((SELECT  DISTINCT ',' + QUOTENAME(CONVERT(VARCHAR(10),EVENTDTM,101)) as DATE FROM TIMESHEETITEM  WHERE DATEDIFF(d,EVENTDTM,GetDate()) <7 ORDER BY DATE DESC
                        FOR XML PATH(''), TYPE
                        ).value('.','NVARCHAR(MAX)')
                        ,1,1,'')

Set @query='SELECT EEID, ' + @cols + 'FROM (
SELECT p.PERSONnum as EEID,p.FULLNM as FULLNM, 
convert(varchar(10),ti.EVENTDTM,101) as PunchDate,
DATEDIFF(MINUTE,ti.enddtm,ti.startdtm)/60*-1 AS SPAN
FROM  (((((  
    TIMESHEETITEM ti  With (NoLock) LEFT JOIN  
        PUNCHEVENT pe With (NoLock) ON ti.STARTPUNCHEVENTID = pe.PUNCHEVENTID) LEFT JOIN 
            DATASOURCE ds ON pe.DATASOURCEID = ds.DATASOURCEID) LEFT JOIN 
                CLIENTCONTEXT cc ON ds.CLIENTCONTEXTID = cc.CLIENTCONTEXTID) 
    LEFT JOIN       PUNCHEVENT AS pe1  With (NoLock) ON ti.ENDPUNCHEVENTID = pe1.PUNCHEVENTID) LEFT JOIN 
            DATASOURCE AS ds1 ON pe1.DATASOURCEID = ds1.DATASOURCEID) LEFT JOIN 
                CLIENTCONTEXT AS cc1 ON ds1.CLIENTCONTEXTID = cc1.CLIENTCONTEXTID
    INNER JOIN PERSON p ON ti.EMPLOYEEID = p.PERSONID
    INNER JOIN LABORACCT la1 ON la1.laboracctid = ti.laboracctid
WHERE DATEDIFF(d,ti.EVENTDTM,GetDate()) <7
AND ti.TmShtItemTypeID = 40 
AND (isnull(cc.CLNT,'') + isnull(cc1.CLNT,''))<> '') X
PIVOT
(
    SUM(SPAN) 
    FOR PunchDate in (' + @cols + ')
    ) p'

Execute(@query)

这是我收到的错误消息:

消息 105,级别 15,状态 1,第 17 行
字符串 ') X PIVOT (.....) p' 后面的非闭合引号。
消息 102,级别 15,状态 1,第 17 行
') 附近的语法不正确 X
PIVOT (.....

任何帮助,将不胜感激。

4

1 回答 1

0

由于您在动态 SQL 中连接空字符串,因此您需要添加更多单引号:

AND (isnull(cc.CLNT,'''') + isnull(cc1.CLNT,''''))<> '''') X

所以完整的代码将是:

DECLARE @cols as NVARCHAR(MAX),
        @query AS NVARCHAR(MAX);

SELECT @cols = STUFF((SELECT  DISTINCT ',' + QUOTENAME(CONVERT(VARCHAR(10),EVENTDTM,101)) as DATE 
                        FROM TIMESHEETITEM  WHERE DATEDIFF(d,EVENTDTM,GetDate()) <7 ORDER BY DATE DESC
                        FOR XML PATH(''), TYPE
                        ).value('.','NVARCHAR(MAX)')
                        ,1,1,'')

Set @query='SELECT EEID, ' + @cols + 'FROM (
SELECT p.PERSONnum as EEID,p.FULLNM as FULLNM, 
convert(varchar(10),ti.EVENTDTM,101) as PunchDate,
DATEDIFF(MINUTE,ti.enddtm,ti.startdtm)/60*-1 AS SPAN
FROM  (((((  
    TIMESHEETITEM ti  With (NoLock) LEFT JOIN  
        PUNCHEVENT pe With (NoLock) ON ti.STARTPUNCHEVENTID = pe.PUNCHEVENTID) LEFT JOIN 
            DATASOURCE ds ON pe.DATASOURCEID = ds.DATASOURCEID) LEFT JOIN 
                CLIENTCONTEXT cc ON ds.CLIENTCONTEXTID = cc.CLIENTCONTEXTID) 
    LEFT JOIN       PUNCHEVENT AS pe1  With (NoLock) ON ti.ENDPUNCHEVENTID = pe1.PUNCHEVENTID) LEFT JOIN 
            DATASOURCE AS ds1 ON pe1.DATASOURCEID = ds1.DATASOURCEID) LEFT JOIN 
                CLIENTCONTEXT AS cc1 ON ds1.CLIENTCONTEXTID = cc1.CLIENTCONTEXTID
    INNER JOIN PERSON p ON ti.EMPLOYEEID = p.PERSONID
    INNER JOIN LABORACCT la1 ON la1.laboracctid = ti.laboracctid
WHERE DATEDIFF(d,ti.EVENTDTM,GetDate()) <7
AND ti.TmShtItemTypeID = 40 
AND (isnull(cc.CLNT,'''') + isnull(cc1.CLNT,''''))<> '''') X
PIVOT
(
    SUM(SPAN) 
    FOR PunchDate in (' + @cols + ')
    ) p'

execute @query
于 2013-07-04T19:17:25.413 回答