0

我有创建表的 .sql (sybase) 文件,即“Create_table_mytable.sql”

但在创建表之前,它会检查现有表并删除相同的表。

这是脚本的样子:-

setuser 'dbo'
go 


    IF EXISTS (SELECT 1 FROM sysobjects o, sysusers u WHERE o.uid=u.uid AND o.name = 'mytable' AND u.name = 'dbo' AND o.type = 'U') 
    begin
        print 'dropping mytable'
         drop table   mytable   
        print 'dropped mytable'  
    end
go    
    IF NOT EXISTS (SELECT 1 FROM sysobjects o, sysusers u WHERE o.uid=u.uid AND o.name = 'mytable' AND u.name = 'dbo' AND o.type = 'U')
    begin       
        create table mytable (
                id numeric(9,0)     IDENTITY ,
                description             varchar(50)     not null
                )

                GRANT SELECT ON dbo.mytable TO my_user

                GRANT INSERT ON dbo.mytable TO my_user

                GRANT DELETE ON dbo.mytable TO my_user
    end
    go


end
go

setuser  
go 

这是cmd文件的内容:-

@ECHO ON

ECHO ">> START <<  mytable.sql" >> %LOG%
%SQL% -S %SERVER% -U %USER% -P %PWD% -D %DB% -i mytable.sql >> %LOG%

@ECHO OFF

这是日志文件的内容:-

">> START <<  mytable.sql" 
Msg 102, Level 15, State 1:
Server 'myserver', Line 14:
Incorrect syntax near 'go'.
">>> SYBASE SCRIPT COMPLETE <<< " 

注意:如果使用 Sybase Intrective SQL 或 ASE 工具执行相同的脚本,则可以正常运行。

4

1 回答 1

1

很好,我得到了答案。这是我在删除表后错过的内容:-

use db
go 

setuser 'dbo'
go


IF EXISTS (SELECT 1 FROM sysobjects o, sysusers u WHERE o.uid=u.uid AND o.name = 'mytable' AND u.name = 'dbo' AND o.type = 'U')
begin
print 'dropping mytable'
    drop table mytable
    print 'dropped table - mytable'
end
go 

IF (@@error != 0)
BEGIN
    PRINT "Error CREATING table 'mytable'"
    SELECT syb_quit()
END
go


IF NOT EXISTS (SELECT 1 FROM sysobjects o, sysusers u WHERE o.uid=u.uid AND o.name = 'mytable' AND u.name = 'dbo' AND o.type = 'U')
begin       
    create table mytable (
            id numeric(9,0)     IDENTITY ,
            description             varchar(50)     not null
            )


            GRANT SELECT ON dbo.mytable TO my_user

            GRANT INSERT ON dbo.mytable TO my_user

            GRANT DELETE ON dbo.mytable TO my_user

end
go

setuser  
go 
于 2012-04-11T06:38:53.760 回答