使用 go in sql 执行动态查询 [英] Execute Dynamic Query with go in sql

查看:38
本文介绍了使用 go in sql 执行动态查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DECLARE @script VARCHAR(MAX);
SET @script = 
    '
    create table ali(id decimal(10,0));
    drop table ali;
    go
    create table ali(id decimal(10,0));
    drop table ali;
    '

EXEC (@script);

执行上述查询时出现错误消息.如果您有解决此问题的想法,请告诉我.

Error message occured when execute above query. Please tell me if you have an idea for resolve this.

消息 102,级别 15,状态 1,第 4 行 'go' 附近的语法不正确.

Msg 102, Level 15, State 1, Line 4 Incorrect syntax near 'go'.

注意:上面创建和删除创建的表的代码只是举例,我还有其他一些带有 go 语句的动态查询.请不要给出这个答案.

DECLARE @script   VARCHAR(MAX),
        @script1  VARCHAR(MAX);
SET @script = 
    '
    create table ali(id decimal(10,0));
    drop table ali;
    ';
SET @script1 = 
    '
    create table ali(id decimal(10,0));
    drop table ali;
    ';
EXEC (@script);
EXEC (@script1);

推荐答案

GO 实际上是 无效的 T-SQL:

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.

您必须在动态 SQL 中删除 GO 的实例,或使用文章中提到的工具之一(例如命令行中的 osql).您的查询应该仍然适用于从动态 SQL 中删除的所有 GO 实例.

You will have to remove instances of GO in your dynamic SQL, or use one of the tools mentioned on the article (such as osql from the command-line). Your query should still work with all instances of GO removed from the dynamic SQL.

这篇关于使用 go in sql 执行动态查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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