Showing posts with label command. Show all posts
Showing posts with label command. Show all posts

Sunday, March 25, 2012

Bulk Insert.

Is there any way I can use the following command like that ?
BULK INSERT Northwind.dbo.[Order Details]
FROM (select CSVTextFile from tblCSV)
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
Thanks in advance
The syntax of BULK INSERT is
quote:

BULK INSERT [ [ 'database_name'.] [ 'owner' ].] { 'table_name' FROM
'data_file' }...
'data_file'
Is the full path of the data file that contains data to copy into the
specified table or view. BULK INSERT can copy data from a disk (including
network, floppy disk, hard disk, and so on).
data_file must specify a valid path from the server on which SQL Server is
running. If data_file is a remote file, specify the Universal Naming
Convention (UNC) name.


Please refer to Books Online for examples of how to use BULK INSERT
http://msdn.microsoft.com/library/de...ba-bz_4fec.asp
Cristian Lefter, SQL Server MVP
MCT, MCSA, MCDBA, MCAD, MCSD .NET
"Rogers" <Rogers@.mailstuff.com> wrote in message
news:Oyx71bHuFHA.2624@.TK2MSFTNGP12.phx.gbl...
> Is there any way I can use the following command like that ?
> BULK INSERT Northwind.dbo.[Order Details]
> FROM (select CSVTextFile from tblCSV)
> WITH
> (
> FIELDTERMINATOR = ',',
> ROWTERMINATOR = '\n'
> )
> Thanks in advance
>
sql

BULK INSERT Vs BCP.exe

We have the following code written in the TSQL using the Bulk Insert command which works fine in SQL Server 2000 and 2005

Code Snippet

BULK INSERT MYTABLE FROM 'C:\TextDataFiles\MYTABLE.txt' WITH ( FORMATFILE = 'C:\MYTABLE.fmt' , MAXERRORS=0, BATCHSIZE=500000,CHECK_CONSTRAINTS, ERRORFILE='C:\MYTABLE.bad')

Please note the MAXERRORS is set to 0. So, if a error occurs nothing is written to the table.

Now we are trying to convert this functionality using the bcp.exe (actually using the API like bcp_control in C++) with the following code

Code Snippet

bcp "MYTABLE" in "'C:\TextDataFiles\MYTABLE.txt' -m0 -f"'C:\MYTABLE.fmt' -e 'C:\MYTABLE.bad' -b500000 -SMYSERVER -Uuid -Ppwd -h"CHECK_CONSTRAINTS"

This code does not work the same way as the BULK INSERT code above. It default the max errors to 10 (as documented) and inserts row(s) before the first error. How can I have the same functionality, fail on first error, with the bcp?

Your help is appreciated.

Since it will still insert the rows that are processed before the first error occurs (then failing and halting the insertions) I would probably push the data to a temporary table, do a check to make sure all data was inserted properly, then move the data from the temp table to the production table. This way you won't get half of the data if the bcp fails.

|||That is the way it works because of the batch=500000. You will always get the "batch" inserted before the error occurs.

|||

Why is the behaviour different in the TSQL for BULK INSERT, we have the same value for the BATCH. Is it possible BATCH INSERT is started in a commitment boundary automatically?. I do not understand why there is a different in the behaviour between the 2 commands.I would like to keep the existing functionality, as it, if possible, with out making significant changes like inserting to temp table and then moving to the perminant tables.

|||The Bulk Insert command creates an "implicit" transaction around the command. So if there is an error, everything gets rolled back. bcp.exe does not.

If you remove the "-b500000" from the bcp.exe command, you will get all or nothing.
sql

Thursday, March 22, 2012

BULK INSERT to a Temporary Table

I wanto to execute a BULK INSERT command into a Temporary Table:
CREATE TABLE #textfile (line varchar(8000))
-- Read the text file into the temp table
BULK INSERT #textfile FROM 'c\Projects.txt'
But when I execute this command a recieve the following error message:
"The current user is not the database or object owner of table
'#textfile'. Cannot perform SET operation."
Im logged in a Microsoft SQL Server 2000, with a login account that
belogs to the role db_owner of the database Im working, but does not
belong to the role db_owner of tempdb database. Is it necessary?
What am I doing wrong?
Thanks,
Paulo
*** Sent via Developersdex http://www.codecomments.com ***
Hi
You may wantr to check out
http://support.microsoft.com/default...b;en-us;302621 regarding
permissions needed.
John
"Paulo Andre Ortega Ribeiro" wrote:

> I wanto to execute a BULK INSERT command into a Temporary Table:
> CREATE TABLE #textfile (line varchar(8000))
> -- Read the text file into the temp table
> BULK INSERT #textfile FROM 'c\Projects.txt'
> But when I execute this command a recieve the following error message:
> "The current user is not the database or object owner of table
> '#textfile'. Cannot perform SET operation."
> I4m logged in a Microsoft SQL Server 2000, with a login account that
> belogs to the role db_owner of the database I4m working, but does not
> belong to the role db_owner of tempdb database. Is it necessary?
> What am I doing wrong?
> Thanks,
> Paulo
>
> *** Sent via Developersdex http://www.codecomments.com ***
>

BULK INSERT to a Temporary Table

I wanto to execute a BULK INSERT command into a Temporary Table:
CREATE TABLE #textfile (line varchar(8000))
-- Read the text file into the temp table
BULK INSERT #textfile FROM 'c\Projects.txt'
But when I execute this command a recieve the following error message:
"The current user is not the database or object owner of table
'#textfile'. Cannot perform SET operation."
Im logged in a Microsoft SQL Server 2000, with a login account that
belogs to the role db_owner of the database Im working, but does not
belong to the role db_owner of tempdb database. Is it necessary?
What am I doing wrong?
Thanks,
Paulo
*** Sent via Developersdex http://www.codecomments.com ***Hi
You may wantr to check out
http://support.microsoft.com/defaul...kb;en-us;302621 regarding
permissions needed.
John
"Paulo Andre Ortega Ribeiro" wrote:

