Sunday, October 20, 2013

How to Insert Multiple Records into a Table with Single INSERT Statement in SQL Server

SQL Server query to Insert Multiple Records a with Single INSERT Statement
CREATE TABLE 
EMPLOYEE(EID INT NOT NULL , ENAME VARCHAR(10),
GENDER VARCHAR(6) NOT NULL,
SAL DECIMAL(10,2),PRIMARY KEY(EID));

METHOD I :
SQL Query to Insert Multiple Records at a time into a Table called "EMPLOYEE".

INSERT 
 INTO EMPLOYEE(EID, ENAME,GENDER,SAL)
VALUES (1,'JAN','M',5000.50),
(2,'FEB','F',8000.80),
(3,'MAR','M',6000.60) ;

SELECT *FROM EMPLOYEE;
--TRUNCATE TABLE EMPLOYEE;

METHOD II :
SQL Query to Insert Multiple Records at a time  a "EMPLOYEE".

INSERT 
 INTO EMPLOYEE(EID, ENAME, GENDER, SAL)
SELECT 1,'JAN','M',5000.50 UNION ALL
SELECT 2,'FEB','F',8000.80 UNION ALL
SELECT 3,'MAR','M',6000.60 ;

SELECT *FROM EMPLOYEE;
TRUNCATE TABLE EMPLOYEE;

METHOD III :
SQL Query to Insert Multiple Records with out specifying Field Names a "EMPLOYEE".

INSERT
INTO EMPLOYEE VALUES(1,'JAN','M',5000.50);
INSERT 
INTO EMPLOYEE VALUES(2,'FEB','F',8000.80);
INSERT
 INTO EMPLOYEE VALUES(3,'MAR','M',6000.60);

SELECT *FROM EMPLOYEE;
--TRUNCATE TABLE  EMPLOYEE;

METHOD IV :
SQL Query to Insert Multiple Records a  "EMPLOYEE".

INSERT 
 INTO EMPLOYEE(EID,ENAME,GENDER,SAL)VALUES(1,'JAN','M',5000.50);
INSERT 
 INTO EMPLOYEE(EID,ENAME,GENDER,SAL)VALUES(2,'FEB','F',8000.80);
INSERT 
 INTO EMPLOYEE(EID,ENAME,GENDER,SAL)VALUES(3,'MAR','M',6000.60);

SELECT *FROM EMPLOYEE;
--TRUNCATE  TABLE EMPLOYEE;
--DROP TABLE EMPLOYEE;

Note :
The TRUNCATE EMPLOYEE Query only deletes all the Records from the EMPLOYEE with out deleting the entire Table Structure from the Database.

Where as , The DROP TABLE EMPLOYEE Query deletes the entire Table with Structure from the Database.

Thanks,TAMATAM

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog