There are many method through which we can insert the data into any table. Lets go through them one by one .
Lets take a table sqlholic in which there are two column sqlholic_ID having Int as data type , Employee_Name. and varchar(32) Datatype.
Now lets enter the data into this table in different ways:
- Using Keyword “Values”.
Insert into sqlholic (sqlholic_ID, Employee_name)
Values(1,'Saurabh')
2. Using Keyword “Select”.
Using the select keyword we can also add the data into particular table, the syntax is given below
Insert into sqlholic (sqlholic_ID, Employee_name)
SELECT 1,'Saurabh'
3. Using Keyword “Select” and another table.
We can insert the data from one table to another table using the keyword “Select”. Keep one thing in mind the data type from both the table should not conflict otherwise insert command will not be executed.
/*We can also insert all data drom one table to anotherr table */
Insert into sqlholic (sqlholic_ID, Employee_name)
SELECT Column1_Name, column2_Name from Table_Name
/*We can also insert perticular data drom one table to anotherr table */
Insert into sqlholic (sqlholic_ID, Employee_name)
SELECT Column1_Name, column2_Name from Table_Name
WHERE Column_Id = 1
4. Insert all data from one table to another table :
Lets us suppose we have a table sqlholic and we want all data from sqlholic to a new table which is not even exist in system so we have a command for this but this only copy data and datatype but do not create the constraint in new table.
select * into Sqlholic_New from Sqlholic
/* Here a new table will be created with name swlholic_New and all data from Sqlholic will be transfered to that new table */
/*Any connstrainst from sqlholic will not be created in sqlholic_New */
5. Using Stored Procedure.
We can also insert the data into table using stored Procedure. Its widely used by Sql developers as they insert the data through stored proc . Here the insert statement is written in stored proc . See example below:
CREATE PROCEDURE [dbo].[uspAddSqlholicDetail]
(
@Sqlholic_ID INT,
@Employee_Name Varchar(32)
)
AS
BEGIN
SET @errorDetails='Insert data into SqlHolic Table'
INSERT INTO SqlHolic(SqlHolic_ID, Employee_Name)
SELECT @Sqlholic_ID,@Employee_Name
END
---Execute the Stored Proc
Exec uspAddSqlholicDetail
@Sqlholic_ID = 1,
Employee_Name = 'Saurabh'
6. Using Output of Stored Procedure.
We can also insert the data into table using Output of any stored Procedure .Lets see how to do this.
--Here is the stored proc whose output is two column Column_a and column_2
CREATE PROCEDURE [dbo].[uspGetTable_aDetail]
AS
BEGIN
SELECT Column_1 , Column_2 From Table_a
END
---Now we will use the output of this stored proc as input of table sqlholic
Insert into sqlholic(sqlholic_ID, Employee_Name)
Exec UspGetTable_aDetails
--Here Output of stored proc will be inserted in sqlholic table.
7. Using Import Functionality
We can also insert the data into table using import Functionality . Below are the step to save the data from excel file to batting table .
Similarly we can import the data from one server to another server using the same method.
8. Using Bulk Insert Command
We can also insert the data into table using Bulk Insert Command to save data from csv file to your table.
BULK INSERT Sqlholic
FROM 'C:\Temp\Test.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n');
Thanks For Visiting
Leave a Reply