> I wanto to execute a BULK INSERT command into a Temporary Table:
> CREATE TABLE #textfile (line varchar(8000))
> -- Read the text file into the temp table
> BULK INSERT #textfile FROM 'c\Projects.txt'
> But when I execute this command a recieve the following error message:
> "The current user is not the database or object owner of table
> '#textfile'. Cannot perform SET operation."
> I4m logged in a Microsoft SQL Server 2000, with a login account that
> belogs to the role db_owner of the database I4m working, but does not
> belong to the role db_owner of tempdb database. Is it necessary?
> What am I doing wrong?
> Thanks,
> Paulo
>
> *** Sent via Developersdex http://www.codecomments.com ***
>

BULK INSERT to a Temporary Table

I wanto to execute a BULK INSERT command into a Temporary Table:
CREATE TABLE #textfile (line varchar(8000))
-- Read the text file into the temp table
BULK INSERT #textfile FROM 'c\Projects.txt'
But when I execute this command a recieve the following error message:
"The current user is not the database or object owner of table
'#textfile'. Cannot perform SET operation."
I´m logged in a Microsoft SQL Server 2000, with a login account that
belogs to the role db_owner of the database I´m working, but does not
belong to the role db_owner of tempdb database. Is it necessary?
What am I doing wrong?
Thanks,
Paulo
*** Sent via Developersdex http://www.developersdex.com ***Hi
You may wantr to check out
http://support.microsoft.com/default.aspx?scid=kb;en-us;302621 regarding
permissions needed.
John
"Paulo Andre Ortega Ribeiro" wrote:
> I wanto to execute a BULK INSERT command into a Temporary Table:
> CREATE TABLE #textfile (line varchar(8000))
> -- Read the text file into the temp table
> BULK INSERT #textfile FROM 'c\Projects.txt'
> But when I execute this command a recieve the following error message:
> "The current user is not the database or object owner of table
> '#textfile'. Cannot perform SET operation."
> I4m logged in a Microsoft SQL Server 2000, with a login account that
> belogs to the role db_owner of the database I4m working, but does not
> belong to the role db_owner of tempdb database. Is it necessary?
> What am I doing wrong?
> Thanks,
> Paulo
>
> *** Sent via Developersdex http://www.developersdex.com ***
>

BULK INSERT stop inserting rows

All,

I am just having this weird issue recently with the BULK INSERT command on one of our SQL2K servers which always returns the message "command(s) completed successfully" (Query Analyzer) but no rows get actually inserted into the bulk table.

BULK INSERT LOADTMP FROM 'D:\TEMP\MYFILE.CSV' WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n', MAXERRORS = 20)

> The command(s) completed successfully.

The same BULK INSERT command used to work for the past year or so and still works just fine on the other servers but for some unknown reasons, it just stopped working on this one box while always returning the message: "the command(s) completed successfully"...

I did try to provide some bogus non-existent filename in the BULK INSERT command and it would also return "the command(s) completed successfully" as well - as long as the entire BULK INSERT command syntax and arguments are correct !!!

I thought that I found some kind of solution in the msdn knowledge database related to the issue by re-applying the SQL2K service pack 4 but it didn't fix it. Rebooting the instance and the server several times did not help either.

Have any of you had encountered similar issues before and how to resolve it? Would appreciate all you inputs...

Thanks,
JohnSince you have "MAXERRORS = 20" in your statement, it returns success unless 21 errors occur.

Try setting maxerrors = 1 and see what message you get.

BULK INSERT STATEMENT PROBLEM... ^^

Hi all,

I want to run BULK INSERT command from ASP.NET.. But, I get Error "You dont have permission to use BULK INSERT Command...". And i try to find out the solution from Internet and a lot of people say that I have to create user under Sysadmin or BULKadmin role.

But I dont see Sysadmin and BULKadmin from my MSSQL Enterprise manager. What I see is only Admin and Sys... Any solution??

Thanks

Suigion

Hi,

use sp_addsrvrolemember to add

SysAdmin and BulkAdmin are types of fixed server roles. You can find them under Security, Server Roles node in EM.

Hope this helps you

Tuesday, March 20, 2012

Bulk Insert Problem

Hi to All!
I am trying to use the 'Bulk Insert' command to load a data file into a
MS-SQL db. The line I am using is:
BULK INSERT Data..tbl_load
FROM 'C:\Data\data.txt'
WITH (
DATAFILETYPE = 'char',
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n'
)
Sql Server is giving error:
Server: Msg 4861, Level 16, State 1, Line 1
Could not bulk insert because file 'C:\Data\data.txt'
could not be opened. Operating system error code 3(error not found).
What is the problem in script
*** Sent via Developersdex http://www.examnotes.net ***Hi
Can you show us the text's file structure and data?
"Ghulam Farid" <gfaryd@.yahoo.com> wrote in message
news:Or$4xS$HGHA.3460@.TK2MSFTNGP12.phx.gbl...
> Hi to All!
> I am trying to use the 'Bulk Insert' command to load a data file into a
> MS-SQL db. The line I am using is:
> BULK INSERT Data..tbl_load
> FROM 'C:\Data\data.txt'
> WITH (
> DATAFILETYPE = 'char',
> FIELDTERMINATOR = '\t',
> ROWTERMINATOR = '\n'
> )
>
> Sql Server is giving error:
> Server: Msg 4861, Level 16, State 1, Line 1
> Could not bulk insert because file 'C:\Data\data.txt'
> could not be opened. Operating system error code 3(error not found).
> What is the problem in script
>
> *** Sent via Developersdex http://www.examnotes.net ***|||There is only one column in the text file and data type is "char"
sample data is like
ASU0028010180
ASU0038010181
ASU0028010182
ASU0028010183
ASU0028010184
*** Sent via Developersdex http://www.examnotes.net ***|||two things that I can think of:
Does the account that SQL Server runs with has read permissions on that file
?
The contents of the file is not encrypted, right?
"Ghulam Farid" wrote:

