Answers

Question and Answer:

  Home  MS SQL Server

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

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

 172 views

More Questions for you: