动态sql查询未在服务器上显示结果

时间:2014-07-07 18:55:10

标签: sql sql-server sql-server-2008

此查询显示在本地系统上运行时的记录,但是当我将其部署到服务器并在服务器上运行时,查询不会显示任何记录。 当我通过sql引擎连接到数据库服务器并执行(alt + x)时,它只显示消息command completed successfully,但不显示任何记录。

任何想法如何解决这个问题,在哪里?

DECLARE @cols AS NVARCHAR(MAX)
, @query  AS NVARCHAR(MAX) 
select @cols = STUFF
(
    (
        SELECT ',' + QUOTENAME(StartDateTime) 
        from
        (
            select distinct StartDateTime 
            from tblEmployeeShift 
            inner join tblShift on tblShift.ShiftId = tblEmployeeShift.ShiftId 
            where EmployeeNewId = 3126 
            and   IsDeleted = 0 
            and   StartDateTime >= '06/07/2014 12:00:00' 
            and StartDateTime <= '07/07/2014 12:00:00' 
        ) d 
        order by StartDateTime 
        FOR XML PATH(''), TYPE 
    ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,''
)

set @query = 
N'SELECT EmployeeNewId
,Full_Name
,' + @cols + N' 
from  
(
    select * 
    from 
    (
        select tblEmployeeShift.EmployeeNewId
        ,Full_Name
        ,StartDateTime
        ,dbo.GetAttendanceFlag(tblEmployeeShift.EmployeeNewId,StartDateTime) as PV 
        from tblEmployeeShift 
        inner join tblShift on tblShift.ShiftId = tblEmployeeShift.ShiftId 
        inner join employee on tblEmployeeShift.EmployeeNewId = SUBSTRING(employee.Employeeid,6,5) 
        where tblEmployeeShift.EmployeeNewId in 
        (
            select top 20 employeenewid 
            from employee e 
            where  e.EmployeeNewId = 3126 
        ) 
        and  IsDeleted = 0 
    ) d
) 
x pivot  
( 
    max(PV) for StartDateTime in (' + @cols + N') 
) c' 

execute sp_executesql @query 

1 个答案:

答案 0 :(得分:0)

服务器上的数据很可能是@cols变量为NULL,并且NULL @cols变量的串联导致NULL @query变量。