Saturday 25 January 2020

Various ways to insert data into table in SQL server

Hi All,

Hope you all are doing good.

In this article, we will know about various ways of how to insert data into tables.

Insert one row at a time
Syntax:
INSERT INTO 
table_name (column_list) 
VALUES (value_list);
Example:
INSERT INTO 
Customers (customer_name, email, phone)
VALUES ('Customer-1', 'xyz@xyz.com', '9090909090');


Insert multiple rows at a time
Syntax:
INSERT INTO table_name (column_list)
VALUES
    (value_list_1),
    (value_list_2),
    ...
    (value_list_n);
Example:
INSERT INTO 
Customers (customer_name, email, phone)
VALUES 
  ('Customer-1', 'xyz@xyz.com', '9090909090'),
  ('Customer-2', 'abc@abc.com', '9191919191'),
  ('Customer-3', 'pqr@pqr.com', '9292929292');


Insert Into select
Syntax:
INSERT INTO table_name (col-1, col-2, col-3, ..., col-n) 
SELECT
    col-1, 
col-2, 
col-3, 
.
.
., 
col-n
FROM
    another_table_name
Example:
INSERT INTO Addresses (street, city, state, zip_code) 
SELECT
    street,
    city,
    state,
    zip_code
FROM
    Customers


Insert the top N of rows
Syntax:
INSERT  [ TOP ( expression ) [ PERCENT ] ] 
INTO target_table (column_list)
query
Example:
INSERT TOP (10) 
INTO Addresses (street, city, state, zip_code) 
SELECT
    street,
    city,
    state,
    zip_code
FROM
    Customers


Insert the top percent of rows
Syntax:
INSERT  [ TOP ( expression ) [ PERCENT ] ] 
INTO target_table (column_list)
query
Example:
INSERT TOP (10) PERCENT  
INTO Addresses (street, city, state, zip_code) 
SELECT
    street,
    city,
    state,
    zip_code
FROM
    Customers

Thanks for reading my article.

No comments:

Post a Comment

Intoduction to Flutter

Hi All, I hope every one is doing good In this article, we will know about the introduction of Flutter.