如果有可能在sql中使用TRUNCATE删除单个实体 [英] if there any possible to delete single entity using TRUNCATE in sql

查看:71
本文介绍了如果有可能在sql中使用TRUNCATE删除单个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有任何可能在sql中使用TRUNCATE删除单个实体

解决方案

如果用single entity表示单行则答案为No - 您不能在sql中使用Truncate来执行此操作 - 请参阅文档 [

^
] class =op> Quote:

从表中删除 所有行而不记录单个行删除。 TRUNCATE TABLE类似于没有WHERE子句的DELETE语句;但是,TRUNCATE TABLE更快,并且使用更少的系统和事务日志资源。



如果您可以识别要删除的行,则在WHERE子句中包含这些详细信息,例如:

  DELETE   FROM  tablename  WHERE  ID = 7 

  DELETE   FROM  tablename 其中 column1 = '  A'  column2 = '  2' 

或者你知道要删除哪一行。



如果单一实体是指一张表,那么答案仍然是否 - 该表仍然存在,但将为空(请参阅上面的文档链接)。

在该实例中,您将需要使用 DROP TABLE [ ^ ]。例如。

  IF   OBJECT_ID (N  '  dbo.tablename',N '  U' IS   NOT   NULL  
DROP TABLE dbo.tablename;

  IF   EXISTS  SELECT  *  FROM  sys.objects  WHERE  object_id =  OBJECT_ID (N '  [dbo]。[tablename]' AND   TYPE   IN (N     U'))
DROP TABLE [dbo]。[tablename]


谢谢。 ................

if there any possible to delete single entity using TRUNCATE in sql

解决方案

If by "single entity" you mean a single row then the answer is "No" - you cannot use Truncate in sql to do that - see the Documentation[^] for Truncate

Quote:

Removes all rows from a table without logging the individual row deletions. TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; however, TRUNCATE TABLE is faster and uses fewer system and transaction log resources.


If you have the means of identifying which row you want to remove then include those details in a WHERE clause e.g.

DELETE FROM tablename WHERE ID=7

or

DELETE FROM tablename where column1 = 'A' and column2 = '2'

or however you know which row to delete.

If by "single entity" you mean a table then the answer is still "No" - the table will still exist but will be empty (refer to documentation link above).
In that instance you will need to use DROP TABLE[^]. E.g.

IF OBJECT_ID(N'dbo.tablename', N'U') IS NOT NULL
    DROP TABLE dbo.tablename;

or

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tablename]') AND TYPE IN (N'U'))
DROP TABLE [dbo].[tablename]


thank you. ................


这篇关于如果有可能在sql中使用TRUNCATE删除单个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