Collections:
Providing Column Names in INSERT Statements in SQL Server
How to provide column names in INSERT Statements in SQL Server?
✍: FYIcenter.com
If you don't want to specify values for columns that have default values, or you want to specify values to columns in an order different than how they are defined, you can provide a column list in the INSERT statement. If a column is omitted in the column, SQL Server applies 3 rules:
The following tutorial exercise gives you some good examples:
INSERT INTO fyi_links (url, id)
VALUES ('sqa.fyicenter.com', 103)
GO
(1 row(s) affected)
INSERT INTO fyi_links (id) VALUES (110)
GO
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'url',
table 'FyiCenterData.dbo.fyi_links'; column does
not allow nulls. INSERT fails.
The statement has been terminated.
SELECT * FROM fyi_links
GO
id url notes counts created
101 dev.fyicenter.com NULL 0 2006-04-30
102 dba.fyicenter.com NULL 0 2007-05-19
103 dba.fyicenter.com NULL NULL 2007-05-19
The first INSERT statement shows that: the order of the columns is reversed; the default value is taken for the un-specified column "created"; the NULL value is taken for the un-specified column "counts", since is has no default value defined and null is allowed.
The second INSERT statement shows the error you get for the un-specified column "url", because it has no default value, and null is not allowed.
⇒ Duplicate Key Error on Primary Key Columns in SQL Server
⇐ DEFAULT - Using Column Default Values in INSERT Statements in SQL Server
2016-11-02, 2049🔥, 0💬
Popular Posts:
What is sqlservr.exe - Process - SQL Server (SQLEX?PRESS) in SQL Server? Process sqlservr.exe is the...
What Is a Parameter File in Oracle? A parameter file is a file that contains a list of initializatio...
How To Change the Name of a Database User in SQL Server? If you want to change the name of an existi...
How To Create a Table Index in Oracle? If you have a table with a lots of rows, and you know that on...
Can Binary Strings Be Converted into NUMERIC or FLOAT Data Types in SQL Server Transact-SQL? Can bin...