> There is only one column in the text file and data type is "char"
> sample data is like
>
> ASU0028010180
> ASU0038010181
> ASU0028010182
> ASU0028010183
> ASU0028010184
>
> *** Sent via Developersdex http://www.examnotes.net ***
>|||I tried to do some testing
create table ww
(
col1 int,
col2 varchar(50),
col3 varchar (50)
)
insert into ww values (47,'ReadyShip','(503)888-999')
insert into ww values (48,'MyShipper','(503)1212-454')
insert into ww values (49,'ReadyShip','(45)888-999')
insert into ww values (50,'MyShipper','(545)1212-454')
xp_cmdshell 'bcp database.dbo.ww out c:\file.fil -c -t, -SSRV -Usa -PRRR'
BULK INSERT ww FROM 'c:\file.fil' WITH (FIELDTERMINATOR = ',')
"Ghulam Farid" <gfaryd@.yahoo.com> wrote in message
news:eMePaj$HGHA.1760@.TK2MSFTNGP10.phx.gbl...
> There is only one column in the text file and data type is "char"
> sample data is like
>
> ASU0028010180
> ASU0038010181
> ASU0028010182
> ASU0028010183
> ASU0028010184
>
> *** Sent via Developersdex http://www.examnotes.net ***|||From the command-prompt, I entered:
NET HELPMSG 3
The result was:
The system cannot find the path specified.
I suggest you check to ensure 'C:\Data\data.txt' exists at the specified
location on the SQL Server. Note that the path is accessed from the server
rather than the client. Of the file resides on the client machine, you'll
need to create a share and assign file/share permissions to the SQL Server
service in order to BULK INSERT from a network location.
Hope this helps.
Dan Guzman
SQL Server MVP
"Ghulam Farid" <gfaryd@.yahoo.com> wrote in message
news:Or$4xS$HGHA.3460@.TK2MSFTNGP12.phx.gbl...
> Hi to All!
> I am trying to use the 'Bulk Insert' command to load a data file into a
> MS-SQL db. The line I am using is:
> BULK INSERT Data..tbl_load
> FROM 'C:\Data\data.txt'
> WITH (
> DATAFILETYPE = 'char',
> FIELDTERMINATOR = '\t',
> ROWTERMINATOR = '\n'
> )
>
> Sql Server is giving error:
> Server: Msg 4861, Level 16, State 1, Line 1
> Could not bulk insert because file 'C:\Data\data.txt'
> could not be opened. Operating system error code 3(error not found).
> What is the problem in script
>
> *** Sent via Developersdex http://www.examnotes.net ***|||Consider that the BULK INSERT.. command processes on the database server, so
the file data.txt would need to exist on the server HD. However, the bulk
copy DOD command (BCP.EXE) processes on the machine that it is executed on.
BCP.EXE is what you would need to use when bulk copying from a local
workstation to SQL Server.
"Ghulam Farid" <gfaryd@.yahoo.com> wrote in message
news:Or$4xS$HGHA.3460@.TK2MSFTNGP12.phx.gbl...
> Hi to All!
> I am trying to use the 'Bulk Insert' command to load a data file into a
> MS-SQL db. The line I am using is:
> BULK INSERT Data..tbl_load
> FROM 'C:\Data\data.txt'
> WITH (
> DATAFILETYPE = 'char',
> FIELDTERMINATOR = '\t',
> ROWTERMINATOR = '\n'
> )
>
> Sql Server is giving error:
> Server: Msg 4861, Level 16, State 1, Line 1
> Could not bulk insert because file 'C:\Data\data.txt'
> could not be opened. Operating system error code 3(error not found).
> What is the problem in script
>
> *** Sent via Developersdex http://www.examnotes.net ***|||Or run the BCP.exe command from the client machine.
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:OhbSbGCIGHA.3944@.tk2msftngp13.phx.gbl...
> From the command-prompt, I entered:
> NET HELPMSG 3
> The result was:
> The system cannot find the path specified.
> I suggest you check to ensure 'C:\Data\data.txt' exists at the specified
> location on the SQL Server. Note that the path is accessed from the
> server rather than the client. Of the file resides on the client machine,
> you'll need to create a share and assign file/share permissions to the SQL
> Server service in order to BULK INSERT from a network location.
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Ghulam Farid" <gfaryd@.yahoo.com> wrote in message
> news:Or$4xS$HGHA.3460@.TK2MSFTNGP12.phx.gbl...
>

Bulk insert problem

Hi guys
I am using the Bulk insert command to import a flat file to the SQL server
2005.
My flat file contains about 1 million rows.
I have already created an empty table to insert the rows from the flat file.
But I dont want to import the first row into my table in the database.
Is there a way I can give some some sort of condition in my bulk insert
command so that the first row from the flat file does not get imported'
Here is my bulk insert statement:
BULK INSERT new1 FROM 'f:\My Documents\flatFileBaseline\one.txt' WITH
(FIELDTERMINATOR = ',')
Cheers
Mita"mita" <mita@.discussions.microsoft.com> wrote in message
news:F742B532-8FA9-4AC1-89AC-7E50FC757619@.microsoft.com...
> Hi guys
> I am using the Bulk insert command to import a flat file to the SQL server
> 2005.
> My flat file contains about 1 million rows.
> I have already created an empty table to insert the rows from the flat
> file.
> But I dont want to import the first row into my table in the database.
> Is there a way I can give some some sort of condition in my bulk insert
> command so that the first row from the flat file does not get imported'
> Here is my bulk insert statement:
> BULK INSERT new1 FROM 'f:\My Documents\flatFileBaseline\one.txt' WITH
> (FIELDTERMINATOR = ',')
>
Read the entry in Books Online for BULK INSERT.
David

Bulk insert problem

