0

This query shows records when run on my local system, but when I deploy it to and run it on the server, the query does not show any records. When I connect to the DB server through sql engine and execute (alt + x), it only displays messages command completed successfully, but displays no records.

Any idea how to fix this, where is the issue?

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 
4
  • 3
    put a PRINT @query just before the sp_executesql to see what the query truly is that is being executed. It is possible that for some reasons the value of @query is NULL or empty based on the value of @cols. So also you might need/want to do a PRINT @cols as well. Commented Jul 7, 2014 at 19:05
  • 2
    Unrelated to your question, but FYI you could use a between statement for your start date: StartDateTime between '06/07/2014 12:00:00' and StartDateTime <= '07/07/2014 12:00:00' Commented Jul 7, 2014 at 19:07
  • Yes @srutzky data is not there, thank you for help. Commented Jul 7, 2014 at 19:21
  • 1
    Excellent experience to use this site and your team @JohnLBevan is doing great. Thanks Commented Jul 7, 2014 at 19:22

1 Answer 1

1

Most likely the data on the server is such that the @cols variable is NULL, and the concatenation of the NULL @cols variable results in a NULL @query variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.