无法通过sql server和代码执行存储过程

本文关键字:代码 执行 存储过程 server sql | 更新日期: 2023-09-27 17:59:07

这是我的存储过程。当我在SQL Server中执行以下脚本时,我得到了以下错误:

超过了最大存储过程、函数、触发器或视图嵌套级别(限制32)。

代码:

Create Procedure [dbo].[GetTotalProductWcnCrashes]
(
   @DroidbugCount int,
   @totalproductcrashes int out,
   @Uniqueproductcrashes int out,
   @SnsPL varchar(500) out,
   @Startdate datetime out,
   @Metabuild varchar(500) out,
   @Rivabuild varchar(500) out,
   @Appsbuild varchar(500) out,
   @toatalWcnCrashes int out,
   @UniqueWcnCrashes int out,
   @TotalHours numeric(18,0) OUT,   
   @Mtbf numeric(18,4) OUT,
   @Cpth numeric(18,4) OUT  
) 
AS
BEGIN
   SET NOCOUNT ON;
   DECLARE
      @SQL nvarchar(max),  
      @SQLString nvarchar(max),
      @ParmDefinition nvarchar(max),
      @totalproductcrashes1 int,
      @Uniqueproductcrashes1 int ,
      @SnsPL1 varchar(500),
      @Startdate1 datetime,
      @Metabuild1 varchar(500),
      @Rivabuild1 varchar(500),
      @Appsbuild1 varchar(500),
      @toatalWcnCrashes1 int,
      @UniqueWcnCrashes1 int,
      @TotalHours1 numeric(18,0),   
      @Mtbf1 numeric(18,4),
      @Cpth1 numeric(18,4),
      @DroidbugCount1 INT         
   SET @SQL = N'SELECT @SnsPL =   SnsPl.PL, @Startdate = SnsHeader.Startdate, @Metabuild = SnsHeader.Metabuild, @Rivabuild = SnsHeader.RivaBuild, @Appsbuild = SnsHeader.Appsbuild, @totalproductcrashes = SUM(CASE WHEN dbo.SnsResult.Crash like %Yes% THEN 1 ELSE 0 END),
  @Uniqueproductcrashes = SUM(CASE WHEN dbo.SnsResult.UniqueCrash like %Yes% AND dbo.SnsResult.CR_ID is NULL THEN 1 ELSE 0 END),
  @TotalHours = Sum(CAST(dbo.SnsResult.ResultDuration AS int))
  FROM dbo.SnsHeader
  INNER JOIN dbo.SnsResult ON dbo.SnsHeader.SnsHeader_PK = dbo.SnsResult.SnsHeader_FK 
  INNER JOIN dbo.SnsPl ON dbo.SnsHeader.PL_FK = dbo.SnsPl.PL_PK 
  GROUP BY dbo.SnsPl.PL , SnsHeader.Startdate ,SnsHeader.Metabuild,SnsHeader.RivaBuild,SnsHeader.Appsbuild'
SET @SQLString= N'EXEC GetTotalProductWcnCrashes @DroidbugCount,
@totalproductcrashes OUTPUT,
@Uniqueproductcrashes OUTPUT,
@SnsPL OUTPUT,
@Startdate OUTPUT,
@Metabuild OUTPUT,
@Rivabuild OUTPUT,
@Appsbuild OUTPUT,
@toatalWcnCrashes OUTPUT,
@UniqueWcnCrashes OUTPUT,
@TotalHours OUTPUT, 
@Mtbf OUTPUT,
@Cpth OUTPUT'

SET @ParmDefinition = N'@DroidbugCount int,
@totalproductcrashes INT OUTPUT,
@Uniqueproductcrashes INT OUTPUT,
@SnsPL varchar(500) OUTPUT,
@Startdate datetime OUTPUT,
@Metabuild varchar(500) OUTPUT,
@Rivabuild varchar(500) OUTPUT,
@Appsbuild varchar(500) OUTPUT,
@toatalWcnCrashes INT OUTPUT,
@UniqueWcnCrashes INT OUTPUT,
@TotalHours numeric(18,0) OUTPUT,   
@Mtbf numeric(18,4) OUTPUT,
@Cpth numeric(18,4) OUTPUT'  

