]> git.joonet.de Git - adminer.git/commitdiff
MS SQL: Set identity_insert in CSV import
authorJakub Vrana <jakub@vrana.cz>
Wed, 26 Feb 2025 21:11:41 +0000 (22:11 +0100)
committerJakub Vrana <jakub@vrana.cz>
Wed, 26 Feb 2025 21:11:41 +0000 (22:11 +0100)
adminer/drivers/mssql.inc.php

index 1d170cf6d39eb8b637c22e06c7caaa1909f08c52..d559b4ef548120365a045dbbf3b3aad09638faa5 100644 (file)
@@ -187,15 +187,14 @@ if (isset($_GET["mssql"])) {
 
                function insertUpdate($table, $rows, $primary) {
                        $fields = fields($table);
+                       queries("SET IDENTITY_INSERT " . table($table) . " ON");
                        foreach ($rows as $set) {
                                $update = array();
-                               $insert = array();
                                $where = array();
                                foreach ($set as $key => $val) {
                                        $name = idf_unescape($key);
                                        if (!$fields[$name]["auto_increment"]) {
                                                $update[] = "$key = $val";
-                                               $insert[$key] = $val;
                                        }
                                        if (isset($primary[$name])) {
                                                $where[] = "$key = $val";
@@ -204,11 +203,12 @@ if (isset($_GET["mssql"])) {
                                //! can use only one query for all rows
                                if (!queries("MERGE " . table($table) . " USING (VALUES(" . implode(", ", $set) . ")) AS source (c" . implode(", c", range(1, count($set))) . ") ON " . implode(" AND ", $where) //! source, c1 - possible conflict
                                        . " WHEN MATCHED THEN UPDATE SET " . implode(", ", $update)
-                                       . " WHEN NOT MATCHED THEN INSERT (" . implode(", ", array_keys($insert)) . ") VALUES (" . implode(", ", $insert) . ");" // ; is mandatory
+                                       . " WHEN NOT MATCHED THEN INSERT (" . implode(", ", array_keys($set)) . ") VALUES (" . implode(", ", $set) . ");" // ; is mandatory
                                )) {
                                        return false;
                                }
                        }
+                       queries("SET IDENTITY_INSERT " . table($table) . " OFF");
                        return true;
                }