0
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
4

2 回答 2

4

注意动态 SQL 中@cols 和@locid 之间的区别。

代替

where locid = @locid

where locid = ' + CAST(@locid as varchar(max)) + '

注意:虽然这是一个快速修复,但请参阅 RBarryYoung 的回答,了解动态 SQL 的最佳实践。

于 2013-06-21T21:27:12.567 回答
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
于 2015-07-11T18:11:14.800 回答