0

我有一个这样的存储过程:

ALTER procedure [dbo].[ParkingDeatailsReportnew] 
  @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 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 dtime between ''' + @startdate + ''' and ''' + @enddate +  '''and locid =  ' + (select l.Locid from Location_tbl l) 
 + '  ) d pivot ( count(Vtype)     for Vtype in (' + @cols + ')  ) p '
execute(@query)
end

执行此查询时出现以下错误:

需要未提供的参数“@locid”。我想从我的位置表中获取所有 locid

4

2 回答 2

0

试试这个——

ALTER PROCEDURE [dbo].[ParkingDeatailsReportnew] 

    @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 dbo.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 
        join VType_tbl v on t.vtid = v.vtid  
        where dtime between ''' + @startdate + ''' 
            and ''' + @enddate + '''
            and locid =  ' + CAST(@locid AS VARCHAR(10)) +
    '  ) d pivot ( count(Vtype) for Vtype in (' + @cols + ')  ) p '

    EXEC sys.sp_executesql @query
END
于 2013-08-05T05:34:39.310 回答
0

您的存储过程无法运行,因为您需要获取参数@locid@StartDate@EndDate

EXEC ParkingDeatailsReportnew   
   @logid = [yourvalue], @StartDate = '[somedate]', @enddate = 'someEndDate'

例如:

EXEC ParkingDeatailsReportnew   
   @logid = 1234, @StartDate = '2013/08/04 08:01:00 ', @enddate = '2013/08/04 11:21:00'

解决这个问题后,开始解决其他问题

- - 编辑 - -

所以你需要从参数中删除它。

ALTER procedure [dbo].[ParkingDeatailsReportnew] 
    @startdate nvarchar(100),@enddate nvarchar(100)

你当初为什么拥有它?
我怀疑它是否完全execute需要VARCHAR,您使用sp_executesql时使用NVARCHAR

我还认为您的逻辑中还有其他错误,但是您稍后会到达那里。

于 2013-08-04T11:42:34.487 回答