创建表,但如果表已存在则删除它
我正在处理一个请求,我必须创建一个表来插入一些数据。所以,显然我首先要有一个删除表。在创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个由 SYSDBA 或其他具有足够
DROP TABLE
和CREATE TABLE
权限的管理员级别用户拥有的存储过程,该存储过程执行以下操作:CREATE TABLE.AS.WITH DATA AND STATS;
您可以通过接受有关是否应将数据和/或统计信息复制到新表的附加参数来使其更加动态。
如果您使用 BTEQ,您可以执行类似的操作(BTEQ 命令语法可能有点偏离,但足够接近以理解要点):
You can create a stored procedure owned by SYSDBA or other admin level user with adequate
DROP TABLE
andCREATE TABLE
privileges that does the following: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):
看来SAS proc sql 不能像T-SQL 一样直接做到这一点。不管怎样,你可以写一个宏来检查数据集是否存在。如果是这样,请先放下它。然后创建表。就像下面的代码一样。
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.