Added more examples to TVP for queries without stored procedures

Jenny Tam 2021-11-25 15:36:33 -08:00
Родитель 168e59a832
Коммит 5525f617e6
1 изменённых файлов: 37 добавлений и 1 удалений

@ -374,7 +374,7 @@ Starting with 5.9.0, the default behavior of pdo_sqlsrv is to show additional OD
## Table-Valued Parameters
Starting with 5.10.0-beta1, the support for [Table-Valued Parameters](https://docs.microsoft.com/sql/relational-databases/tables/use-table-valued-parameters-database-engine) is introduced. The example of how to use Table-Valued Parameters with PHP Drivers is shown below.
Starting with 5.10.0-beta1, the support for [Table-Valued Parameters](https://docs.microsoft.com/sql/relational-databases/tables/use-table-valued-parameters-database-engine) is introduced. The example of how to use Table-Valued Parameters (TVP) with PHP Drivers is shown below.
First, create tables, a table type, and a stored procedure:
```sql
@ -511,3 +511,39 @@ If the user is not using the default DBO schema, then the schema name must be pr
$tvpInput = array($tvpType => $inputs, $schema);
```
The user may also use Table-Valued Parameters without stored procedures. Consider the following:
```sql
CREATE TYPE id_table_type AS TABLE(id INT PRIMARY KEY)
CREATE TABLE test_table (id INT PRIMARY KEY)
```
Using pdo_sqlsrv, the user may insert into test_table like this:
```
$tsql = "INSERT INTO test_table SELECT * FROM ?";
$tvpInput = array('id_table_type' => [[1], [2], [3]]);
$stmt = $conn->prepare($tsql);
$stmt->bindParam(1, $tvpInput, PDO::PARAM_LOB);
$result = $stmt->execute();
```
Similarly, using sqlsrv, except with a user defined schema, is shown below:
```
$schema = 'my schema';
$tvpName = 'id_table_type';
$tsql = "INSERT INTO [$schema].[test_table] SELECT * FROM ?";
$params = [
[[$tvpname => [[1], [2], [3]], $schema]],
];
$stmt = sqlsrv_query($conn, $tsql, $params);
if (!$stmt) {
print_r(sqlsrv_errors());
}
sqlsrv_free_stmt($stmt);
```