Development Tip

테이블이있는 경우 삭제하는 방법은 무엇입니까?

yourdevel 2020. 9. 30. 11:34
반응형

테이블이있는 경우 삭제하는 방법은 무엇입니까?


테이블 이름은 Scores입니다.

다음을 수행하는 것이 맞습니까?

IF EXISTS(SELECT *
          FROM   dbo.Scores)
  DROP TABLE dbo.Scores

다음을 수행하는 것이 맞습니까?

IF EXISTS(SELECT *
          FROM   dbo.Scores)
  DROP TABLE dbo.Scores

아니요. 행이 포함 된 경우에만 테이블을 삭제합니다 (테이블이 존재하지 않으면 오류가 발생 함).

대신 영구 테이블의 경우

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

또는 임시 테이블의 경우 다음을 사용할 수 있습니다.

IF OBJECT_ID('tempdb.dbo.#T', 'U') IS NOT NULL
  DROP TABLE #T; 

SQL Server 2016+는 DROP TABLE IF EXISTS …. @Jovan의 답변을 참조하십시오 .


SQL Server 2016에서 다음을 사용할 수 있습니다.

DROP TABLE IF EXISTS dbo.Scores

참조 : DROP IF EXISTS-SQL Server 2016의 새로운 기능

곧 SQL Azure 데이터베이스에 포함될 것입니다.


ANSI SQL / 크로스 플랫폼 방식은 SQL 데이터베이스 내의 개체에 대한 메타 데이터를 쿼리하도록 특별히 설계된 INFORMATION_SCHEMA 를 사용하는 것 입니다.

if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'Scores' AND TABLE_SCHEMA = 'dbo')
    drop table dbo.Scores;

대부분의 최신 RDBMS 서버는 최소한 MySQL , Postgres , Oracle , IBM DB2Microsoft SQL Server 7.0 이상을 포함한 기본 INFORMATION_SCHEMA 지원을 제공 합니다.


실제로 작동하지 않는 많은 것을 보았습니다. 임시 테이블이 생성되면 tempdb에서 삭제해야합니다!

작동하는 유일한 코드는 다음과 같습니다.

IF OBJECT_ID('tempdb..#tempdbname') IS NOT NULL     --Remove dbo here 
    DROP TABLE #tempdbname   -- Remoeve "tempdb.dbo"

또는:

if exists (select * from sys.objects where name = 'Scores' and type = 'u')
    drop table Scores

이게 도움이 되길 바란다:

begin try drop table #tempTable end try
begin catch end catch

SQL Server 2016 (13.x) 이상

DROP TABLE IF EXISTS dbo.Scores

이전 버전에서

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

U 는 당신입니다table type


인수가 존재하는 테이블의 이름이면 1을 반환하고 그렇지 않으면 0을 반환하는 작은 UDF를 작성했습니다.

CREATE FUNCTION [dbo].[Table_exists]
(
    @TableName VARCHAR(200)
)
    RETURNS BIT
AS
BEGIN
    If Exists(select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = @TableName)
        RETURN 1;

    RETURN 0;
END

GO

테이블이있는 User경우 삭제하려면 다음과 같이 호출하십시오.

IF [dbo].[Table_exists]('User') = 1 Drop table [User]

간단합니다.

IF OBJECT_ID(dbo.TableName, 'U') IS NOT NULL
DROP TABLE dbo.TableName

여기서 dbo.TableName원하는 테이블입니다 'U' 입니다 type당신의 table.


IF EXISTS (SELECT NAME FROM SYS.OBJECTS WHERE object_id = OBJECT_ID(N'Scores') AND TYPE in (N'U'))
    DROP TABLE Scores
GO

나는 사용한다:

if exists (select * 
           from sys.tables 
           where name = 'tableName' 
           and schema_id = schema_id('dbo'))
begin
    drop table dbo.tableName
end

A better visual and easy way, if you are using Visual Studio, just open from menu bar,

View -> SQL Server Object Explorer

it should open like shown here

enter image description here

Select and Right Click the Table you wish to delete, then delete. Such a screen should be displayed. Click Update Database to confirm.

enter image description here

This method is very safe as it gives you the feedback and will warn of any relations of the deleted table with other tables.


Do like this, it is the easiest way.

qry will be your own query, whatever you want in the select list.

set @qry = ' select * into TempData from (' + @qry + ')Tmp  '

exec (@qry)

select * from TempData 

drop table TempData

참고URL : https://stackoverflow.com/questions/7887011/how-to-drop-a-table-if-it-exists

반응형