1

I'm trying to insert the data from a text file into my database. I'm using SQL Server Management Studio 17.

I'm using this query:

BULK INSERT db.[dbo].[mytable]
    FROM 'path/file.txt'
    WITH 
        (FIELDTERMINATOR = ';',
         ROWTERMINATOR = '\n') 

The problem is that it's working in SQL Server Management Studio but when i'm trying to execute this php script it shows an error even though I granted the user the permission to bulk after following this:

You do not have permission to use the bulk load statement

The error:

Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 4834 [code] => 4834 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]You do not have permission to use the bulk load statement. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]You do not have permission to use the bulk load statement. ) )

The code i'm using

<?php

$serverName = "SQLEXPRESS"; 
$connectionInfo = array( "Database"=>"master");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}    
$query1 = "BULK INSERT db.[dbo].[mytable]
    FROM 'path/file.txt'
    WITH 
        (FIELDTERMINATOR = ';',
         ROWTERMINATOR = '\n')";

$result1 = sqlsrv_query($conn,$query1) or  die( print_r(sqlsrv_errors(), true) );

?>
1
  • I would suggest that the user does not have the right permissions. Try ALTER SERVER ROLE bulkadmin ADD MEMBER {Your PHP SQL Login}; (obviously replace the text in the braces ({})). Commented Feb 11, 2019 at 16:55

1 Answer 1

1

Account that is used by your PHP application need this permission:

GRANT ADMINISTER BULK OPERATIONS TO [server_login]

Your SSMS process seems run using other credentials and they have such permission.

The other reason can be if SQL Server service account does not have enough permissions to read the source file

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it turns out that i'm using other credentials. The weird thing is even i'm using in PHP and SSMS windows authentication, the user in PHP script is different from SSMS. I figured that out in error.log. I think that's another issue. Thank you for your help.
@Morsi.Hamza, is your question answered? If so, consider to mark it as such one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.