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

Notice the difference between @cols and @locid in dynamic SQL.

Replace

where locid = @locid

With

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

Note: while this is a quick fix please see answer from RBarryYoung for best practice with dynamic 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 に答える