我有一个sql server存储过程,它在php中返回一个表并在php中格式化它。以下是处理该
的代码的相关部分$tsql_dropSP = "IF OBJECT_ID('spGetPivot', 'P') IS NOT NULL
DROP PROCEDURE spGetPivot";
$stmt1= sqlsrv_query( $conn, $tsql_dropSP);
if( $stmt1 === false )
{
echo "Error in executing statement 1.\n";
die( print_r( sqlsrv_errors(), true));
}
$tsql_createSP = "create Proc spGetPivot (
@CHR AS NVARCHAR(MAX)
)
AS
BEGIN
DECLARE @cols NVARCHAR (MAX)
DECLARE @query1 NVARCHAR(MAX)
DECLARE @queryO NVARCHAR(MAX)
SELECT @cols = COALESCE (@cols + ',[' + ChrLocus + ']', '[' + ChrLocus + ']')
FROM
(SELECT DISTINCT Chromosome+'_'+ CAST(Locus AS VARCHAR(10))ChrLocus
FROM ProgenyRawJuly14
where Chromosome in ('Gm11')
--,'Gm11', 'Gm12', 'Gm13', 'Gm14', 'Gm15', 'Gm16', 'Gm17', 'Gm18', 'Gm19', 'Gm20')
)PV
ORDER BY ChrLocus
SET @query1 = 'SELECT Strain_ID, COLNAMES as Markers, ' + @cols + '
into #temp
FROM
( -- Source data for pivoting
SELECT CONCAT(Chromosome,''_'',Locus) ChrLocus,Strain_ID,Markers, COLNAMES, sort
FROM ProgenyRawJuly14
CROSS APPLY(VALUES (1, MarkerSet1,''MarkerSet1''),(2, MarkerSet2,''MarkerSet2''),(3, Parent1_Marker,''Parent1_Marker''),(4, Parent2_MarkerA,''Parent2_MarkerA''),
(5, Parent2_MarkerB,''Parent2_MarkerB''))
AS COLUMNNAMES(Sort, Markers,COLNAMES)
UNION
SELECT CONCAT(Chromosome,''_'',Locus) ChrLocus,Strain_ID,Markers, COLNAMES, sort
FROM ParentRawTableJuly14
CROSS APPLY(VALUES (1, MarkerSet1,''MarkerSet1''),(2, MarkerSet2,''MarkerSet2''),(3, Parent1_Marker,''Parent1_Marker''),(4, Parent2_MarkerA,''Parent2_MarkerA''),
(5, Parent2_MarkerB,''Parent2_MarkerB''))
AS COLUMNNAMES(Sort, Markers,COLNAMES)
) x
PIVOT
(
--Defines the values in each dynamic columns
min(Markers)
-- Get the names from the @cols variable to show as column
FOR ChrLocus IN ('+ @cols +')
) p
order by Strain_ID, sort;
SELECT @queryO=COUNT(*) FROM tempdb.sys.columns WHERE object_id = object_id(''tempdb..#temp'');
SELECT * INTO temptable FROM #temp;'
select * from temptable;
END";
$stmt2 = sqlsrv_query($conn, $tsql_createSP);
if( $stmt2 === false )
{
echo "Error in executing statement 2.\n";
die( print_r( sqlsrv_errors(), true));
}
$getProducts="";
$params = array(
array($chrom_set,SQLSRV_PARAM_IN),
array($getProducts,SQLSRV_PARAM_OUT)
);
$tsql_callSP = "{call spGetPivot( ?,?)}";
$stmt3 = sqlsrv_query($conn,$tsql_callSP,$params);
if ( $stmt3 === false) { echo "Error in executing statement 3.\n";
die( FormatErrors( sqlsrv_errors() ) );}
$productCount = 0;
echo $stmt3;
我收到以下错误:
Error in executing statement 3.
我在网上论坛上尝试了几条建议:但都没有。我是sql server中这个存储过程部分的新手。不确定我是否正确使用变量和输出参数。