mirror of
https://github.com/vrana/adminer.git
synced 2025-08-12 09:34:10 +02:00
MS SQL: Don't insert identity without IDENTITY_INSERT
This commit is contained in:
@@ -192,9 +192,14 @@ if (isset($_GET["mssql"])) {
|
|||||||
$set = reset($rows);
|
$set = reset($rows);
|
||||||
$columns = "c" . implode(", c", range(1, count($set)));
|
$columns = "c" . implode(", c", range(1, count($set)));
|
||||||
$c = 0;
|
$c = 0;
|
||||||
|
$insert = array();
|
||||||
foreach ($set as $key => $val) {
|
foreach ($set as $key => $val) {
|
||||||
$c++;
|
$c++;
|
||||||
if (isset($primary[idf_unescape($key)])) {
|
$name = idf_unescape($key);
|
||||||
|
if (!$fields[$name]["auto_increment"]) {
|
||||||
|
$insert[$key] = "c$c";
|
||||||
|
}
|
||||||
|
if (isset($primary[$name])) {
|
||||||
$where[] = "$key = c$c";
|
$where[] = "$key = c$c";
|
||||||
} else {
|
} else {
|
||||||
$update[] = "$key = c$c";
|
$update[] = "$key = c$c";
|
||||||
@@ -205,12 +210,14 @@ if (isset($_GET["mssql"])) {
|
|||||||
$values[] = "(" . implode(", ", $set) . ")";
|
$values[] = "(" . implode(", ", $set) . ")";
|
||||||
}
|
}
|
||||||
if ($where) {
|
if ($where) {
|
||||||
queries("SET IDENTITY_INSERT " . table($table) . " ON");
|
$identity = queries("SET IDENTITY_INSERT " . table($table) . " ON");
|
||||||
$return = queries("MERGE " . table($table) . " USING (VALUES\n\t" . implode(",\n\t", $values) . "\n) AS source ($columns) ON " . implode(" AND ", $where) //! source, c1 - possible conflict
|
$return = queries("MERGE " . table($table) . " USING (VALUES\n\t" . implode(",\n\t", $values) . "\n) AS source ($columns) ON " . implode(" AND ", $where) //! source, c1 - possible conflict
|
||||||
. ($update ? "\nWHEN MATCHED THEN UPDATE SET " . implode(", ", $update) : "")
|
. ($update ? "\nWHEN MATCHED THEN UPDATE SET " . implode(", ", $update) : "")
|
||||||
. "\nWHEN NOT MATCHED THEN INSERT (" . implode(", ", array_keys($set)) . ") VALUES ($columns);" // ; is mandatory
|
. "\nWHEN NOT MATCHED THEN INSERT (" . implode(", ", array_keys($identity ? $set : $insert)) . ") VALUES (" . ($identity ? $columns : implode(", ", $insert)) . ");" // ; is mandatory
|
||||||
);
|
);
|
||||||
queries("SET IDENTITY_INSERT " . table($table) . " OFF");
|
if ($identity) {
|
||||||
|
queries("SET IDENTITY_INSERT " . table($table) . " OFF");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$return = queries("INSERT INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ") VALUES\n" . implode(",\n", $values));
|
$return = queries("INSERT INTO " . table($table) . " (" . implode(", ", array_keys($set)) . ") VALUES\n" . implode(",\n", $values));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user