我无法执行存储过程。这是一个错误

时间:2015-04-09 12:01:25

标签: sql sql-server stored-procedures

我无法执行存储过程。这是一个错误

  

列名无效' UNION ALL'。

任何人都可以提供帮助

USE [M511Symaster]
GO
/****** Object:  StoredProcedure [dbo].[CostCenterWiseBudget]    Script Date: 09/04/2015 16:59:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 ALTER PROCEDURE [dbo].[CostCenterWiseBudget]

      @CoCode  char(4),
      @DivCode char(3),
      @MinYear int,
      @Maxyear int,
      @Fromdate Date,
      @Todate Date
AS
Declare @lnLoop int,
        @Sql varchar(max)
set  @lnLoop = @MinYear
set  @Sql = ''

BEGIN
     SET NOCOUNT ON;
     while @MinYear <= @Maxyear
     BEGIN
         if @lnLoop < @MinYear
         BEGIN
            set @Sql =  @Sql + " UNION ALL "
         END
         set @Sql = "SELECT  a.cost_code,b.name, SUM(a.amount) AS Actual_amt,b.Budgeted_Amt as Budgeted_Amt,"
         set @Sql =  @Sql + " Actual_amt - Budgeted_Amt as var_Amt "
         set @Sql =  @Sql + " FROM " + @CoCode + @DivCode + ".dbo.FactTrans a,"
         set @Sql =  @Sql + @CoCode + "Symaster.dbo.CostMst b where a.cost_code = b.code AND "
         set @Sql =  @Sql + CONVERT(VARCHAR(8),@Fromdate ,1) + " <=  CONVERT(VARCHAR(8),a.Vou_dt ,1)"
         set @Sql =  @Sql + " AND "
         set @Sql =  @Sql + CONVERT(VARCHAR(8),@Todate ,1) + " <=  CONVERT(VARCHAR(8),a.Vou_dt ,1)"
         set @Sql =  @Sql + " GROUP BY a.cost_code,b.code,b.name "
         SET @MinYear = @MinYear + 1
     END
     EXEC (@Sql)
END

2 个答案:

答案 0 :(得分:3)

您应该在程序中使用单引号而不是双引号。

USE [M511Symaster]
GO
/****** Object:  StoredProcedure [dbo].[CostCenterWiseBudget]    Script Date: 09/04/2015 16:59:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 Alter PROCEDURE [dbo].[CostCenterWiseBudget]

      @CoCode  char(4),
      @DivCode char(3),
      @MinYear int,
      @Maxyear int,
      @Fromdate Date,
      @Todate Date
AS
Declare @lnLoop int,
        @Sql varchar(max)
set  @lnLoop = @MinYear
set  @Sql = ''

BEGIN
     SET NOCOUNT ON;
     while @MinYear <= @Maxyear
     BEGIN
         if @lnLoop < @MinYear
         BEGIN
            set @Sql =  @Sql + ' UNION ALL '
         END
         set @Sql = 'SELECT  a.cost_code,b.name, SUM(a.amount) AS Actual_amt,b.Budgeted_Amt as Budgeted_Amt,'
         set @Sql =  @Sql + ' Actual_amt - Budgeted_Amt as var_Amt '
         set @Sql =  @Sql + ' FROM ' + @CoCode + @DivCode + '.dbo.FactTrans a,'
         set @Sql =  @Sql + @CoCode + 'Symaster.dbo.CostMst b where a.cost_code = b.code AND '
         set @Sql =  @Sql + CONVERT(VARCHAR(8),@Fromdate ,1) + ' <=  CONVERT(VARCHAR(8),a.Vou_dt ,1)'
         set @Sql =  @Sql + ' AND '
         set @Sql =  @Sql + CONVERT(VARCHAR(8),@Todate ,1) + ' <=  CONVERT(VARCHAR(8),a.Vou_dt ,1)'
         set @Sql =  @Sql + ' GROUP BY a.cost_code,b.code,b.name '
         SET @MinYear = @MinYear + 1
     END
     EXEC (@Sql)
END

答案 1 :(得分:1)

在上面的查询&#34; (双引号)代替&#39; (单引号)。此外,UNION ALL在动态查询中放错了位置。

请改为尝试:

USE [M511Symaster]
GO
/****** Object:  StoredProcedure [dbo].[CostCenterWiseBudget]    Script Date: 09/04/2015 16:59:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 ALTER PROCEDURE [dbo].[CostCenterWiseBudget]

      @CoCode  char(4),
      @DivCode char(3),
      @MinYear int,
      @Maxyear int,
      @Fromdate Date,
      @Todate Date
AS
Declare @lnLoop int,
        @Sql varchar(max)
set  @lnLoop = @MinYear
set  @Sql = ''


BEGIN
     SET NOCOUNT ON;
     if @MinYear <= @Maxyear
     begin
         set @Sql = 'SELECT  a.cost_code,b.name, SUM(a.amount) AS Actual_amt,b.Budgeted_Amt as Budgeted_Amt,'
         set @Sql =  @Sql + ' Actual_amt - Budgeted_Amt as var_Amt '
         set @Sql =  @Sql + ' FROM ' + @CoCode + @DivCode + '.dbo.FactTrans a,'
         set @Sql =  @Sql + @CoCode + 'Symaster.dbo.CostMst b where a.cost_code = b.code AND '
         set @Sql =  @Sql + CONVERT(VARCHAR(8),@Fromdate ,1) + ' <=  CONVERT(VARCHAR(8),a.Vou_dt ,1)'
         set @Sql =  @Sql + ' AND '
         set @Sql =  @Sql + CONVERT(VARCHAR(8),@Todate ,1) + ' <=  CONVERT(VARCHAR(8),a.Vou_dt ,1)'
         set @Sql =  @Sql + ' GROUP BY a.cost_code,b.code,b.name '

         SET @MinYear = @MinYear + 1
     end

     while @MinYear <= @Maxyear
     BEGIN
         set @Sql =  @Sql + ' UNION ALL '

         set @Sql = 'SELECT  a.cost_code,b.name, SUM(a.amount) AS Actual_amt,b.Budgeted_Amt as Budgeted_Amt,'
         set @Sql =  @Sql + ' Actual_amt - Budgeted_Amt as var_Amt '
         set @Sql =  @Sql + ' FROM ' + @CoCode + @DivCode + '.dbo.FactTrans a,'
         set @Sql =  @Sql + @CoCode + 'Symaster.dbo.CostMst b where a.cost_code = b.code AND '
         set @Sql =  @Sql + CONVERT(VARCHAR(8),@Fromdate ,1) + ' <=  CONVERT(VARCHAR(8),a.Vou_dt ,1)'
         set @Sql =  @Sql + ' AND '
         set @Sql =  @Sql + CONVERT(VARCHAR(8),@Todate ,1) + ' <=  CONVERT(VARCHAR(8),a.Vou_dt ,1)'
         set @Sql =  @Sql + ' GROUP BY a.cost_code,b.code,b.name '

     END
     EXEC (@Sql)
END