|
| 1 | +--TEST-- |
| 2 | +Test sql_variant as an output parameter |
| 3 | +--DESCRIPTION-- |
| 4 | +Since output param is not supported for sql_variant columns, this test verifies a proper error message is returned |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | +include 'MsCommon.inc'; |
| 8 | + |
| 9 | +function TestSimpleSelect($conn, $tableName) |
| 10 | +{ |
| 11 | + $count = 0; |
| 12 | + |
| 13 | + $stmt = $conn->prepare("SELECT ? = COUNT(* ) FROM $tableName"); |
| 14 | + $stmt->bindParam( 1, $count, PDO::PARAM_INT, 4 ); |
| 15 | + $stmt->execute(); |
| 16 | + echo "Number of rows: $count\n"; |
| 17 | + |
| 18 | + $value = 'xx'; |
| 19 | + |
| 20 | + $stmt = $conn->prepare("SELECT ? = c2_variant FROM $tableName"); |
| 21 | + $stmt->bindParam( 1, $value, PDO::PARAM_STR, 50 ); |
| 22 | + $stmt->execute(); |
| 23 | + echo "Variant column: $value\n\n"; |
| 24 | + |
| 25 | +} |
| 26 | + |
| 27 | +function CreateVariantTable($conn, $tableName) |
| 28 | +{ |
| 29 | + try |
| 30 | + { |
| 31 | + $stmt = $conn->exec("CREATE TABLE [$tableName] ([c1_int] int, [c2_variant] sql_variant)"); |
| 32 | + } |
| 33 | + catch (Exception $e) |
| 34 | + { |
| 35 | + echo "Failed to create a test table\n"; |
| 36 | + echo $e->getMessage(); |
| 37 | + } |
| 38 | + |
| 39 | + $tsql = "INSERT INTO [$tableName] ([c1_int], [c2_variant]) VALUES (1, ?)"; |
| 40 | + |
| 41 | + $data = "This is to test if sql_variant works with output parameters"; |
| 42 | + |
| 43 | + $stmt = $conn->prepare($tsql); |
| 44 | + $result = $stmt->execute(array($data)); |
| 45 | + if (! $result) |
| 46 | + echo "Failed to insert data\n"; |
| 47 | +} |
| 48 | + |
| 49 | +function RunTest() |
| 50 | +{ |
| 51 | + StartTest("pdo_param_output_select_variant"); |
| 52 | + try |
| 53 | + { |
| 54 | + include("MsSetup.inc"); |
| 55 | + // Connect |
| 56 | + $conn = new PDO( "sqlsrv:server=$server;Database=$databaseName", $uid, $pwd); |
| 57 | + $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); |
| 58 | + echo "\n"; |
| 59 | + |
| 60 | + // Now test with another stored procedure |
| 61 | + $tableName = GetTempTableName(); |
| 62 | + CreateVariantTable($conn, $tableName); |
| 63 | + |
| 64 | + // Test a simple select to get output |
| 65 | + TestSimpleSelect($conn, $tableName); |
| 66 | + |
| 67 | + $conn = null; |
| 68 | + } |
| 69 | + catch (Exception $e) |
| 70 | + { |
| 71 | + echo $e->getMessage(); |
| 72 | + } |
| 73 | + echo "\nDone\n"; |
| 74 | + EndTest("pdo_param_output_select_variant"); |
| 75 | +} |
| 76 | + |
| 77 | +RunTest(); |
| 78 | + |
| 79 | +?> |
| 80 | +--EXPECT-- |
| 81 | + |
| 82 | +Number of rows: 1 |
| 83 | +SQLSTATE[42000]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Implicit conversion from data type sql_variant to nvarchar(max) is not allowed. Use the CONVERT function to run this query. |
| 84 | +Done |
| 85 | +Test "pdo_param_output_select_variant" completed successfully. |
0 commit comments