Hi guys
I am using the Bulk insert command to import a flat file to the SQL server
2005.
My flat file contains about 1 million rows.
I have already created an empty table to insert the rows from the flat file.
But I dont want to import the first row into my table in the database.
Is there a way I can give some some sort of condition in my bulk insert
command so that the first row from the flat file does not get imported'
Here is my bulk insert statement:
BULK INSERT new1 FROM 'f:\My Documents\flatFileBaseline\one.txt' WITH
(FIELDTERMINATOR = ',')
Cheers
Mita"mita" <mita@.discussions.microsoft.com> wrote in message
news:F742B532-8FA9-4AC1-89AC-7E50FC757619@.microsoft.com...
> Hi guys
> I am using the Bulk insert command to import a flat file to the SQL server
> 2005.
> My flat file contains about 1 million rows.
> I have already created an empty table to insert the rows from the flat
> file.
> But I dont want to import the first row into my table in the database.
> Is there a way I can give some some sort of condition in my bulk insert
> command so that the first row from the flat file does not get imported'
> Here is my bulk insert statement:
> BULK INSERT new1 FROM 'f:\My Documents\flatFileBaseline\one.txt' WITH
> (FIELDTERMINATOR = ',')
>
Read the entry in Books Online for BULK INSERT.
David

Monday, March 19, 2012

bulk insert not working from asp.netpage

Hi,

I am tryin to run the store procedure which has bulk insert command.when i run the code,it says you don't have permission to use bulk insert command. This query/storeprocedure is running succesfully in the backend(sqlserver2000).

solution is only user in Bulkadmin role has permission for bulk insert .But i don't know how to create one and use it in asp.net.

Can anyone help me in this. ..Here is the code.

string conn_string1 =@."Data Source=SENTHIL\TEST;Initial Catalog=EMPLOYEE;Integrated Security=SSPI";

SqlConnection objconn1=

new SqlConnection(conn_string1);

objconn1.Open();

SqlCommand command1=

new SqlCommand("bulkinsert",objconn1);

command1.CommandType=CommandType.StoredProcedure;

command1.ExecuteNonQuery();

Response.Write("Stored procedure excecuted");

objconn1.Close();

thanks,

kar

In http://msdn2.microsoft.com/en-us/library/ms188365.aspx

Requires INSERT and ADMINISTER BULKOPERATIONS permissions. Additionally, ALTER TABLE permission isrequired if one or more of the following is true:

Constraints exist and the CHECK_CONSTRAINTS option is not specified.

Note: Disabling constraints is the default behavior. To check constraints explicitly, use the CHECK_CONSTRAINTS option.

Sunday, March 11, 2012

Bulk insert from a .csv file

Hi folks,

