如何有条件地在Sybase(TSQL)中创建表?

编程入门 行业动态 更新时间:2024-10-26 22:20:43
本文介绍了如何有条件地在Sybase(TSQL)中创建表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好的,所以Sybase(12.5.4)将允许我执行以下操作来删除表(如果该表已经存在):

OK, so Sybase (12.5.4) will let me do the following to DROP a table if it already exists:

IF EXISTS ( SELECT 1 FROM sysobjects WHERE name = 'a_table' AND type = 'U' ) DROP TABLE a_table GO

但是如果我尝试对表创建进行同样的操作,我总是会被警告table已经存在,因为它继续进行并试图创建我的表并忽略了条件语句。只需尝试运行以下语句两次,您就会明白我的意思了:

But if I try to do the same with table creation, I always get warned that the table already exists, because it went ahead and tried to create my table and ignored the conditional statement. Just try running the following statement twice, you'll see what I mean:

IF NOT EXISTS ( SELECT 1 FROM sysobjects WHERE name = 'a_table' AND type = 'U' ) CREATE TABLE a_table ( col1 int not null, col2 int null ) GO

运行以上命令会产生以下结果错误:

Running the above produces the following error:

(本地主机)上的SQL Server错误错误:2714,行:7消息:有在数据库中已经是一个名为 a_table的对象。

该怎么办? ?!

推荐答案

到目前为止,我唯一想到的解决方法是使用立即执行:

The only workaround I've come up with so far is to use execute immediate:

IF NOT EXISTS ( SELECT 1 FROM sysobjects WHERE name = 'a_table' AND type = 'U' ) EXECUTE("CREATE TABLE a_table ( col1 int not null, col2 int null )") GO

工作就像一种魅力,感觉就像肮脏的骇客。

works like a charm, feels like a dirty hack.

更多推荐

如何有条件地在Sybase(TSQL)中创建表?

本文发布于:2023-07-26 11:42:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1216008.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有条件   Sybase   TSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!