从函数调用主存储过程时,在DataTable中提供子过程结果

时间:2013-02-27 05:51:09

标签: sql-server stored-procedures

我有一个sql存储过程,它调用另一个sql存储过程。我在sql server中执行时工作正常。但是当我从类中调用函数时,它返回subprocedure返回的DataTable。我想返回主程序返回的数据。

我的主要程序:

ALTER PROCEDURE [dbo].[StoredProcedure2]

@QuaterList varchar(50) = NULL
AS
BEGIN
    DECLARE @sql nvarchar(4000)

    SELECT @sql='SELECT Id, Suggester, Requester, Date_Created from CRM.dbo.CRM_Doctor_Request WHERE 1=1 '
    If (@QuaterList) IS NOT NULL
    BEGIN
        DECLARE @MonthsList varchar(1000)
        EXEC dbo.SPGetMonthsListforMultipleQuaters  @QuaterList, @MonthsList OUTPUT
        SELECT @MonthsList
        SELECT @sql=@sql + ' AND MONTH(Date_Created) in ('
        + SUBSTRING(@MonthsList, 1, LEN(@MonthsList)-1) +')'
    END


    EXEC sp_executesql @sql, N' @QuaterList varchar(50) ', 
                    @QuaterList
END

子程序SPGetMonthsListforMultipleQuaters

ALTER PROCEDURE dbo.SPGetMonthsListforMultipleQuaters
    @InputList varchar(10),
    @MonthsList varchar(1000) OUTPUT
AS
BEGIN
    SELECT @MonthsList= ''
    /* Create temp table */
    CREATE TABLE #TempListTable
    (
        TableField int
    )
    /* add data into temp table */
    DECLARE @TableField varchar(10), @Pos int

    SET @InputList = LTRIM(RTRIM(@InputList))+ ','
    SET @Pos = CHARINDEX(',', @InputList, 1)

    IF REPLACE(@InputList, ',', '') <> ''
    BEGIN
        WHILE @Pos > 0
        BEGIN
            SET @TableField = LTRIM(RTRIM(LEFT(@InputList, @Pos - 1)))
            IF @TableField <> ''
            BEGIN
                INSERT INTO #TempListTable (TableField) VALUES (CAST(@TableField AS int)) --Use Appropriate conversion
            END
            SET @InputList = RIGHT(@InputList, LEN(@InputList) - @Pos)
            SET @Pos = CHARINDEX(',', @InputList, 1)
        END
    END 
    /* fetch data from temptable using cursor */
    DECLARE @ArrayItem nvarchar(100)
    DECLARE @Array_Cursor CURSOR

    SET @Array_Cursor = CURSOR FAST_FORWARD FOR select TableField 
    from #TempListTable

    OPEN @Array_Cursor
    FETCH NEXT FROM @Array_Cursor  INTO @ArrayItem

    WHILE @@FETCH_STATUS = 0  
    BEGIN
        /* creating list of months */

        IF (@ArrayItem = 1)
            SELECT @MonthsList=@MonthsList + '4,5,6,'
        IF (@ArrayItem = 2)
            SELECT @MonthsList=@MonthsList + '7,8,9,'
        IF (@ArrayItem = 3)
            SELECT @MonthsList=@MonthsList + '10,11,12,'
        IF (@ArrayItem = 4)
            SELECT @MonthsList=@MonthsList + '1,2,3,'

        FETCH NEXT FROM @Array_Cursor INTO @ArrayItem
    END
    /*print @MonthsList*/

    Close  @Array_Cursor
    deallocate  @Array_Cursor
END

1 个答案:

答案 0 :(得分:1)

问题解决了。这是因为我写了SELECT @MonthsList所以它也返回了那个不需要的东西,并且也造成了问题。所以我删除了它并解决了问题。

新程序代码是:

ALTER PROCEDURE [dbo].[StoredProcedure2]

@QuaterList varchar(50) = NULL
AS
BEGIN
    DECLARE @sql nvarchar(4000)

    SELECT @sql='SELECT Id, Suggester, Requester, Date_Created from CRM.dbo.CRM_Doctor_Request WHERE 1=1 '
    If (@QuaterList) IS NOT NULL
    BEGIN
        DECLARE @MonthsList varchar(1000)
        EXEC dbo.SPGetMonthsListforMultipleQuaters  @QuaterList, @MonthsList OUTPUT

        SELECT @sql=@sql + ' AND MONTH(Date_Created) in ('
        + SUBSTRING(@MonthsList, 1, LEN(@MonthsList)-1) +')'
    END


    EXEC sp_executesql @sql, N' @QuaterList varchar(50) ', 
                    @QuaterList
END