dongwuge6201 2018-06-20 14:56
浏览 140
已采纳

PHP + SQL - 使用SQL Server的“创建到”脚本通过PHP创建表

In SQL Server you seem to be able to get the CREATE TABLE script by right clicking a table, 'Script Table As...' and selecting 'Create To'. I've copied the output script and while it does seem to work and create the table when pasted onto the 'New Query' window, it does not work when used in PHP. Example code:

$sqlcommand = 
"CREATE TABLE [dbo].[departments](
    [department_pk_fk] [varchar](25) NOT NULL,
    [des] [varchar](100) NULL,
    CONSTRAINT [PK_departamentos] PRIMARY KEY CLUSTERED
(
    [department_pk_fk] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
    ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
";
$site_api->execQuery($sqlcommand);

Anyone have any useful input about why this would happen?

Edit: execQuery code:

public function execQuery($q,$scalar=FALSE,$returns=FALSE) {  
//return an assoc array or a scalar
    global $Cfg, $site_api;

    $this->query=$q;


    try {
      $result_array = array();
        $result = $this->conn->query($this->query);
        if (is_bool($result)===TRUE) {  //if reslt is true or false then there is nothing to do
            return $result;
        } else {
            if (!$scalar) {
                if ($result->rowCount()==0)
                    return null;
                //SQLSRV
                while($row = $result->fetchAll(SQLSRV_FETCH_ASSOC)) {
                    $result_array[]=$row;
                }
                $result->closeCursor();
                if($returns) return $result_array[0];
            } else {
                $row = $result->fetchAll(SQLSRV_FETCH_NUMERIC);
                if($returns) return $row[0];
            }
        }
    }
    catch (Exception $e) {
        print "Error Code <br>".$e->getCode();
        print "Error Message <br>".$e->getMessage();
        print "Strack Trace <br>".nl2br($e->getTraceAsString());
    }
}`
  • 写回答

1条回答 默认 最新

  • duanke2012 2018-06-21 06:09
    关注

    Your CREATE statement is correct, just remove the keyword GO from your script. The GO keyword is not part of T-SQL, it is SQL Server utilitiy statement.

    Because I don't know how your class is written, I've made a simple script for test using PHP Driver for SQL Server (SQLSRV_FETCH_ASSOC constant is specific for this driver). It works correct without GO statement and gives error with this statement (PHP 7.1 and SQL Server Express 2012).

    <?php
    $server = 'server\instance,port';
    $cinfo = array(
        "Database"=>'database'
    );
    
    $conn = sqlsrv_connect($server, $cinfo);
    if( $conn === false )
    {
        echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    
    $sql = 
        "CREATE TABLE [dbo].[departments](
            [department_pk_fk] [varchar](25) NOT NULL,
            [des] [varchar](100) NULL,
            CONSTRAINT [PK_departamentos] PRIMARY KEY CLUSTERED
            ([department_pk_fk] ASC) WITH (
                PAD_INDEX = OFF, 
                STATISTICS_NORECOMPUTE = OFF, 
                IGNORE_DUP_KEY = OFF, 
                ALLOW_ROW_LOCKS = ON, 
                ALLOW_PAGE_LOCKS = ON
            ) ON [PRIMARY]
        ) ON [PRIMARY]";
    $stmt = sqlsrv_query($conn, $sql);
    if( $stmt === false ) {
        echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
        exit;
    }
    
    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);
    
    echo 'OK'."</br>";
    ?>
    

    If you don't know how the SQL statement is verified, try to remove schema name (dbo), brackets for object names ([]), because there can be some additional syntax corrections in class method.

    Other things, that can cause problems are permissions.

    You can always use sqlsrv_errors() to get additional information about specific errors.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)