我有一个这样的存储过程:
ALTER PROCEDURE [dbo].[ParkingDeatailsReportcheck]
@locid INTEGER ,
@startdate NVARCHAR(100) ,
@enddate NVARCHAR(100)
AS
BEGIN
DECLARE @cols AS NVARCHAR(MAX) ,
@query AS NVARCHAR(MAX)
SELECT @cols = STUFF((SELECT DISTINCT
',' + QUOTENAME(Vtype)
FROM 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 inner join VType_tbl v on t.vtid = v.vtid
join Location_tbl l on t.locid=l.Locid where locid='
+ CAST(@locid AS VARCHAR(MAX)) + ' and dtime between '''
+ @startdate + ''' and ''' + @enddate + ''' ) d pivot (
count(Vtype)
for Vtype in (' + @cols + ') ) p '
EXECUTE(@query)
END
执行此存储过程时出现如下错误:不明确的列名“locid”。我的预期输出是这样的:
locationname Date Emaar Staff Lost Ticket Normal VIP VVIP
---------- ----------- ----------- ----------- ----------- -----------
Fashion 2013-05-08 1 0 2 0 1
Fashion 2013-05-25 0 0 1 1 0
Fashion 2013-05-27 0 0 17 2 1