次のようなストアド プロシージャがあります。
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