创建表,但如果表已存在则删除它

发布于 2025-01-05 10:58:58 字数 374 浏览 1 评论 0原文

我正在处理一个请求,我必须创建一个表来插入一些数据。所以,显然我首先要有一个删除表。在创建 st 之前但是当我第一次运行它时(在创建表之前),它会弹出一个错误,指出表未创建,然后创建表并从这里开始。因此,每次任何人第一次运行我的代码时,都会在 drop table st 处弹出此错误。有人有更好的主意吗?

像“如果表存在则删除否则创建表”之类的事情
我不知道我们如何在 sql 中执行此操作

Drop table table_name;---------------->这里它第一次抛出一个错误,说表不存在。

创建表 table_name

{ 等等 };

顺便说一句,我正在 Teradata 上工作,但简单的 sql 逻辑会有所帮助。

I am working on a request where I have to create a table to insert some data. So, obviously I will have first have a delete table st. before the create st. but when I am running this for the first time(before the table can be created) it will pop up an error saying table not created and then creates table and goe son from here. So every time any one runs my code for the first time it will pop up this error at drop table st. Does any one have any better idea??

Some thing like " if table exists then drop else create table"
I am not sure how are we going to do this in sql

Drop table table_name;--------------> here it throws an error for the first time saying table does not exist.

Create table table_name

{ so on };

By the way I am working on Teradata but a simple sql logic would help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘴硬脾气大 2025-01-12 10:58:58

您可以创建一个由 SYSDBA 或其他具有足够 DROP TABLECREATE TABLE 权限的管理员级别用户拥有的存储过程,该存储过程执行以下操作:

  1. 检查 DBC.Tables 以查看对象是否存在。
  2. 如果对象存在,则删除它。
  3. 运行 DDL 以重新创建表:CREATE TABLE.AS.WITH DATA AND STATS;

您可以通过接受有关是否应将数据和/或统计信息复制到新表的附加参数来使其更加动态。

如果您使用 BTEQ,您可以执行类似的操作(BTEQ 命令语法可能有点偏离,但足够接近以理解要点):

SELECT 1 
FROM DBC.TABLES 
WHERE DatabaseName = '<TargetDB>'
  AND TableName = '<TargetTable>'
  AND TableKind = 'T' /* Make sure it is in fact a table, not a view, macro etc */


.IF ACIVITYCOUNT = 0 THEN GOTO CreateNewTable;

DROP TABLE <TargetDB>.<TargetTable>;

.IF ERRORCODE = 3807 THEN GOTO CreateNewTable; /* Table dropped by another process? */
.IF ERRORCODE > 0 THEN .QUIT ERRORCODE; /* Unexpected error */

.LABEL CreateNewTable;

CREATE <TargetDB>.<TargetTable> AS <SourceDB>.<SourceTable> WITH DATA AND STATISTICS;

You can create a stored procedure owned by SYSDBA or other admin level user with adequate DROP TABLE and CREATE TABLE privileges that does the following:

  1. Check DBC.Tables to see if object exists.
  2. If object exists, drop it.
  3. Run the DDL to recreate the table: CREATE TABLE <TargetDB>.<TargetTable> AS <SourceDB>.<SourceTable> WITH DATA AND STATS;

You can make it more dynamic by accepting additional parameters on whether the data and/or stats should be copied to the new table.

If you are using BTEQ, you can do something similar (BTEQ command syntax may be a little off but close enough to get the point across):

SELECT 1 
FROM DBC.TABLES 
WHERE DatabaseName = '<TargetDB>'
  AND TableName = '<TargetTable>'
  AND TableKind = 'T' /* Make sure it is in fact a table, not a view, macro etc */


.IF ACIVITYCOUNT = 0 THEN GOTO CreateNewTable;

DROP TABLE <TargetDB>.<TargetTable>;

.IF ERRORCODE = 3807 THEN GOTO CreateNewTable; /* Table dropped by another process? */
.IF ERRORCODE > 0 THEN .QUIT ERRORCODE; /* Unexpected error */

.LABEL CreateNewTable;

CREATE <TargetDB>.<TargetTable> AS <SourceDB>.<SourceTable> WITH DATA AND STATISTICS;
赢得她心 2025-01-12 10:58:58

看来SAS proc sql 不能像T-SQL 一样直接做到这一点。不管怎样,你可以写一个宏来检查数据集是否存在。如果是这样,请先放下它。然后创建表。就像下面的代码一样。

%macro checkDrop(tmpData);
%if %SYSFUNC(exist(&tmpData)) %then %do;
    proc sql;
    drop table &tmpData;
    quit;
    %end;
    %else %do;
    proc sql;
    create table &tmpData (a numberic, b numberic);
    %end;
%mend;
%checkDrop(tmp)

It seems SAS proc sql cannot do it like T-SQL directly. Anyway, you can write a macro to check if the data set exist. If so, drop it first. Then create the table. Like the following code.

%macro checkDrop(tmpData);
%if %SYSFUNC(exist(&tmpData)) %then %do;
    proc sql;
    drop table &tmpData;
    quit;
    %end;
    %else %do;
    proc sql;
    create table &tmpData (a numberic, b numberic);
    %end;
%mend;
%checkDrop(tmp)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文