-1

手順は以下のとおりです。@startdateをvarcharに変換しましたが、SQLを取得し続けます。スカラー変数「@sql」を宣言する必要があります。私は何が欠けていますか?

CREATE PROCEDURE [dbo].[project_revenue_forecast_monthly]
@startDate datetime,
@sql varchar(max)
AS

SET @sql = 'SELECT CLIENTNAME, PROJECTNAME, PROJECTSD, 
    isnull([' + REPLACE(SUBSTRING(CONVERT(varchar, @startDate, 13), 4, 8), ' ', '') + '],0) as
      [' + REPLACE(SUBSTRING(CONVERT(varchar, @startDate, 13), 4, 8), ' ', '') + '], 
    isnull([' + REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 1, @startDate), 13), 4, 8), ' ', '') + '],0) as  
      [' + REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 1, @startDate), 13), 4, 8), ' ', '') + '],
    isnull([' + REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 2, @startDate), 13), 4, 8), ' ', '') + '],0) as  
      [' + REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 2, @startDate), 13), 4, 8), ' ', '') + '],
    isnull([' + REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 3, @startDate), 13), 4, 8), ' ', '') + '],0) as  
      [' + REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 3, @startDate), 13), 4, 8), ' ', '') + '],
    isnull([' + REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 4, @startDate), 13), 4, 8), ' ', '') + '],0) as  
      [' + REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 4, @startDate), 13), 4, 8), ' ', '') + '],
    isnull([' + REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 5, @startDate),  13), 4, 8), ' ', '') + '],0) as  
      [' + REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 5, @startDate), 13), 4, 8), ' ', '') + '],
    FORECASTTIMEDAYS, BILLINGRATEDAILY
    FROM
      (
      SELECT
        CLIENTNAME, PROJECTNAME, PROJECTS, SCHENTRYDATE, FORECASTTOTAL
      FROM viewprojscheduling_group
      ) AS SourceTable
    PIVOT
      (
      SUM(FORECASTTOTAL)
      FOR SCHENTRYDATE IN (' +
              QUOTENAME(REPLACE(SUBSTRING(CONVERT(varchar, @startDate, 13), 4, 8), ' ', '')) + ', ' +
              QUOTENAME(REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 1, @startDate), 13), 4, 8), ' ', '')) + ', ' +
              QUOTENAME(REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 2, @startDate), 13), 4, 8), ' ', '')) + ', ' +
              QUOTENAME(REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 3, @startDate), 13), 4, 8), ' ', '')) + ', ' +
              QUOTENAME(REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 4, @startDate), 13), 4, 8), ' ', '')) + ', ' +
              QUOTENAME(REPLACE(SUBSTRING(CONVERT(varchar, DATEADD(MONTH, 5, @startDate), 13), 4, 8), ' ', '')) + ')
      ) AS PivotTable'

execute(@sql)
4

1 に答える 1

2

このようにしてください。外から通過したくない場合は、@Startdate場所も変更する必要があることに注意してください。

CREATE PROCEDURE [dbo].[project_revenue_forecast_monthly]
@startDate datetime,

AS

Declare @sql varchar(max)
.....
于 2013-03-06T06:24:48.917 に答える