如何使用cmd执行.sql脚本(sybase)文件

时间:2012-04-09 11:40:50

标签: cmd sqlcmd sybase-ase

我有.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 Tools执行,相同的脚本可以正常运行。

1 个答案:

答案 0 :(得分: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