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

发布于 2024-07-08 22:06:19 字数 642 浏览 11 评论 0原文

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

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

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

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

运行上面的语句会产生以下错误:

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

这是怎么回事?!

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

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 Error on (localhost)
Error:2714 at Line:7 Message:There is
already an object named 'a_table' in
the database.

What's the deal with that?!

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

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

发布评论

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

评论(9

享受孤独 2024-07-15 22:06:19

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

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

工作起来就像一个魅力,感觉就像一个肮脏的黑客。

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.

浮云落日 2024-07-15 22:06:19

除了在 execute("create table ...") 中调用 create table 之外,没有其他方法,

SYBASE 手册说:

当创建表命令出现在 if...else 块或
while 循环中,Adaptive Server 在之前创建表的模式
判断条件是否为真。 如果出现以下情况,这可能会导致错误
该表已经存在。 为了避免这种情况,请确保
数据库中不存在同名视图或使用
执行语句如下:

if not exists
    (select * from sysobjects where name="my table")
begin
execute "create table mytable(x int)"
end

There is no other way than calling create table in execute("create table ...")

SYBASE Manual says:

When a create table command occurs within an if...else block or a
while loop, Adaptive Server creates the schema for the table before
determining whether the condition is true. This may lead to errors if
the table already exists. To avoid this situation, either make sure a
view with the same name does not already exist in the database or use
an execute statement, as follows:

if not exists
    (select * from sysobjects where name="my table")
begin
execute "create table mytable(x int)"
end
浪菊怪哟 2024-07-15 22:06:19

我还没有对此进行测试,但您可以尝试将创建表语句移至存储过程中。 然后,您可以根据现有的 if 语句有条件地调用该存储过程。

I haven't tested this, but you could try moving the create table statement into a sproc. You could then conditionally call that sproc based on your existing if statement.

坏尐絯 2024-07-15 22:06:19

在 char @variable 中分配“CREATE TABLE”语句,然后执行 EXEC(@variable)。

Assign the "CREATE TABLE" statement in a char @variable and then do an EXEC(@variable).

箹锭⒈辈孓 2024-07-15 22:06:19

如果您想始终创建表,但有条件地删除它,您可以使用:

IF(SELECT count(*) FROM sysobjects WHERE name="tableNameWithoutUserPart") > 0
    DROP TABLE tableNameWithUserPart
GO

CREATE TABLE tableNameWithUserPart ...

If you want to always create the table, but conditionally drop it, you can use:

IF(SELECT count(*) FROM sysobjects WHERE name="tableNameWithoutUserPart") > 0
    DROP TABLE tableNameWithUserPart
GO

CREATE TABLE tableNameWithUserPart ...
心欲静而疯不止 2024-07-15 22:06:19

不需要任何解决方法;)

根据文档:

CREATE [ GLOBAL TEMPORARY ] TABLE [ IF NOT EXISTS ] [ owner.]table-name
( { column-definition | table-constraint | pctfree }, ... )
[ { IN | ON } dbspace-name ]
[ ENCRYPTED ]
[ ON COMMIT { DELETE | PRESERVE } ROWS
   | NOT TRANSACTIONAL ]
[ AT location-string ]
[ SHARE BY ALL ]

只需使用 IF NOT EXISTS。

There are no workarounds needed ;)

According to the documentation:

CREATE [ GLOBAL TEMPORARY ] TABLE [ IF NOT EXISTS ] [ owner.]table-name
( { column-definition | table-constraint | pctfree }, ... )
[ { IN | ON } dbspace-name ]
[ ENCRYPTED ]
[ ON COMMIT { DELETE | PRESERVE } ROWS
   | NOT TRANSACTIONAL ]
[ AT location-string ]
[ SHARE BY ALL ]

Just use the IF NOT EXISTS.

少年亿悲伤 2024-07-15 22:06:19
IF object_id('a_table') IS NULL
BEGIN
    CREATE TABLE a_table (
        col1 int not null,
        col2 int null
    ) 
END
IF object_id('a_table') IS NULL
BEGIN
    CREATE TABLE a_table (
        col1 int not null,
        col2 int null
    ) 
END
段念尘 2024-07-15 22:06:19

当使用 Sybase Anywhere 10.01 进行测试时,此方法有效:

if not exists(select * from SysColumns where tname = 'AAA') then create table DBA.AAA(  UNIQUEID integer not null ) END IF ;

This works when tested with Sybase anywhere 10.01 :

if not exists(select * from SysColumns where tname = 'AAA') then create table DBA.AAA(  UNIQUEID integer not null ) END IF ;
梦晓ヶ微光ヅ倾城 2024-07-15 22:06:19

尝试使用开始和结束。

如果不存在(
选择计数(1)
来自系统对象
WHERE 名称 = 'a_table'
AND 类型 = 'U'

开始
创建表 a_table (
col1 int 不为空,
col2 int 空

END

Try using Begin and End.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文