MS SQL Server Concepts and Programming Question:
Download Questions PDF

PHP MSSQL - How To Insert Data into an Existing Table?

MS SQL Server Interview Question
MS SQL Server Interview Question

Answer:

If you want to insert a row of data into an existing table, you can use the INSERT INTO statement as shown in the following sample script:

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

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

mssql_close($con);
?>

If you run this script, unfortunately, you will get an error:

SQL statement failed with error:
The statement has been terminated.

So what is wrong with the statement? The error message does not give any details. You need to take this statement to SQL Server Management Studio to try it:

USE GlobalGuideLineDatabase
GO

INSERT INTO ggl_links (id, url) VALUES (
101, 'www.GlobalGuideLine.com')
GO
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'notes',
table 'GlobalGuideLineDatabase.dbo.ggl_links

Download MS SQL Server Interview Questions And Answers PDF

Previous QuestionNext Question
PHP MSSQL - How To Drop an Existing Table?PHP MSSQL - How To Make a Column Nullable?