There are several statements in T-SQL that insert data into a table: Insert values, insert Select, insert Exec, select INTO, and BULK insert;
1. Insert Values
This is a more commonly used method of inserting data, you can do single or multiple data insertion
First create a table and then insert the data into the operation
CREATE TABLE Test (
ID int NOT NULL identity (primary key),
Name nvarchar (100)
)
Single Data insertion
INSERT into test (name) VALUES (' Test1 ')
Multi-row data insertion
INSERT into test (name) VALUES (' Test2 '), (' Test3 '), (' Test4 ')
Both of these methods can save the field behind the token name (this is not recommended), as follows:
INSERT into test values (' test1 ')
INSERT into test values (' Test2 '), (' Test3 '), (' Test4 ')
Multi-row data insertions are processed as an atomic operation, that is, data entered into a table if any row fails, the statement is not inserted into the table.
For the enhanced VALUES clause, you can also construct a derived table as a table-valued constructor in a standard way, as follows:
SELECT * FROM (values
(' 001 ', ' test1 '),
(' 002 ', ' test2 '),
(' 003 ', ' test3 ')
) as Test (id,name)
2. Insert Select
The Insert SELECT statement is similar to the Insert Values statement, unlike the values clause, where you can specify a select query, such as copying just the data into another table test1, first creating a test1 table with the same structure as test
CREATE TABLE Test1 (
ID int NOT NULL PRIMARY key,
Name nvarchar (100)
)
Execute INSERT statement
INSERT INTO Test1
Select Id,name from Test
Insert Test1 table results as follows to completely copy data from test table to Test1
Delete data in Test1
TRUNCATE TABLE Test1
The data insertion method can also be used to insert multiple rows of data using UNION ALL
INSERT INTO Test1
Select ' 1 ', ' test1 ' UNION ALL
Select ' 2 ', ' test2 ' UNION ALL
Select ', ' test3 '
Query Test1 table results as follows:
3. Insert Exec
Use the INSERT EXEC statement to insert a stored procedure or a result set of a dynamic SQL batch into the target table.
To create a test stored procedure
Create proc proc_test @name as nvarchar (100)
As
SELECT * FROM test where name like @name + '% '
Go
Test Execution Results
exec proc_test ' Test '
Delete test1 Data
TRUNCATE TABLE Test1
Execute INSERT statement
Insert into Test1 (id,name)
EXEC proc_test ' test '
Querying data in a test1 table
4. SELECT INTO
The SELECT INTO statement is a non-standard T-SQL statement that uses the results of the query to create and populate the target table.
Use the following statement to copy the data in test to a temporary table in temp, where the structure and data of the temp table are based on the test table, which copies (column name, type, empty, identity) properties and data to the target table, but indexes, constraints, triggers, and permissions are not copied. If these need to be recreated in the target table.
Select Id,name
Into #temp
From Test
Query the #temp table with the following data:
5. BULK INSERT
Use the BULK INSERT statement to insert data from a file into an existing table. Specify tables, source files, and options in the statement. You can specify multiple options, including data type, field terminator, line terminator, and other file options
To modify the test table structure:
ALTER TABLE test add address nvarchar, age int
Empty Test table
TRUNCATE TABLE test
Edit the local file, save the following blue font data in the TXT file, and specify the file address in the SQL statement.
1,tom, Haidian District, Beijing, 20
2,sam, Chaoyang District, Beijing, 23
3,amy, Pudong District, Shanghai, 27
4, Zhang San, Tangshan, Hebei province, 28
5, John Doe, Haidian District, Beijing, 31
EXECUTE statement
Bulk Insert Dbo.test from ' C:\sqltest.txt '
With (
Datafiletype= ' char ',
Fieldterminator= ', ',
Rowterminator = ' \ n '
)
The query results are as follows
SQL data Insertion