我的存储过程是这样的:
alter PROCEDURE [dbo].[ParkingDeatailsReportnewstack]
@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 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
join dbo.Location_tbl l on t.locid=l.Locid
where dtime between ''' + @startdate + ''' and ''' + @enddate + ''' order by l.LocName
) d
pivot (
count(Vtype) for Vtype in (' + @cols + ')
) p '
EXEC sys.sp_executesql @query
End
我想按升序获取我的 locname,但是在按 l.LocName 给出顺序时出现错误:ORDER BY 子句在视图、内联函数、派生表、子查询中无效