从VB脚本执行时,有效的SQL查询失败 [英] Valid SQL query fails when executed from VB Script

查看:121
本文介绍了从VB脚本执行时,有效的SQL查询失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在SQL Management Studio中成功运行的SQL查询,但是当我从VB脚本运行相同的查询时,它失败并出现错误:

I have SQL query that successfully runs in the SQL management studio, but when I'm running the same query from VB script it fails with error:

用于SQL Server的Microsoft OLE DB提供程序:关键字'ALTER'附近的语法不正确.

Microsoft OLE DB Provider for SQL Server: Incorrect syntax near the keyword 'ALTER'.

我的SQL查询是:

SET XACT_ABORT ON;
BEGIN TRANSACTION VersionBuild
BEGIN TRANSACTION
GO
CREATE TABLE dbo.TimeTemplate
    (
    id int NOT NULL IDENTITY (1, 1),
    startTime datetime NOT NULL,
    endTime datetime NOT NULL
    )  ON [PRIMARY]
GO
ALTER TABLE dbo.TimeTemplate ADD CONSTRAINT
    PK_TimeTemplate PRIMARY KEY CLUSTERED 
    (
    id
    ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
COMMIT

INSERT INTO [dbo].[TimeTemplate] ([startTime],[endTime])
VALUES ('2013-03-15 00:00:00.000','2013-03-15 23:59:00.000')

INSERT INTO [dbo].[Form] ([name] , [description], [dateCreated], [fileName])
VALUES ('TimeTemplates.aspx' , '' , getdate(), 'TimeTemplates.aspx')

;

If @@Error <> 0 BEGIN
ROLLBACK TRANSACTION VersionBuild
END ELSE 
BEGIN
UPDATE Version SET version = 74;
COMMIT TRANSACTION VersionBuild
END

推荐答案

来自 MSDN :

GO不是Transact-SQL语句;这是由 sqlcmd和osql实用程序以及SQL Server Management Studio代码 编辑器.

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

也来自同一篇文章:

基于ODBC或OLE DB API的应用程序收到语法错误 如果他们尝试执行GO命令. SQL Server实用程序从不 将GO命令发送到服务器.

Applications based on the ODBC or OLE DB APIs receive a syntax error if they try to execute a GO command. The SQL Server utilities never send a GO command to the server.

尝试删除您的GO语句,并将脚本分解为多个命令.

Try removing your GO statements and break the script up into multiple commands.

这篇关于从VB脚本执行时,有效的SQL查询失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