alter procedure [dbo].[ParkingDeatailsReport]
as
begin
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@locid INTEGER
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Vtype)
from VType_tbl
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
set @query = 'SELECT Date, ' + @cols + '
from
(
select v.Vtype, convert(date, dtime) as Date
from Transaction_tbl t
inner join VType_tbl v on t.vtid = v.vtid
where locid = ' + CAST(@locid as varchar(max)) + ') d
pivot
(
count(Vtype)
for Vtype in (' + @cols + ')
) p '
execute(@query)
end
答案 0 :(得分:4)
注意动态SQL中@cols和@locid之间的区别。
<强>替换强>
where locid = @locid
。通过强>
where locid = ' + CAST(@locid as varchar(max)) + '
注意:虽然这是一个快速修复,但请参阅RBarryYoung的答案,了解动态SQL的最佳实践。
答案 1 :(得分:0)
错误清楚地说明,您当前的SP代码不接受任何参数,并且您尝试将参数传递给SP。如果要在SP中接受参数,则语法为:
Create Procedure Procedure-name
(
Input parameters ,
Output Parameters (If required)
)
As
Begin
Sql statement used in the stored procedure
End
因此,在您的情况下,SP可以更改为:
alter procedure [dbo].[ParkingDeatailsReport]
(
@cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@locid INTEGER
)
as
begin
..... you code go hear
END