SET @DroidbugCount1 = 1
EXECUTE sp_executesql
@SQLString,
@ParmDefinition,
@DroidbugCount=@DroidbugCount1,
@totalproductcrashes = @totalproductcrashes1 OUTPUT ,
@Uniqueproductcrashes = @Uniqueproductcrashes1 OUTPUT,
@SnsPL  = @SnsPL1 OUTPUT,
@Startdate = @Startdate1 OUTPUT ,
@Metabuild = @Metabuild1 OUTPUT,
@Rivabuild = @Rivabuild1 OUTPUT,
@Appsbuild = @Appsbuild1 OUTPUT,
@toatalWcnCrashes = @toatalWcnCrashes1 OUTPUT,
@UniqueWcnCrashes = @UniqueWcnCrashes1 OUTPUT,
@TotalHours = @TotalHours1 OUTPUT,  
@Mtbf = @Mtbf1 OUTPUT,
@Cpth = @Cpth1 OUTPUT

       SELECT   @totalproductcrashes1 as N'@totalproductcrashes',
    @Uniqueproductcrashes1 as N'@Uniqueproductcrashes',
    @SnsPL1 as N'@SnsPL',
    @Startdate1 as N'@Startdate',
    @Metabuild1 as N'@Metabuild',
    @Rivabuild1 as N'@Rivabuild',
    @Appsbuild1 as N'@Appsbuild',
    @toatalWcnCrashes1 as N'@toatalWcnCrashes',
    @UniqueWcnCrashes1 as N'@UniqueWcnCrashes',
    @TotalHours1 as N'@TotalHours',
    @Mtbf1 as N'@Mtbf',
    @Cpth1 as N'@Cpth'  
  END

有人帮我纠正上面的存储过程,我是写存储过程的新手。

以及如何从C#代码调用上述存储过程

提前谢谢。

无法通过sql server和代码执行存储过程

您的存储过程GetTotalProductWcnCrashes在一个无休止的循环中调用自己。您是如何生成此存储过程的。

全面了解存储过程后,您只需要第一个select语句——当您选择要执行的存储过程时,存储过程的其余部分看起来像是SQL Management Studio中自动生成的代码。由于某种原因,它被保存在存储过程的顶部。

您的存储过程应该是这样的。但请记住,我不是你的数据库。

USE [Reports]
 GO
 SET ANSI_NULLS ON
 GO
 SET QUOTED_IDENTIFIER ON
 GO

Create Procedure [dbo].[GetTotalProductWcnCrashes]
(
@DroidbugCount int,
@totalproductcrashes int out,
@Uniqueproductcrashes int out,
@SnsPL varchar(500) out,
@Startdate datetime out,
@Metabuild varchar(500) out,
@Rivabuild varchar(500) out,
@Appsbuild varchar(500) out,
@toatalWcnCrashes int out,
@UniqueWcnCrashes int out,
@TotalHours numeric(18,0) OUT,  
@Mtbf numeric(18,4) OUT,
@Cpth numeric(18,4) OUT 
 ) 
 AS
 BEGIN
SET NOCOUNT ON;

SELECT @SnsPL =   SnsPl.PL,@Startdate=SnsHeader.Startdate,@Metabuild=SnsHeader.Metabuild,
  @Rivabuild=SnsHeader.RivaBuild,@Appsbuild=SnsHeader.Appsbuild,
  @totalproductcrashes = SUM(CASE WHEN dbo.SnsResult.Crash like '%Yes%' THEN 1 ELSE 0 END),
  @Uniqueproductcrashes = SUM(CASE WHEN dbo.SnsResult.UniqueCrash like '%Yes%' AND dbo.SnsResult.CR_ID is NULL THEN 1 ELSE 0 END),
  @TotalHours = Sum(CAST(dbo.SnsResult.ResultDuration AS int))
  FROM dbo.SnsHeader
  INNER JOIN dbo.SnsResult ON dbo.SnsHeader.SnsHeader_PK = dbo.SnsResult.SnsHeader_FK 
  INNER JOIN dbo.SnsPl ON dbo.SnsHeader.PL_FK = dbo.SnsPl.PL_PK 
  GROUP BY dbo.SnsPl.PL , SnsHeader.Startdate ,SnsHeader.Metabuild,SnsHeader.RivaBuild,SnsHeader.Appsbuild
  END
  GO

请记住,我没有数据库,我举的这个例子可能不正确,但可以肯定的是,你的存储过程是不正确的,并且会有争议地调用自己,最高可达32个嵌套调用计数。

此外,为了回答您在评论中的问题,请关注这个stackoverflow链接,了解如何使用c#调用存储过程。

如何在C#程序中执行存储过程