SQL - Data modification - Part 5 - Inserting default values in SQL
Haziran 8, 2013 by sql tutorial
|
|
Article Information
This SQL Tutorial contains and tries to cover following subjects:
- Explanation of Inserting default values in sql.
- Example to Inserting default values in sql
Articles tries to provide answer to following questions
- How to insert default values into table in sql?
- How to add a row with predefined set of values in sql?
Article covers followings indirectly:
- SQL insert command
- SQL insert select
- DEFAULT values
Articles pre-requisites following information:
- General knowledge of SQL Server Management Studio
- General knowledge of INSERT command
Inserting a stored procedure result set into table
SQL INSERT and sql insert select had been covered in earlier articles, and last article we had covered inserting a stored procedure result set.
SQL INSERT command also can insert a row the default values without providing it to a column name or value. If default is defined for a column, it is inserted. That type of insert is used to insert dummy type entries sometimes.
Inserting default is not much used in real life applications, but has use in special need.
Lets recall basic sql insert syntax again:
INSERT (column1, column2) VALUES (value1, values2)
To insert data to table with defaults, following sql insert syntax is used.
INSERT tableName DEFAULT VALUES
Lets see in a sql insert example which inserts row as default. To achieve it first , we will first create a table with two column. CustomerID is identity and customerName has default value in CREATE script. When sql insert tries default value, it will automatically place the provided default value to table as row. CREATE SCRIPT of table is as follows:
Now we can try to insert default value as row. SQL insert command, syntax and steps are as follows:
As above sql insert example indicates, new row has been inserted into the table with default values. In that sql insert, we have not provided any column name, instead the table name has been provided after INSERT INTO. Following table name, DEFAULT VALUES syntax is added. If we check the new row, ID is identity column came automatically due 1-1 seed. CustomerName came from default value which we provided while creating table.
a result of stor
Data Layers
Area: | programming \ languages \ tsql \ \ \ |
Ref: | |
Loc: | articles |
Tags: | tsql |
|