从一个存储过程返回值到另一个存储过程

时间:2017-10-31 03:36:33

标签: c# asp.net sql-server stored-procedures

我想将一个存储过程的返回值用于另一个存储过程。我在互联网上搜索并尝试了几种解决方案,但所有这些解决方案都不起作用,我不知道我犯了什么错误。

我想使用其返回值的存储过程是:

CREATE PROCEDURE dbo.pro_ForeignKeyCheck
     (@tableName VARCHAR(100),
      @columnName VARCHAR(100),
      @idValue INT) 
AS BEGIN
    SET NOCOUNT ON

    DECLARE fksCursor CURSOR FAST_FORWARD FOR 
        SELECT 
            tc.table_name, ccu.column_name
        FROM
            information_schema.table_constraints tc 
        JOIN
            information_schema.constraint_column_usage ccu ON tc.constraint_name = ccu.constraint_name 
        JOIN
            information_schema.referential_constraints rc ON tc.constraint_name = rc.constraint_name 
        JOIN
            information_schema.table_constraints tc2 ON rc.unique_constraint_name = tc2.constraint_name 
        JOIN
            information_schema.constraint_column_usage ccu2 ON tc2.constraint_name = ccu2.constraint_name 
        WHERE
            tc.constraint_type = 'Foreign Key' 
            AND tc2.table_name = @tableName 
            AND ccu2.column_name = @columnName
        ORDER BY
            tc.table_name

    DECLARE @fkTableName VARCHAR(100),
            @fkColumnName VARCHAR(100),
            @fkFound BIT,
            @params NVARCHAR(100),
            @sql NVARCHAR(500)

    OPEN fksCursor

    FETCH NEXT FROM fksCursor INTO @fkTableName, @fkColumnName

    SET @fkFound = 0
    SET @params = N'@fkFound BIT OUTPUT'

    WHILE @@fetch_status = 0 AND COALESCE(@fkFound, 0) <> 1 
    BEGIN
        SELECT @sql = 'set @fkFound = (select top 1 1 from [' + @fkTableName + '] where [' + @fkColumnName + '] = ' + cast(@idValue as varchar(10)) + ')'
        PRINT @sql

        EXEC sp_executesql @sql, @params, @fkFound OUTPUT

        FETCH NEXT FROM fksCursor INTO @fkTableName, @fkColumnName
    END

    CLOSE fksCursor
    DEALLOCATE fksCursor

    SELECT COALESCE(@fkFound, 0)
    RETURN 0
END

这用于检查所有子表中是否使用主键值,我们称之为

EXECUTE  pro_ForeignKeyCheck 'tablename','columnName', 1

EXECUTE  pro_ForeignKeyCheck @tablename = 'tablename', @columnName = 'columnName', @idValue = 1

它会工作,但我不能在其他存储过程中使用返回值

CREATE  PROCEDURE [dbo].[pro_Delete_acount]
    @UserID int,
    @Action NVARCHAR(10)
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE  @count int , @ErrNo int , @ErrMsg varchar(2000), @exit int



    set @exit=0
    if @Action ='ADMIN'
    begin
    /*---------Call Store Procedure pro_ForeignKeyCheck to check if there is value are exit in child table*/
    --EXEC  @exit = pro_ForeignKeyCheck1 @tablename='tb_M_admin',@columnName='admin_id',@idValue= @UserID 
    --select @exit

    --EXEC   @exit = pro_ForeignKeyCheck  @tablename='tb_M_admin',@columnName='admin_id',@idValue= 1

    EXEC @exit = pro_ForeignKeyCheck 'tb_M_admin','admin_id', 0 
    --select @exit

    select -2[Ret_Status],@exit[ErrNo],0[ErrMsg] -- test 

    end
end

有人可以帮我吗?

全部谢谢

3 个答案:

答案 0 :(得分:1)

添加

@Status int OUTPUT
<{1>}中的

因此它以

开头
pro_ForeignKeyCheck

并在结尾处执行如下

CREATE PROCEDURE dbo.pro_ForeignKeyCheck1
@tableName VARCHAR(100),
@columnName VARCHAR(100),
@idValue int,
@Status int OUTPUT

停止最后一行并添加新的

在另一个存储过程中,按如下方式调用

--select coalesce(@fkFound,0)
select @Status = coalesce(@fkFound,0)
--return 0

现在将使用返回值。

感谢所有

答案 1 :(得分:0)

EXEC @exit = pro_ForeignKeyCheck 'tb_M_admin','admin_id', 0 

---
select coalesce(@fkFound,0)
return 0 --< this will be assigned to @exit

替换此代码
return IsNull(@fkFound, 0)

您可以将select留给其他用途,但不会影响RETURN值。所以你也可以删除它。

答案 2 :(得分:0)

这是一个SP从另一个SP调用的简单示例。我希望它会给你一些想法。

create procedure dbo.first_proc
@bd datetime,
@d int output
as
select @d= DATEDIFF(day,@bd,getdate()) 
go

create procedure dbo.sec_proc
@birthday datetime
as
declare @days int
exec dbo.first_proc @birthday, @days output

select 'you live '+cast(@days as varchar) + ' days' result
go

exec dbo.sec_proc '1959-09-17'