我试图找出我尝试合并的表是否有identity
列(来自php应用程序)。正在构建的查询是:
USE dhs;
select COLUMN_NAME as name
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'dbo'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
AND TABLE_NAME = 'orders'
如果我在Management Studio中运行它,我会得到id
的返回值(这是我所期望的)。但是,当我从我的应用程序运行相同的查询时,不会返回任何内容。我已经为我的数据库用户提供了完全权限,因此它不能成为权限问题。下面是我正在运行的php(当前返回false,即使它应该返回'id'):
$array = empty($array) ? $_POST : $array; //if no array is passed, use post
$info = self::information_schema($db, $schema, $table);
$clean = array(); //the hex-exscaped key values pairs that are matched against the database column names (so only items whose key matches a column name are used)
$primary = array();// ON section array
$update = array(); //the array for elements to update, each position is set to target.$k = source.$k
$insert = array(); //The insert string, which is essentially array_keys($clean) minus any identity field with source. prepended
$fields = array(); //fields to insert to with no source. prepended and no identity fields included
$query= "USE $db
select COLUMN_NAME as name
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = '$schema'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
AND TABLE_NAME = '$table'"; //this gets the identity column of table it has one (to prevent trying to insert into an IDENTITY field)
$handle = sqlsrv_query(self::$conn, $query);
$value = sqlsrv_fetch_array($handle, SQLSRV_FETCH_NUMBERIC);
var_dump($value);
SQL Server是否允许您从SSMS外部选择INFORMATION_SCHEMA
?