The TRUNCATE TABLE command in SQL Server
The TRUNCATE TABLE statement is used to delete all records from a table in SQL Server. This command works similarly to the DELETE command but without the WHERE clause.
Syntax of the TRUNCATE TABLE command in SQL Server
TRUNCATE TABLE [ten_CSDL.] [ten_schema.] ten_bang
[ WITH (PARTITIONS (so_phanvung
| so _phanvung TO so _p
| so _phanvung TO so _p
hanvung)];
Variable name or variable value
ten_CSDL
It depends on you. If specified, this is the name of the database.
ten_scheme
Option. This is the name of the schema (translating as schema or namespace) that the table belongs to.
ten_bang
The table you want to delete the record.
WITH (PARTITIONS (so_phanvung | so _phanvung TO so _phanvung)
Optional and can only be used with a partition table. If specified, so_phanvung is the number of the region you want to delete in this table. To delete multiple zones, use comma to separate values or range of numerical partition values. If you use this clause with a non-partitioned table, SQL Server will report an error. This feature is not available in all versions of SQL Server.
Note
- If you delete all records in the table, the counter at every indentity column will be repeated from the beginning.
- Cannot delete all records in the table referenced by Foreign Key.
- Before deleting the entire record in the table, it must have priority as ALTER TABLE.
For example
In SQL Server, deleting the entire record is the fastest way if you don't have to retrieve data. If the table is TRUNCATE, deleting rows will not be recorded so it cannot be returned to the previous operation. TRUNCATE is also easier than deleting a table with the DROP command or creating a new one with CREATE. See the example below.
TRUNCATE TABLE;
This example will delete all records in the table.
The above command is equivalent to the DELETE command below.
DELETE FROM nhanvien;
Both of these commands delete all data in the table. The difference is that with the DELETE command you can return to the operation before deleting if you want to remain with TRUNCATE.
Let's see another example with the name of the database.
TRUNCATE TABLE totn.danhba;
This example deletes all list entries in the database named totn.
For example with partitions
If you want to delete a 1 or 1 region record, use the TRUNCATE TABLE command with the WITH PARTITIONS clause.
TRUNCATE TABLE nhanvien
WITH (PARTITIONS (1 TO 5, 7));
In this example, the table is a partition table and the TRUNCATE TABLE statement will delete regional records from 1 to 5 and zone 7 on this table.
Frequently asked questions
Q: Can I back up the database to the old state (rollback) after executing the TRUNCATE TABLE command in SQL Server?
Answer: The TRUNCATE TABLE command can be rolled back using the transaction. Example as below.
CREATE TABLE bang_test (cot1 int);
INSERT INTO bang_test VALUES (1);
INSERT INTO bang_test VALUES (2);
INSERT INTO bang_test VALUES (3);
-- Tạo chuyển tiếp
BEGIN TRAN;
-- Xóa bản ghi trong bảng
TRUNCATE TABLE dbo.bảng_test;
-- Rollback bảng đã xóa bản ghi
ROLLBACK;
SELECT * FROM bang_
test;
The SELECT statement above will return the results of the records below.
cot1
----------
1
2
3
Thus, the database state has been restored to the old state and the 3 records remain on the state_test.
Previous article: DELETE command in SQL Server
The following article: Conditions EXISTS in SQL Server
You should read it
May be interested
- Clean command in Windowsthe clean command deletes all partitions or formats the volume from the current drive. the command applies to windows server (semi-annual channel), windows server 2019, windows server 2016, windows server 2012 r2, windows server 2012.
- DELETE TOP command in SQL Serverthe delete top command in sql server is used to delete records from a table in sql and limits the number of records based on an existing value or percentage.
- CASE function in SQL Server (part 1)in sql server, the case function verifies the value based on the list of given conditions, then returns one or more results. in this article we will illustrate a number of different uses of this function in different situations.
- The sfc command in Windows(applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012)
- The cacls command in Windowsthe cacls command displays or modifies an arbitrary access control list (dacl) on the specified file. the command applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012.
- CREATE TABLE command in SQL to create a database tablewhat does the create table command in sql do? in this article, let's learn everything you need to know about the create table statement in sql!
- CASE statement in SQL Serverthis article will show you in detail how to use the case statement handling function in sql server with specific syntax and examples to better visualize and capture functions.
- SELECT TOP command in SQL Serverin sql server, the select top command is used to retrieve records from one or more tables in sql server and limit the number of return records based on a fixed value or percentage.
- SELECT INTO command in SQL Serverthe article guides using select into command in sql server with syntax and examples.
- Segment tables in SQL Servertable partitioning technique (table partitioning) to effectively manage the database with large capacity.