2

Is there any known length restriction for the sql variable on sqlsrv_query? I have an application that sends a block of commands do SQL SERVER (insert into..., insert into..., etc). It works fine on Linux (mssql) but on Windows (sqlsrv) the last instructions are ignored. The sql command where the last instructions are lost has 4900 lines.

$qry_str = "";
for () {
    $qry = "INSERT INTO tbl
                (field1,
                 field2,
                 etc)
            VALUES
                ('$val1',
                 '$val2',
                 etc)";
    //echo "<pre>$qry</pre>";
    $qry_str .= "$qry;\n";
}
fcaserio
  • 726
  • 1
  • 9
  • 18
  • just to be clear, you're asking about the length of the query itself, and not the result set coming back, correct? – Jeff Lambert Oct 14 '15 at 20:58
  • exactly, the length of the query itself – fcaserio Oct 14 '15 at 21:02
  • A single command with 4900 lines??? – Sean Lange Oct 14 '15 at 21:04
  • not sure if this is an exact duplicate or not, but [this](http://stackoverflow.com/a/1869810/697370) has your answer and links to docs – Jeff Lambert Oct 14 '15 at 21:04
  • It's not a single command, is a group of commands, as shown above. We prefer to send it in a single string to SQL server to make sure the batch is executed only if there is no error on any of the queries. If sent individually to SQL server one mispeled query will be ignored, but the rest will be executed. It works fine with mssql on linux but is not working with sqlsrv – fcaserio Oct 14 '15 at 21:57

1 Answers1

0

On sqlsrv_query a sql string larger then 65536 is truncated to 65536 characters. Try to use mssql_query or write your query to file and execute it from sqlcmd tool. This may help you Does Microsoft sqlsrv driver for PHP has limitation on query string length?

Community
  • 1
  • 1
gofr1
  • 15,741
  • 11
  • 42
  • 52
  • As far as I know mssql_query is not available on 5.3+ windows versions – fcaserio Mar 01 '16 at 13:01
  • Quite right. So you can do it with `sqlcmd`, the link shows example how to do it. – gofr1 Mar 01 '16 at 13:07
  • I'm looking for some workaround php only. For now I'm splitting large commands (GT 65536) on my class to exec blocks of code instead of processing the entire command. tks anyway for the suggestion. – fcaserio Mar 01 '16 at 14:19