PHP MSSQL - How To Insert Multiple Rows with a subquery?
Submitted by: AdministratorIf 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
Submitted by: Administrator
<?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
Submitted by: Administrator
Read Online MS SQL Server Job Interview Questions And Answers
Top MS SQL Server Questions
☺ | What Happens If Strings Are Casted into Wrong Code Pages in MS SQL Server? |
☺ | How To Provide Default Values to Function Parameters? |
☺ | What are system databases in MS SQL Server? |
☺ | PHP MSSQL - How To Display a Past Time in Days, Hours and Minutes? |
☺ | How To Use Subqueries with the EXISTS Operators in MS SQL Server? |
Top Databases Programming Categories
☺ | RDBMS Interview Questions. |
☺ | SQL Interview Questions. |
☺ | SSRS Interview Questions. |
☺ | Database Administrator (DBA) Interview Questions. |
☺ | Sybase Interview Questions. |