Insert records in SQL table with different way

In this article we will learn how to insert records in Sql table using different way.





Example:

1.  INSERT INTO tableName (table fields) VALUES (values1)
2.  INSERT INTO tableName (table fields) VALUES (values1), 

(values2)
3.  INSERT INTO tableName (table fields) SELECT  (values1)

UNION ALL  SELECT VALUES1

Let's start it with practically.

Create a database which name is STUDENTDEMO.

Query:

CREATE DATABASE STUDENTDEMO

USE STUDENTDEMO

Query for create a table :

---CREATE A TABLE
CREATE TABLE tbl_Student
(
Id INT Primary Key Identity(1,1),
Name NVARCHAR(100),
FatherName NVARCHAR(100),
Age INT,
Class INT
)

Write query for insert the records in SQL table using different way.

 First way: For insert the records in SQL table. 

INSERT INTO tbl_Student (Name,FatherName,Age,Class) VALUES ('Rakesh','Mohan',10,5)

INSERT INTO  tbl_Student (Name,FatherName,Age,Class) VALUES ('Shaym','Sohan',12,6)

It means for insert multiple records in a table we will write whole statement again and again.

Second way: 

INSERT INTO tbl_Student (Name,FatherName,Age,Class)  VALUES                          ('John','Smith',15,7), ('Dinesh','Mongia',18,9), ('John','Carry',9,4)
In second way don't need to write insert into table name and its fields use always values with comma separated.

Third way:

INSERT INTO tbl_Student (Name,FatherName,Age,Class)  
SELECT   'Amit','Rakesh',10,5
 UNION ALL
SELECT 'Abhishek','Rakesh',12,6
UNION ALL
SELECT 'Birendra','Rakesh',14,7

In third way don’t write whole statement like insert into table use always union all and select clause then your record inserted successfully.

For this article you can also watch it's video from youtube and subscribe my channel
 ( dotnettechpoint.com ) for get latest videos.

https://www.youtube.com/watch?v=glllgIAIItk

Or 

You can also also watch from here.

In this video we will learn how to insert records in SQL table with different way



I hope this article helps you, please provide me with the feedback for my article with videos.


No comments

Powered by Blogger.