我有一个这样的存储过程:
ALTER PROCEDURE [dbo].[ParkingDeatailsReportnew]
@startdate NVARCHAR(100),
@enddate NVARCHAR(100)AS BEGINDECLARE @cols AS NVARCHAR(MAX) , @query AS NVARCHAR(MAX)
SELECT @cols = STUFF(( SELECT DISTINCT ',' + QUOTENAME(Vtype)
FROM dbo.VType_tbl FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET @query =
'SELECT LocName,Date, ' + @cols + '
from (
select l.LocName,v.Vtype, convert(date, dtime) as Date
from Transaction_tbl t
join VType_tbl v on t.vtid = v.vtid
where dtime between ''' + @startdate + ''' and ''' + @enddate + '''
and locid IN (SELECT l.Locid FROM dbo.Location_tbl l)
) d pivot ( count(Vtype) for Vtype in (' + @cols + ') ) p '
EXEC sys.sp_executesql @query
END
在执行时我收到这样的错误:
无法绑定多部分标识符“l.LocName”。
我该如何解决这个问题?