0

是否可以使用连接到 Windows 服务器数据库的 SQLSRV 从 PHP 中的存储过程返回数据。目前,我的代码成功提交了查询,但没有返回任何内容。行输出甚至不运行一次的循环(即对象为空)

这是PHP

/* Set up and execute the first query. */
$sql1 = "
USE CRDCMS2PublicDevelopment
EXEC SearchForXML6b 
@SessionID = 973543,
@LineID = 892245,
@SortOrder = 'YearPublished DESC, SortTitle ASC',
@PageNumber = 1,
@RecordsPerPage = 20
";
$params1 = array( $orderId, $qty, $productId );
$stmt1 = sqlsrv_query( $conn, $sql1/*, $params1*/ );

/* Display the value of the output parameters. */   
while ( $obj = sqlsrv_fetch_object( $stmt1 ) ) {       
    //SET PARAMETERS - SET TERMS    
    echo 'loop';  
    echo $obj->listingXML;
}

/* If both queries were successful, commit the transaction. */
/* Otherwise, rollback the transaction. */
if( $stmt1 ) {
     sqlsrv_commit( $conn );
     echo "Transaction committed.<br />";
} else {
     sqlsrv_rollback( $conn );
     echo "Transaction rolled back.<br />";
}

这是存储过程

USE [ my db name ]
GO
/****** Object:  StoredProcedure [dbo].[ SPROC name ]    Script Date: 12/08/2013 14:45:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[ SPROC name ]
@DatabaseID int = 1,
@PageNumber int = 1,
@RecordsPerPage int = 20,
@SessionID int = 0,
@LineID int = 0,
@SearchFor nvarchar(max) = '',
@UserID int = 0,
@SortOrder nvarchar(max) = 'YearPublished DESC, SortTitle ASC',
@Published int = 3, -- -1=all, 0=not published, 1=prov, 2=full, 3=all published
@SearchXML nvarchar(max) = '', -- xml representation of advanced search
@ShowSelected int = 0,
@IsWebSearch int = 0
AS
BEGIN
    SET NOCOUNT ON;

    --SET @recordsPerPage = 50

    -- ----------------------------------------------------------------------------------------------
    -- don't allow a null page number or recs per page
    -- ----------------------------------------------------------------------------------------------
    if @PageNumber is null
        SET @PageNumber = 1
    if @RecordsPerPage is null
        SET @RecordsPerPage = 20

    -- ----------------------------------------------------------------------------------------------
    -- Set up the header of the SQL to run
    -- ----------------------------------------------------------------------------------------------
    DECLARE @sql nvarchar(max)
    set @sql = '
        DECLARE @sql nvarchar(max)
        DECLARE @startRow int
        DECLARE @endRow int
        SET @startRow = '+STR(((@PageNumber-1) * @RecordsPerPage)) + '
        SET @endRow = '+STR(((@PageNumber) * @RecordsPerPage)+1) + '

        DECLARE @total int

        DECLARE @t TABLE (RowNumber int IDENTITY, AccessionNumber bigint not null, SortTitle nvarchar(450), YearPublished int, DatabaseID int, RecordTypeID int, Selected int NULL, Source nvarchar(450))'



        SET @sql = @sql + '
  DECLARE @hits varchar(max)

  SELECT @hits = Hits from UserHitTemp where LineID = ' + LTRIM(RTRIM(STR(@lineID))) + '  

        insert into @t ( AccessionNumber, SortTitle , YearPublished , DatabaseID , RecordTypeID , Source)
            SELECT C.AccessionNumber, SortTitle, YearPublished, DatabaseID, RecordTypeID, ISNULL((select top 1 FieldText from SearchField where AccessionNumber = C.accessionNumber and SearchTag = ''SO''),'''') as Source 
            FROM dbo.SplitAccessionList(@hits) H
            JOIN CRDDocument C on C.AccessionNumber = H.AccessionNumber ORDER BY ' + @SortOrder



    SET @sql = @sql + '
        set @total = @@ROWCOUNT

        DECLARE @xml XML
        SET @xml = (
        SELECT TOP 1 @total as "@RecordCount",
            0 as "@SelectedCount",
            '+LTRIM(RTRIM(STR(@SessionID)))+' as "@SearchSessionID",
            '+LTRIM(RTRIM(STR(@PageNumber)))+' as "@PageNumber",
            '+LTRIM(RTRIM(STR(@RecordsPerPage)))+' as "@RecordsPerPage",
            '+LTRIM(RTRIM(STR(@LineID)))+' as "@LineID",
            ''advanced)'' as "@SearchType",

            (   SELECT dbo.GetRecordForListingXML(AccessionNumber, RowNumber,ISNULL(Selected,0)) 
                FROM @t T
                WHERE T.RowNumber > @startRow and T.RowNumber < @endRow

                FOR XML PATH(''''), ELEMENTS, TYPE
            )
            FOR XML PATH(''listing''), ELEMENTS
        )

        SELECT @XML as listingXML'

    -- ----------------------------------------------------------------------------------------------
    -- Run the SQL to extract the results as XML
    -- ----------------------------------------------------------------------------------------------

    PRINT @sql

    EXEC sp_executesql @sql

END

请注意:如果我复制并粘贴 php ($sql1) 中使用的确切 sql,它会在 SQL Server Management Studio 中正确返回一行。我认为我正在尝试可以完成,这是正确的方法吗?

谢谢。

4

1 回答 1

0

看起来所有存储过程都在构建变量@sql。

尝试在存储过程的底部添加:

EXEC @sql
于 2013-08-12T17:22:49.750 回答