Question

How should I write a stored procedure for a dynamic SQL. This Code returns no rows and says incorrect syntax near @EmpCode

The base query is at http://sqlfiddle.com/#!3/6ad02e/9 . It is to convert rows to cols if the no. of cols are unknown. I'm trying to send parameters to the same query and just added the code 'in bold'...

set @query = '**DECLARE @CompanyId uniqueidentifier = null,   
                      @EmpCode nvarchar(50) = null
               IF @CompanyId='+'00000000-0000-0000-0000-000000000000'+'
               SET @CompanyId = NULL
               IF @EmpCode = '+''+'
               SET @EmpCode = NULL**

              SELECT id, name,' + @cols + ' 
        from 
        (
          select e.id, e.name, lt.type, l.days 
          from tblEmp e
          left outer join tblEmpLeaves l 
            on e.id = l.empId
          left outer join tblLeaveType lt 
            on l.leaveId = lt.Id
          **WHERE
            (E.Deleted = 0) AND
            (E.EmpCompanyId = @CompanyId OR @CompanyId IS NULL)AND
            (E.EmpCode LIKE %@EmpCode% OR @EmpCode IS NULL)**
        ) x
        pivot 
        (
            max(days)
            for type in (' + @cols + ')
        ) p 
        order by id'
Was it helpful?

Solution

It seems that you are not escaping quotes properly. The following works without errors :

set @query = '
DECLARE @CompanyId uniqueidentifier = null,   
                      @EmpCode nvarchar(50) = null
               IF @CompanyId=''00000000-0000-0000-0000-000000000000''
               SET @CompanyId = NULL 
               IF @EmpCode =''''
               SET @EmpCode = NULL 
SELECT id, name,' + @cols + ' 
            from 
            (
              select e.id, e.name, lt.type, l.days 
              from tblEmp e
              left outer join tblEmpLeaves l 
                on e.id = l.empId
              left outer join tblLeaveType lt 
                on l.leaveId = lt.Id
            ) x
            pivot 
            (
                max(days)
                for type in (' + @cols + ')
            ) p 
            order by id'

http://sqlfiddle.com/#!3/6ad02e/29/0

OTHER TIPS

Try this:

set @query = 'DECLARE @CompanyId uniqueidentifier = null,   
                      @EmpCode nvarchar(50) = null
               IF @CompanyId='+'00000000-0000-0000-0000-000000000000'+'
               SET @CompanyId = NULL
               IF @EmpCode = '+''+'
               SET @EmpCode = NULL**

              SELECT id, name,' + @cols + ' 
        from 
        (
          select e.id, e.name, lt.type, l.days 
          from tblEmp e
          left outer join tblEmpLeaves l 
            on e.id = l.empId
          left outer join tblLeaveType lt 
            on l.leaveId = lt.Id
          WHERE
            (E.Deleted = 0) AND
            (E.EmpCompanyId = @CompanyId OR @CompanyId IS NULL)AND
            (E.EmpCode LIKE %+@EmpCode+% OR @EmpCode IS NULL)**
        ) x
        pivot 
        (
            max(days)
            for type in (' + @cols + ')
        ) p 
        order by id'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top