存储过程在具有相同参数的同一数据库上给出不同的结果

时间:2014-03-14 05:30:41

标签: c# sql entity-framework stored-procedures edmx

我有一个存储过程,只在特定情况下给出不同的结果。

当我从SQL Server Management Studio 2008 R2调用它时,它会将0作为输出。

当我从C#类文件中调用它时。它给我1作为输出。

我正在使用edmx文件,并且肯定会更新。

来自SSMS [SQL Server Management Studio]的调用如下所示

exec proc_GetPrimaryKeyUsageCount 62, 'tblFormula'

这使输出为0

从C#文件调用相同的存储过程如下

_db.GetPrimaryKeyUsageCount(62, "tblFormula");

这使输出为1

存储过程是

CREATE PROCEDURE proc_GetPrimaryKeyUsageCount (  
 @PrimaryKeyColumnId INT  
 ,@PrimaryKeyTable NVARCHAR(max)  
 --,@Response INT OUTPUT  
 )  
AS  
BEGIN  
 DECLARE @counter INT  
 DECLARE @sqlCommand NVARCHAR(max)  
 DECLARE @ForeignKey TABLE (  
  child_table VARCHAR(max)  
  ,child_fk_column VARCHAR(max)  
  )  
 DECLARE @child_table VARCHAR(max)  
 DECLARE @child_fk_column VARCHAR(max)  

 SET @counter = 0  

 INSERT INTO @ForeignKey  
 SELECT child_table = c.TABLE_NAME  
  ,child_fk_column = c.COLUMN_NAME  
 FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE p  
 INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS pc ON pc.UNIQUE_CONSTRAINT_SCHEMA = p.CONSTRAINT_SCHEMA  
  AND pc.UNIQUE_CONSTRAINT_NAME = p.CONSTRAINT_NAME  
 INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c ON c.CONSTRAINT_SCHEMA = pc.CONSTRAINT_SCHEMA  
  AND c.CONSTRAINT_NAME = pc.CONSTRAINT_NAME  
 WHERE EXISTS (  
   SELECT 1  
   FROM INFORMATION_SCHEMA.COLUMNS  
   WHERE COLUMN_NAME = 'IsDeleted'  
    AND TABLE_SCHEMA = p.TABLE_SCHEMA  
    AND TABLE_NAME = p.TABLE_NAME  
    AND p.TABLE_NAME = @PrimaryKeyTable  
   )  

 DECLARE db_cursor CURSOR  
 FOR  
 SELECT child_table  
  ,child_fk_column  
 FROM @ForeignKey  

 OPEN db_cursor  

 FETCH NEXT  
 FROM db_cursor  
 INTO @child_table  
  ,@child_fk_column  

 WHILE @@FETCH_STATUS = 0  
 BEGIN  
  PRINT 'select count(*) from ' + CAST(@child_table AS VARCHAR) + ' where ' + CAST(@child_fk_column AS VARCHAR) + ' = ' + CAST(@PrimaryKeyColumnId AS VARCHAR)  

  SET @sqlCommand = 'select @cnt=count(*) from ' + CAST(@child_table AS VARCHAR) + ' where ' + CAST(@child_fk_column AS VARCHAR) + ' = ' + CAST(@PrimaryKeyColumnId AS VARCHAR)  

  EXEC sp_executesql @sqlCommand  
   ,N'@cnt int OUTPUT'  
   ,@cnt = @counter OUTPUT  

  IF @counter > 0  
   BREAK  

  FETCH NEXT  
  FROM db_cursor  
  INTO @child_table  
   ,@child_fk_column  
 END  

 SELECT @counter AS [PrimaryKeyUsageCount]  
END  

第一个参数是主键的Id,第二个参数是具有该主键的表的名称。

过程返回同一数据库中其他表中主键使用的计数。如果它甚至发现1次出现,它将返回该计数,否则为0。

如果需要额外的东西,请告诉我。

1 个答案:

答案 0 :(得分:0)

有几个错误,可能会导致问题。 INSERT应该是这样的:

INSERT INTO @ForeignKey  
 SELECT c.TABLE_NAME,c.COLUMN_NAME  
 FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE p  
 INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS pc ON pc.UNIQUE_CONSTRAINT_SCHEMA = p.CONSTRAINT_SCHEMA  
  AND pc.UNIQUE_CONSTRAINT_NAME = p.CONSTRAINT_NAME  
 INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c ON c.CONSTRAINT_SCHEMA = pc.CONSTRAINT_SCHEMA  
  AND c.CONSTRAINT_NAME = pc.CONSTRAINT_NAME  
 WHERE EXISTS (  
   SELECT *  
   FROM INFORMATION_SCHEMA.COLUMNS AS isc 
   WHERE isc.COLUMN_NAME = 'IsDeleted'  
    AND isc.TABLE_SCHEMA = p.TABLE_SCHEMA  
    AND isc.TABLE_NAME = p.TABLE_NAME  
    AND p.TABLE_NAME = @PrimaryKeyTable  
   ) 

光标循环后应该是:

CLOSE db_cursor
DEALLOCATE db_cursor