MS SQL Server Concepts and Programming Question:
Download Questions PDF

PHP ODBC - How To Insert Multiple Rows with a subquery?

MS SQL Server Interview Question
MS SQL Server Interview Question

Answer:

If want to insert rows into a table based on data rows from other tables, you can use a subquery inside the INSERT statement as shown in the following script example:

<?php
$con = odbc_connect('ggl_SQL_SERVER','sa','GlobalGuideLine');

$sql = "INSERT INTO ggl_links"
. " SELECT id+1000, REVERSE(url), notes, counts, time"
. " FROM ggl_links";
$res = odbc_exec($con, $sql);
if (!$res) {
print("SQL statement failed with error: ");
print(odbc_error($con).": ".odbc_errormsg($con)." ");
} else {
print("Multiple rows inserted. ");
}

odbc_close($con);

If you run this script, the table should have 4 rows now. And you will get:

Multiple rows inserted


Download MS SQL Server Interview Questions And Answers PDF

Previous QuestionNext Question
PHP ODBC - How To Insert Data into an Existing Table?PHP ODBC - How To Get the Number of Affected Rows?