I have a small problem - I am unable to load data from a .csv file into a table in SQL Server. Here is the command I am running:
BULK INSERT CCSProgramParticipation FROM 'c:\test.csv'
WITH (
DATAFILETYPE = 'char',
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

Data in test.csv is the following format: (date fields can be blank)
NY580232,0,6/30/2006,3567,396,7/1/2005,9/9/2005
NY580232,0,6/30/2006,14850,462,12/12/2005,
...
...

What I see is the data does get loaded; however, data from the following row is getting inserted in the last field of a particular row (previous row) - it seems like the rowterminator is being ignored.

Has anyone encountered this issue? Please let me know your thoughts on this.

Thanks so much!

-ParulTry inserting ROWTERMINATOR ='\r\n'|||I have the following command now:

BULK INSERT CCSTest FROM 'c:\test.csv'
WITH (
DATAFILETYPE = 'char',
FIELDTERMINATOR = ',',
ROWTERMINATOR ='\r\n'
)

But this is erroring out with the following message:
Bulk Insert fails. Column is too long in the data file for row 1, column 7. Make sure the field terminator and row terminator are specified correctly.

I am not sure why...

Thanks!|||because exactly what the message says...your column is too small for the data...|||I understand, but I am not sure why it is erroring out in the first place - the fields are large enough for the data that they are supposed to hold.|||How can you be so sure?

Wht not DTS the file to a staging table and see what it builds, or import it into access

In any event, trying to troubleshoot this without more details is difficult.

Read the hint sticky at the top of the forum and post what it asks for

You may need to attach a sample load file as well|||Here is the file attached...as you try to load this file you will see that some rows are getting clubbed into a single field in the row preceding them...
I really think the file has invalid characters in the last column (date).

Thursday, March 8, 2012

BULK INSERT datafile parameter

Hi everyone,
how can I pass a variable to the command "BULK INSERT", to the datafile
parameter? The following script:
create procedure some_procedure (@.filename varchar(256)) as
begin
bulk insert some_table from @.filename with(codepage='raw');
end;
fails with error "Incorrect syntax near '@.filename'". Any idea?
Thanks,
Tamas Beri
Create a string with the bulk insert command and exec it ie
exec('bulk insert sometable from ' + @.filename + yad yada)
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"gfoyle" <gfoyle@.discussions.microsoft.com> wrote in message
news:546E659E-ABC3-4B34-B6D5-2EB647E3A6A9@.microsoft.com...
> Hi everyone,
> how can I pass a variable to the command "BULK INSERT", to the datafile
> parameter? The following script:
> create procedure some_procedure (@.filename varchar(256)) as
> begin
> bulk insert some_table from @.filename with(codepage='raw');
> end;
> fails with error "Incorrect syntax near '@.filename'". Any idea?
> Thanks,
> Tamas Beri
>
|||Thanks,
it's finally working with the exec...
"Wayne Snyder" wrote:

> Create a string with the bulk insert command and exec it ie
> exec('bulk insert sometable from ' + @.filename + yad yada)
> --
> Wayne Snyder, MCDBA, SQL Server MVP
> Mariner, Charlotte, NC
> www.mariner-usa.com

BULK INSERT datafile parameter

Hi everyone,
how can I pass a variable to the command "BULK INSERT", to the datafile
parameter? The following script:
create procedure some_procedure (@.filename varchar(256)) as
begin
bulk insert some_table from @.filename with(codepage='raw');
end;
fails with error "Incorrect syntax near '@.filename'". Any idea?
Thanks,
Tamas BeriCreate a string with the bulk insert command and exec it ie
exec('bulk insert sometable from ' + @.filename + yad yada)
--
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"gfoyle" <gfoyle@.discussions.microsoft.com> wrote in message
news:546E659E-ABC3-4B34-B6D5-2EB647E3A6A9@.microsoft.com...
> Hi everyone,
> how can I pass a variable to the command "BULK INSERT", to the datafile
> parameter? The following script:
> create procedure some_procedure (@.filename varchar(256)) as
> begin
> bulk insert some_table from @.filename with(codepage='raw');
> end;
> fails with error "Incorrect syntax near '@.filename'". Any idea?
> Thanks,
> Tamas Beri
>|||Thanks,
it's finally working with the exec...
"Wayne Snyder" wrote:
> Create a string with the bulk insert command and exec it ie
> exec('bulk insert sometable from ' + @.filename + yad yada)
> --
> Wayne Snyder, MCDBA, SQL Server MVP
> Mariner, Charlotte, NC
> www.mariner-usa.com

BULK INSERT datafile parameter

Hi everyone,
how can I pass a variable to the command "BULK INSERT", to the datafile
parameter? The following script:
create procedure some_procedure (@.filename varchar(256)) as
begin
bulk insert some_table from @.filename with(codepage='raw');
end;
fails with error "Incorrect syntax near '@.filename'". Any idea?
Thanks,
Tamas BeriCreate a string with the bulk insert command and exec it ie
exec('bulk insert sometable from ' + @.filename + yad yada)
--
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"gfoyle" <gfoyle@.discussions.microsoft.com> wrote in message
news:546E659E-ABC3-4B34-B6D5-2EB647E3A6A9@.microsoft.com...
> Hi everyone,
> how can I pass a variable to the command "BULK INSERT", to the datafile
> parameter? The following script:
> create procedure some_procedure (@.filename varchar(256)) as
> begin
> bulk insert some_table from @.filename with(codepage='raw');
> end;
> fails with error "Incorrect syntax near '@.filename'". Any idea?
> Thanks,
> Tamas Beri
>|||Thanks,
it's finally working with the exec...
"Wayne Snyder" wrote:

> Create a string with the bulk insert command and exec it ie
> exec('bulk insert sometable from ' + @.filename + yad yada)
> --
> Wayne Snyder, MCDBA, SQL Server MVP
> Mariner, Charlotte, NC
> www.mariner-usa.com

Wednesday, March 7, 2012

BULK INSERT crashes Stored Procedure ?

I'm trying to BULK INSERT from a series of files, but the files may or may
not exist.
Problem is that if a BULK INSERT command fails (because the file doesn't
exist), the next line of code in the SP is never reached!
>> Server: Msg 4860, Level 16, State 1, Line 1
>> Could not bulk insert. File 'C:\2003_05_02.txt' does not exist.
>> CODE:
>> set @.cmd = 'BULK INSERT Z_BATCH_IMP_USPS_RAW FROM '''
>> + @.path + @.fn + ''''
>> + ' WITH
>> (
>> FIELDTERMINATOR = ''|''
>> ,ROWTERMINATOR = ''\n''
>> ,TABLOCK
>> )
>> '
>> EXEC( @.cmd ) ; -- BOMBS
>> -- EXEC sp_executesql @.cmd ; -- BOMBS
Naturally I've tried setting XACT_ABORT OFF (among other various
superstitious rituals). Nothing works.
TIA,
--joe
PS,
I don't have control of this server & the current admin & users aren't very
technical... As such I want to avoid using xp_cmdshell... similarly, I don't
want to use a linked text server, and DTS is a problem b/c the file names
change along with the aforementioned administrative issues. I would like to
have something that works stand alone that can run w/out intervention and a
minimum of admin.I googled around for this problem, and it appears that "It's supposed to
work that way" is the answer. Is this the only answer, that this class of
error throws a "fatal" & immediately short-circuits further execution of the
remainder of the SP?
This has become a show stopper at the moment.
I'm trying to run through a directory of ASCII files, so having a fatal on
just one file is a poor option when there are 20 files to import. I'd use
xp_cmdshell, but its permissions seem to get turned off whenever a patch is
applied, and that doesn't guard me against a bad 0-byte ASCII file (no
CR/LF).
Is there perhaps a way to set up DTS so it handles dynamic file names when
importing from ASCII? Any suggestions?
Thanx,
-joe
PS, Just to reiterate from my previous post, I thought of using a linked
OLEDB text server, but that requires the right OLEDB driver and permissions,
along with a capable DBA watching over things (plus if the file doesn't
exist, will the OLEDB driver throw the same kind of fatal?). Using bcp in
DOS from a scheduler doesn't solve the dynamic file name problem (oh yeh,
try programming DOS batch files... it can be done, BUT!!!), and implementing
client software (perl, WSE, etc.) on the server or workstation creates
complexity & other headaches. Please, oh dear God, Why ME!!!!' Tell them
to go back to the @.#$ mainframe, hell, they were better off with 3x5
cards!!!!!!
Previous n.g. thread:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&frame=right&th=28fd4c29145420ba&seekm=OQmAG3VcBHA.1848%40tkmsftngp05#link12
thread:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=3BF9B2DC.65712B51%40drew.edu&rnum=1&prev=/groups%3Fq%3Dhandling%2Berrors%2BCould%2Bnot%2Bbulk%2Binsert.%2BFile%2Bgroup:microsoft.public.sqlserver.programming%2Bgroup:microsoft.public.sqlserver.programming%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26group%3Dmicrosoft.public.sqlserver.programming%26selm%3D3BF9B2DC.65712B51%2540drew.edu%26rnum%3D1
--
+--
To e-mail me,
replace "REPLACETHIS" with buddhashortfatguy
"buddhashortfatguy" <REPLACETHIS@.yahoo.com> wrote in message
news:W4xsb.34667$Mc.21141@.twister.austin.rr.com...
> I'm trying to BULK INSERT from a series of files, but the files may or may
> not exist.
> Problem is that if a BULK INSERT command fails (because the file doesn't
> exist), the next line of code in the SP is never reached!
> >> Server: Msg 4860, Level 16, State 1, Line 1
> >> Could not bulk insert. File 'C:\2003_05_02.txt' does not exist.
> >> CODE:
> >> set @.cmd = 'BULK INSERT Z_BATCH_IMP_USPS_RAW FROM '''
> >> + @.path + @.fn + ''''
> >> + ' WITH
> >> (
> >> FIELDTERMINATOR = ''|''
> >> ,ROWTERMINATOR = ''\n''
> >> ,TABLOCK
> >> )
> >> '
> >> EXEC( @.cmd ) ; -- BOMBS
> >> -- EXEC sp_executesql @.cmd ; -- BOMBS
>
> Naturally I've tried setting XACT_ABORT OFF (among other various
> superstitious rituals). Nothing works.
>
> TIA,
> --joe
> PS,
> I don't have control of this server & the current admin & users aren't
very
> technical... As such I want to avoid using xp_cmdshell... similarly, I
don't
> want to use a linked text server, and DTS is a problem b/c the file names
> change along with the aforementioned administrative issues. I would like
to
> have something that works stand alone that can run w/out intervention and
a
> minimum of admin.
>
>|||I just came up with a ghastly hack for a solution... a tad dangerous
perhaps...
In the SP, I log the file in a log table before I run the BULK INSERT. The
SP checks in the log table see if an attempt has been made on that file name
before, and will loop if an attempt has already been made.
I then set up a 2-step job on the server. Both steps call the same SP, and
step 1 calls step 2 on failure, and step 2 calls step 1 on failure. Until
the SP runs out of possible file names (filename is based on date in a
xxxxmmddyy.txt format), it'll keep BULK INSERTing files it finds and failing
on ones it doesn't (the SP succeeds when it can exit without error, when it
runs out of names, the maximum of which is based upon today's date).
Finally, both job steps (whichever one happens to be running at the time)
exit the job on success (which is again, running out of file names to look
for).
As I said, a ghastly hack.
+--
To e-mail me,
replace "REPLACETHIS" with buddhashortfatguy
"buddhashortfatguy" <REPLACETHIS@.yahoo.com> wrote in message
news:W4xsb.34667$Mc.21141@.twister.austin.rr.com...
> I'm trying to BULK INSERT from a series of files, but the files may or may
> not exist.
> Problem is that if a BULK INSERT command fails (because the file doesn't
> exist), the next line of code in the SP is never reached!
> >> Server: Msg 4860, Level 16, State 1, Line 1
> >> Could not bulk insert. File 'C:\2003_05_02.txt' does not exist.
> >> CODE:
> >> set @.cmd = 'BULK INSERT Z_BATCH_IMP_USPS_RAW FROM '''
> >> + @.path + @.fn + ''''
> >> + ' WITH
> >> (
> >> FIELDTERMINATOR = ''|''
> >> ,ROWTERMINATOR = ''\n''
> >> ,TABLOCK
> >> )
> >> '
> >> EXEC( @.cmd ) ; -- BOMBS
> >> -- EXEC sp_executesql @.cmd ; -- BOMBS
>
> Naturally I've tried setting XACT_ABORT OFF (among other various
> superstitious rituals). Nothing works.
>
> TIA,
> --joe
> PS,
> I don't have control of this server & the current admin & users aren't
very
> technical... As such I want to avoid using xp_cmdshell... similarly, I
don't
> want to use a linked text server, and DTS is a problem b/c the file names
> change along with the aforementioned administrative issues. I would like
to
> have something that works stand alone that can run w/out intervention and
a
> minimum of admin.
>
>|||Before you start using Bulk Insert, can you confirm if the file really exists , you might want to use the command "xp_fileexist"|||"Prasanna" <anonymous@.discussions.microsoft.com> wrote in message
news:D1E87B91-DD63-4B62-BC27-1DC437CAB343@.microsoft.com...
> Before you start using Bulk Insert, can you confirm if the file really
exists , you might want to use the command "xp_fileexist"
No such xp_ on their machine, although xp_cmdshell is there, and worked at
one point in time. Plus, the admin situation is problematic such that xp_*
priveleges are revoked when service packs get applied.
This has to work w/out any special priveleges or assignments, just a job
that works reliably (nightly) w/out user intervention.
Even so, if the ASCII files are 0-Byte or formatted badly in any regard,
BULK INSERT will also make a T-SQL stored proc bomb out w/ a fatal, so even
if I knew the file existed, it doesn't protect the batch process from
interruption.
I *did* find a workaround to this problem. See my last reply to myself in
the thread. It's too bad that MS never got around to fixing these issues
(poor T-SQL error handling) until Longhorn.
Thanks,|||just a fyi, enhanced error handling has nothing to do with longhorn. it's to
do with yukon.
--
-oj
RAC v2.2 & QALite!
http://www.rac4sql.net
"buddhashortfatguy" <REPLACETHIS@.yahoo.com> wrote in message
news:HlFsb.36901$Mc.28999@.twister.austin.rr.com...
> "Prasanna" <anonymous@.discussions.microsoft.com> wrote in message
> news:D1E87B91-DD63-4B62-BC27-1DC437CAB343@.microsoft.com...
> > Before you start using Bulk Insert, can you confirm if the file really
> exists , you might want to use the command "xp_fileexist"
> No such xp_ on their machine, although xp_cmdshell is there, and worked at
> one point in time. Plus, the admin situation is problematic such that xp_*
> priveleges are revoked when service packs get applied.
> This has to work w/out any special priveleges or assignments, just a job
> that works reliably (nightly) w/out user intervention.
> Even so, if the ASCII files are 0-Byte or formatted badly in any regard,
> BULK INSERT will also make a T-SQL stored proc bomb out w/ a fatal, so
even
> if I knew the file existed, it doesn't protect the batch process from
> interruption.
> I *did* find a workaround to this problem. See my last reply to myself in
> the thread. It's too bad that MS never got around to fixing these issues
> (poor T-SQL error handling) until Longhorn.
> Thanks,
>
>|||ahhhh, you are my oj in the mourning... and yukon correct me any time you
want when i get the code names for MS products mixed up
--
+--
To e-mail me,
replace "REPLACETHIS" with buddhashortfatguy
"oj" <nospam_ojngo@.home.com> wrote in message
news:e3vmQWbqDHA.2496@.TK2MSFTNGP09.phx.gbl...
> just a fyi, enhanced error handling has nothing to do with longhorn. it's
to
> do with yukon.
> --
> -oj
> RAC v2.2 & QALite!
> http://www.rac4sql.net
>
> "buddhashortfatguy" <REPLACETHIS@.yahoo.com> wrote in message
> news:HlFsb.36901$Mc.28999@.twister.austin.rr.com...
> > "Prasanna" <anonymous@.discussions.microsoft.com> wrote in message
> > news:D1E87B91-DD63-4B62-BC27-1DC437CAB343@.microsoft.com...
> > > Before you start using Bulk Insert, can you confirm if the file really
> > exists , you might want to use the command "xp_fileexist"
> >
> > No such xp_ on their machine, although xp_cmdshell is there, and worked
at
> > one point in time. Plus, the admin situation is problematic such that
xp_*
> > priveleges are revoked when service packs get applied.
> >
> > This has to work w/out any special priveleges or assignments, just a job
> > that works reliably (nightly) w/out user intervention.
> >
> > Even so, if the ASCII files are 0-Byte or formatted badly in any regard,
> > BULK INSERT will also make a T-SQL stored proc bomb out w/ a fatal, so
> even
> > if I knew the file existed, it doesn't protect the batch process from
> > interruption.
> >
> > I *did* find a workaround to this problem. See my last reply to myself
in
> > the thread. It's too bad that MS never got around to fixing these issues
> > (poor T-SQL error handling) until Longhorn.
> >
> > Thanks,
> >
> >
> >
>|||The SP "xp_fileexist" exists in Master db, not in the User DB.|||Thanks, I gather that it's new in SS2K?
It's not listed in the SS2K BOL in the T-SQL ref. Doesn't require additional
rights, great!
It still doesn't solve the problem with any other kind of fatal error in
BULK INSERT (a 0-byte file, for instance).
The bottom line is that MS SQL 7's & 8's error handling isn't very capable
and I'll still have to either go w/ a hacked workaround in the job manager
or a workaround in client software (Perl, WSE...).
What really makes me wonder is, why in the first place was BULK INSERT
implemented to throw fatal errors *at all?* It's an import utility, that's
all it is - it's not a table reference that is the subject of
latent/deferred binding. Makes me wonder about the underpinning API, the
(familiar) scent of old Sybase code lurking under the hood.
"Prasanna" <anonymous@.discussions.microsoft.com> wrote in message
news:8AD1A570-60F4-48CF-B368-73369419CC27@.microsoft.com...
> The SP "xp_fileexist" exists in Master db, not in the User DB.|||Just to let you know you're not alone,
I posted this a few hours before you.
http://communities.microsoft.com/newsgroups/previewFrame.asp?ICP=msdn&sLCID=
us&sgroupURL=microsoft.public.sqlserver.programming&sMessageID=%253CuaBAWQSq
DHA.1884@.TK2MSFTNGP09.phx.gbl%253E.
At the moment I'm using this technic: in the sp that needs to bulk insert I
assign the text of the format file to a variable and create the file on the
fly (using a wrapper to the file system object), something like this:
/*********/
declare @.txt varchar(8000)
declare @.FormatFilePath varchar(255)
declare @.path varchar(255)
set @.txt='8.0
1
1 SQLCHAR 0 0 "\r\n" 2 Code ""
'
select @.path='Target.dat', @.FormatFilePath='c\winnt\temp\Target.fmt'
exec @.Res=dbo.st_WriteToFile @.FormatFilePath, @.txt --This is a FSO wrapper
that creates or overwrites a file with the specified text
if @.Res<>0
goto ErrorHandler
create table Target(
PKCheck int Identity(1,1),
Code char(15)
)
exec(N'bulk insert Target from '''
+ @.path
+ N''' with (FORMATFILE='''
+ @.TempDirPath
+ N''', TABLOCK)')
/**********/
It works fine, but of course if the data file or the table change their
structures you have to modify the sp, and you can't use third parties format
file without checking for their correctness.
I' was trying to generalize this approch by adding error controls when I
stumbled into your same problem.
Now I'm thinking of using BCP's format option via xp_cmdshell to produce a
valid format file, but of course I have to take into account the cases when
the number of table fields and file fields and their mutual positions
differ, but the problem of using format files not created by you remains.
Still working on.
Salvor
--
++++++++++++++
To e-mail me,
remove ".NO_SPAM" from my e-mail address

Bulk Insert Command

I am trying to use the 'Bulk Insert' command to load a data file into a
MS-SQL db. The line I am using is:

Bulk Insert SVC_Details From "C:\XFILE.TXT" With (FieldTerminator = ',')

I have tried the file name with " around it and with ' around it and both
the " and ', but every time the Query Analyzer comes back with the following
error:

--Begin Error Msg--
Server: Msg 4861, Level 16, State 1, Line 1
Could not bulk insert because file ''C:\XFILE.TXT'' could not be opened.
Operating system error code 123(The filename, directory name, or volume
label syntax is incorrect.).
--End Msg--

I am not sure what is wrong with this line. Can any one tell me what is
wrong?

Mike
m charney at dunlap hospital dot orgDo not use " but use ' and be sure that the file is there

i have uset this query

bulk insert tlf.dbo.bruger_data from 'C:\TEKST.txt'
with
(
FIRSTROW = 1,
FIELDTERMINATOR = '";"',
ROWTERMINATOR = '"\n'
)

"Mike" <noway@.forgetit.com> skrev i en meddelelse
news:66x3f.181$aV5.91@.newssvr30.news.prodigy.com.. .
>I am trying to use the 'Bulk Insert' command to load a data file into a
>MS-SQL db. The line I am using is:
> Bulk Insert SVC_Details From "C:\XFILE.TXT" With (FieldTerminator = ',')
> I have tried the file name with " around it and with ' around it and both
> the " and ', but every time the Query Analyzer comes back with the
> following error:
> --Begin Error Msg--
> Server: Msg 4861, Level 16, State 1, Line 1
> Could not bulk insert because file ''C:\XFILE.TXT'' could not be opened.
> Operating system error code 123(The filename, directory name, or volume
> label syntax is incorrect.).
> --End Msg--
> I am not sure what is wrong with this line. Can any one tell me what is
> wrong?
> Mike
> m charney at dunlap hospital dot org

BULK INSERT and the ERRORFILE parameter

Can someone tell me if the ERRORFILE parameter is a valid (and
functioning) parameter for the BULK INSERT command when using SQL
Server 2000?
Finding documentation on this has been difficult, but it appears to be
a new parameter under the BULK INSERT command for 2005. If I look at
the list of parameters for this command under 2000, it doesn't appear
(see
http://msdn.microsoft.com/library/?...lref/ts_ba-b...
)
However if I run a BULK INSERT command under 2000 with the ERRORFILE
parameter, it doesn't give me a syntax error, it just appears to ignore
it.
Was the ERRORFILE option available under SQL 2000?
Thanks in advance,
Nick.Hi
ERRORFILE does not seem to produce and error in SQL 2000, but as you have
found it does seem to be ignored. The syntax in books online does not have
this option, therefore I think it is a new feature for SQL 2005. You could
use BCP if you really need this.
John
"Nick" wrote:

> Can someone tell me if the ERRORFILE parameter is a valid (and
> functioning) parameter for the BULK INSERT command when using SQL
> Server 2000?
> Finding documentation on this has been difficult, but it appears to be
> a new parameter under the BULK INSERT command for 2005. If I look at
> the list of parameters for this command under 2000, it doesn't appear
> (see
> http://msdn.microsoft.com/library/?...lref/ts_ba-b...
> )
> However if I run a BULK INSERT command under 2000 with the ERRORFILE
> parameter, it doesn't give me a syntax error, it just appears to ignore
> it.
> Was the ERRORFILE option available under SQL 2000?
> Thanks in advance,
> Nick.
>

BULK INSERT and the ERRORFILE parameter

Can someone tell me if the ERRORFILE parameter is a valid (and
functioning) parameter for the BULK INSERT command when using SQL
Server 2000?
Finding documentation on this has been difficult, but it appears to be
a new parameter under the BULK INSERT command for 2005. If I look at
the list of parameters for this command under 2000, it doesn't appear
(see
http://msdn.microsoft.com/library/?url=/library/en-us/tsqlref/ts_ba-b...
)
However if I run a BULK INSERT command under 2000 with the ERRORFILE
parameter, it doesn't give me a syntax error, it just appears to ignore
it.
Was the ERRORFILE option available under SQL 2000?
Thanks in advance,
Nick.Hi
ERRORFILE does not seem to produce and error in SQL 2000, but as you have
found it does seem to be ignored. The syntax in books online does not have
this option, therefore I think it is a new feature for SQL 2005. You could
use BCP if you really need this.
John
"Nick" wrote:
> Can someone tell me if the ERRORFILE parameter is a valid (and
> functioning) parameter for the BULK INSERT command when using SQL
> Server 2000?
> Finding documentation on this has been difficult, but it appears to be
> a new parameter under the BULK INSERT command for 2005. If I look at
> the list of parameters for this command under 2000, it doesn't appear
> (see
> http://msdn.microsoft.com/library/?url=/library/en-us/tsqlref/ts_ba-b...
> )
> However if I run a BULK INSERT command under 2000 with the ERRORFILE
> parameter, it doesn't give me a syntax error, it just appears to ignore
> it.
> Was the ERRORFILE option available under SQL 2000?
> Thanks in advance,
> Nick.
>

BULK INSERT and Application role

On a connection I use an application role via command:
sp_setapprole 'AppRole', 'xxxx'
When I use a command like this:
BULK INSERT TableX FROM 'C:\tmp\file.dat' WITH
( FORMATFILE='C:\tmp\file.fmt', ROWS_PER_BATCH=10, TABLOCK )
I got the error:
Msg 4834, Level 16, State 4, Line 4
You do not have permission to use the bulk load statement.
so, I would add bulkadmin permission to my applicatio role.
Is it possible ?The bulkadmin role is a server-level role. Application roles are at the
database level. If you are using SQL Server 2005, you can create a stored
proc where the BULK INSERT is done with elevated privileges and then grant
EXEC to the app role.
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau
"Max" <Max@.discussions.microsoft.com> wrote in message
news:7167E83B-538F-4D72-BF0D-70F757FC289F@.microsoft.com...
On a connection I use an application role via command:
sp_setapprole 'AppRole', 'xxxx'
When I use a command like this:
BULK INSERT TableX FROM 'C:\tmp\file.dat' WITH
( FORMATFILE='C:\tmp\file.fmt', ROWS_PER_BATCH=10, TABLOCK )
I got the error:
Msg 4834, Level 16, State 4, Line 4
You do not have permission to use the bulk load statement.
so, I would add bulkadmin permission to my applicatio role.
Is it possible ?