MS SQL Server Concepts and Programming Question:
Download Questions PDF

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

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 = mssql_connect('LOCALHOST','sa','GlobalGuideLine');
mssql_select_db('GlobalGuideLineDatabase', $con);

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

mssql_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 MSSQL - How To Insert Data with NULL Values?PHP MSSQL - How To Get the Number of Affected Rows?