sqlsrv_field_metadata
检索预定义语句的字段的元数据。有关如何预定义语句的信息,请参阅 sqlsrv_query 或 sqlsrv_prepare。请注意,可以在执行任何预定义语句之前或之后对其调用 sqlsrv_field_metadata。
语法
sqlsrv_field_metadata( resource $stmt)
参数
$stmt:查找其字段元数据的语句资源。
返回值
数组的 array 或 false。结果集中的每个字段在该数组中都有一个对应的数组。每个子数组都有键,如下表中所述。如果检索字段元数据时出现错误,则返回 false。
键 | 说明 |
---|---|
Name |
该字段对应的列的名称。 |
Type |
与 SQL 类型对应的数值。 |
Size |
对于字符类型(char(n)、varchar(n)、nchar(n)、nvarchar(n)、XML)的字段,为字符数。对于二进制类型(binary(n)、varbinary(n)、UDT)的字段,为字节数。对于其他 SQL Server 数据类型,为 NULL。 |
Precision |
可变精度类型(real、numeric、decimal、datetime2、datetimeoffset 和 time)的精度。对于其他 SQL Server 数据类型,为 NULL。 |
Scale |
可变小数位数类型(numeric、decimal、datetime2、datetimeoffset 和 time)的小数位数。对于其他 SQL Server 数据类型,为 NULL。 |
Nullable |
一个枚举值,指示该列可以为 Null (SQLSRV_NULLABLE_YES),该列不可以为 Null (SQLSRV_NULLABLE_NO),或者不知道该列是否可以为 Null (SQLSRV_NULLABLE_UNKNOWN)。 |
下表提供有关各个子数组的键的详细信息(有关这些类型的详细信息,请参阅 SQL Server 文档):
SQL Server 2008 数据类型 | 类型 | 最小/最大精度 | 最小/最大小数位数 | Size |
---|---|---|---|---|
bigint |
-5 |
8 |
||
binary |
-2 |
0 < n < 8000 1 |
||
bit |
-7 |
|||
char |
1 |
0 < n < 8000 1 |
||
date |
91 |
10/10 |
0/0 |
|
datetime |
93 |
23/23 |
3/3 |
|
datetime2 |
93 |
19/27 |
0/7 |
|
datetimeoffset |
-155 |
26/34 |
0/7 |
|
decimal |
3 |
1/38 |
0/精度值 |
|
float |
6 |
4/8 |
||
image |
-4 |
2 GB |
||
int |
4 |
|||
money |
3 |
19/19 |
4/4 |
|
nchar |
-8 |
0 < n < 4000 1 |
||
ntext |
-10 |
1 GB |
||
numeric |
2 |
1/38 |
0/精度值 |
|
nvarchar |
-9 |
0 < n < 4000 1 |
||
real |
7 |
4/4 |
||
smalldatetime |
93 |
16/16 |
0/0 |
|
smallint |
5 |
2 字节 |
||
smallmoney |
3 |
10/10 |
4/4 |
|
text |
-1 |
2 GB |
||
time |
-154 |
8/16 |
0/7 |
|
timestamp |
-2 |
8 字节 |
||
tinyint |
-6 |
1 字节 |
||
udt |
-151 |
变量 |
||
uniqueidentifier |
-11 |
16 |
||
varbinary |
-3 |
0 < n < 8000 1 |
||
varchar |
12 |
0 < n < 8000 1 |
||
xml |
-152 |
0 |
(1) 零 (0) 指示允许最大大小。
可为 Null 的键可以是 yes 或 no。
示例
下面的示例创建一个语句资源,然后检索并显示字段元数据。此示例假定本地计算机上已安装 SQL Server 和 AdventureWorks 数据库。从命令行运行此示例时,所有的输出都将写入控制台。
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
$serverName = "(local)";
$connectionInfo = array( "Database"=>"AdventureWorks");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Prepare the statement. */
$tsql = "SELECT ReviewerName, Comments FROM Production.ProductReview";
$stmt = sqlsrv_prepare( $conn, $tsql);
/* Get and display field metadata. */
foreach( sqlsrv_field_metadata( $stmt) as $fieldMetadata)
{
foreach( $fieldMetadata as $name => $value)
{
echo "$name: $value\n";
}
echo "\n";
}
/* Note: sqlsrv_field_metadata can be called on any statement
resource, pre- or post-execution. */
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>