关键字GO引发PHP错误

时间:2012-06-05 20:44:35

标签: php sql sql-server linux sql-server-2005

我遇到了一个问题,我试图在Linux服务器上恢复php中的MSSQL转储。我正在读取文件,然后将文件作为查询传递。

这有效:

CREATE TABLE [dbo].[recipes](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Title] [nvarchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Desc] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Directions] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [CompAuthor] [nvarchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [CompName] [nvarchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Servings] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 CONSTRAINT [PK_recipes] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)
)

但是只要我添加一个像GO

这样的词
WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
 ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)
    )

GO

它会抛出类似

的错误
Warning: mssql_query(): message: Incorrect syntax near 'GO'

我的问题是导致此错误的原因以及如何修复?

2 个答案:

答案 0 :(得分:6)

GO不是Transact-SQL关键字。它是Management Studio和SQLCMD等工具的批处理分隔符。

答案是不将GO命令注入您从应用程序发送的批次中。只需将它们作为单独的批次发送(如有必要)。

来源:GO (Transact-SQL)

答案 1 :(得分:0)

好吧,有时您会有通过Management Studio或查询分析器进行部署的脚本。

就我而言,我管理多台服务器并部署脚本可能很乏味,因此我必须创建一个自动部署到所有服务器的PHP工具。以下是我为克服“GO”问题所做的工作:

    // split the script into several statements
    $sqls = explode("GO",$sql);

    // execute each statement separately
    foreach($sqls as $sqlb)
    {       
        $rs = mssql_query($sqlb, $mssql);

        if (!$rs) {
            echo "MSSQL {$client} sql: {$sqlb} Error: " . mssql_get_last_message();
        } else {

            if($rs === true)
            {
                echo "OK";
            } else
            {
                while($row = mssql_fetch_assoc($rs)) {
                    print_r($row);
                }
            }
        }
    }