在另一个程序中调用一个程序

时间:2016-04-26 13:57:59

标签: sql-server stored-procedures

我创建了一个存储过程来检查列是否有值。如果列有值,则返回1,否则返回-1 以下是代码:

CREATE PROCEDURE [dbo].[sp_addPermissions](@groupName NVARCHAR(20), @column NVARCHAR(20))
AS
    DECLARE @SQL NVARCHAR(MAX)
    DECLARE @CountDef NVARCHAR(MAX)
    DECLARE @FieldCount INT

    IF EXISTS(SELECT * FROM addPermissions WHERE groupName = @groupName)
    BEGIN
        IF EXISTS(SELECT * FROM sys.columns WHERE NAME = @column AND OBJECT_ID = OBJECT_ID(N'addPermissions'))
        BEGIN 
                SET @SQL=N'SELECT @Count=count(*) 
                         FROM addPermissions 
                         WHERE '+ quotename(@column) + ' IS NOT NULL
                         AND groupName = @groupName'

                         EXEC sys.sp_executesql @SQL, N'@groupName VARCHAR(20), @Count INT OUTPUT' , 
                             @groupName= @groupName, 
                             @Count = @FieldCount OUTPUT
                         --SELECT(@SQL)
                         select @FieldCount
                         IF(@FieldCount = 0)
                         BEGIN
                              RETURN -1
                         END
                         ELSE
                         BEGIN
                              RETURN 1
                         END
        END
        ELSE
        BEGIN
            RAISERROR('Column does not exist', 16, 1)
        END
    END
    ELSE
    BEGIN
        RAISERROR('Group do not have permission', 16, 1)
    END

我创建了第二个调用第一个过程的过程 的代码:

CREATE PROCEDURE getAddPermissions(@username NVARCHAR(40), @column NVARCHAR(20))
AS
    DECLARE @sSQL NVARCHAR(20)
    IF EXISTS(SELECT * FROM userLogin WHERE username = @username)
    BEGIN
        SELECT @sSQL = userGroup.groupName FROM userGroup 
        INNER JOIN userLogin
        ON userGroup.groupName = userLogin.groupName
        WHERE userLogin.username = @username

        EXEC sp_addPermissions @sSQL, @column
    END
    ELSE
    BEGIN
        PRINT 'User ' + @username + ' does not exist.'
    END

当我执行第一个程序时,它工作正常,它返回1或-1。但是当我在第二个过程中执行第一个过程时它返回0,它不会返回1或-1。

1 个答案:

答案 0 :(得分:1)

var unlisten = browserHistory.listen(fn)

// ...some time later...
unlisten()