MS SQL Server Concepts and Programming Question:
Download Questions PDF

PHP MSSQL - How To Insert Data with NULL Values?

MS SQL Server Interview Question
MS SQL Server Interview Question

Answer:

There are two ways to provide NULL value to a column in an INSERT statement:

* Include the column in the statement, but specify keyword NULL as the value.
* Exclude the column from the statement.

The following tutorial exercise inserts two rows. Both of them have NULL values. But they were inserted differently:

<?php
$con = mssql_connect('LOCALHOST','sa','GlobalGuideLine');
mssql_select_db('GlobalGuideLineDatabase', $con);

$sql = "INSERT INTO ggl_links"
. " (id, url, notes, counts, time)"
. " VALUES (101, 'www.GlobalGuideLine.com',"
. " NULL, NULL, NULL)";
$res = mssql_query($sql,$con);
if (!$res) {
print("SQL statement failed with error: ");
print(" ".mssql_get_last_message()." ");
} else {
print("One data row inserted. ");
}

$sql = "INSERT INTO ggl_links (id, url) VALUES ("
. " 102, 'www.GlobalGuideLine.com')";
$res = mssql_query($sql,$con);
print("Another data row inserted. ");

mssql_close($con);
?>

If you run this script, you will get:

One data row inserted.
Another data row inserted.

Download MS SQL Server Interview Questions And Answers PDF

Previous QuestionNext Question
PHP MSSQL - How To Make a Column Nullable?PHP MSSQL - How To Insert Multiple Rows with a subquery?