gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:45 26 4
gpt4 key购买 nike

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

当我从 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。

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

最佳答案

有几个错误,可能会导致问题。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

关于c# - 存储过程在具有相同参数的同一数据库上给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22396895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com