Showing posts with label statement. Show all posts
Showing posts with label statement. Show all posts

Thursday, March 22, 2012

Bulk Insert statement with JOIN !!!

Hi,

I have two tables. Employees and departments as follows:

Employees
- Name
- Salary
- Department ID

Departments
-Department ID
-Department Name

Now what I am doing is that I am allowing the user to give me a CSV file with all the employees data to bulk insert it in the table Employees as follows:

Employee Name, Salary, Department Name

The problem is that the users know nothing about the department ID. So what they type in the CSV ffile as you can see above is the department name.

The SQL statement I normally use for bulk insert is:

BULK INSERT CAS.dbo.employees

FROM 'c:\list.csv' WITH

(FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' )


My question is, how can I use the same technique but insert the departments IDs not name as in the CSV file.!!!!

Appreciate your help.
Thanks

If you use SQL Server 2005 you could use OPENROWSET function with BULK option. OPENROWSET if Table-value function, so you could use it wiht JOIN. Then use INSERT INTO:

Code Snippet

create table Employees(

Name nvarchar(20),

Salary decimal,

DepartmentID int

)

go

create table Departments

(

DepartmentID int,

DepartmentName nvarchar(20)

)

go

insert into Departments values(1,'Dep1')

insert into Departments values(2,'Dep2')

--Select only

select Name, Salary, DepartmentID

from OPENROWSET(BULK 'C:\emp.txt', FORMATFILE='C:\format_file.txt') emp

JOIN Departments dep on (emp.DepartmentName=dep.DepartmentName)

--Insert data into Employees table

insert into Employees

select Name, Salary, DepartmentID

from OPENROWSET(BULK 'C:\emp.txt', FORMATFILE='C:\format_file.txt') emp

JOIN Departments dep on (emp.DepartmentName=dep.DepartmentName)

My CSV file:

Code Snippet

User1,100,Dep1
User2,1000,Dep2

My format file:

Code Snippet

<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/formathttp://schemas.microsoft.com/sqlserver/2004/bulkload/format">http://schemas.microsoft.com/sqlserver/2004/bulkload/format</A< A>>" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
'>http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
; <FIELD ID="1" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="20"/>
<FIELD ID="2" xsi:type="CharTerm" TERMINATOR="," MAX_LENGTH="20"/>
<FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="20"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="Name" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="2" NAME="Salary" xsi:type="SQLDECIMAL"/>
<COLUMN SOURCE="3" NAME="DepartmentName" xsi:type="SQLNVARCHAR"/>
</ROW>
</BCPFORMAT>

|||Thanks, that does it.

Cheers

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

Bulk Insert Statement

Hi All

I have a text file (sample below) i am bulk loading it into a staging table, the problem i am getting is the data is being loaded and scrambling the rows and I need the data to be imported exactly the same row by row

040207,"1007","","3319506/"
031207,"1509",">","US78016"
031207,"1509",">","AA004388"
031207,"1509",">","COMD88"
031207,"1509",">","US78016"
031207,"1509",">","AA001601"
031207,"1509",">","COMD88"
031207,"1510",">","US78016"
031207,"1510",">","AA004337"
031207,"1510",">","COMD88"
031307,"1138",">","US78016"
031307,"1139",">","AA004293"
031307,"1139",">","COMD81"


set nocount on
bulk insert data_load_stage.dbo.
from 'C:\load\CM07.txt'
with ( fieldterminator = ',')

missing a switch i think

thanks in advance

rich

Richie,

You'll need to add a clustered index to your staging table and then add the ORDER hint to your BULK INSERT statement (both matching the ordering of the input file).

From BULK INSERT in BOL:

ORDER ( { column [ ASC | DESC ] } [ ,... n ] )

Specifies how the data in the data file is sorted. Bulk load operation performance is improved if the data loaded is sorted according to the clustered index on the table. If the data file is sorted in a different order, or there is no clustered index on the table, the ORDER clause is ignored. The column names supplied must be valid columns in the destination table. By default, the bulk insert operation assumes the data file is unordered.

sql

Bulk Insert Statement

I am trying to perform bulk insert but it is not working.

I created a table

INSERT INTO [demo].[dbo].[test]

([ID] ,[Plates] ,[Driver])

VALUES

(<ID, int,> ,<Plates, varchar(7),> ,<Driver, varchar(7),>)

I am tryin to insert a file.txt

100, 091-184, DOUG798
101, 406-846, DALL152
102, 384-080, TIZE489
103, 064-460, NAMO927
104, 101-366, CETI001
105, 109-366, JESS111

Any help, please

Juvan

hi Juvan,

the syntax you used is not correct for BULK INSERT statement,
have a look at http://msdn2.microsoft.com/en-us/library/ms188365.aspx for the statement's full synopsis and syntax..

/*

[FILE d:\fmt.txt]

9.0

3

1 SQLCHAR 0 10 "," 1 FirstName "Latin1_General_CI_AS"

2 SQLCHAR 0 10 "," 2 LastName "Latin1_General_CI_AS"

3 SQLCHAR 0 8 "\r\n" 3 BDate "Latin1_General_CI_AS"

[/FILE d:\fmt.txt]

[FILE d:\studs.txt]

Juvan, Bonni, 19701015

Andrea, Montanari, 19651030

[/FILE d:\studs.txt]

*/

SET NOCOUNT ON;

USE tempdb;

GO

CREATE TABLE dbo.Students(

FirstName varchar(10) NOT NULL,

LastName varchar(10) NOT NULL,

BDate datetime NOT NULL

);

GO

BULK INSERT dbo.Students

FROM 'd:\studs.txt'

WITH(FORMATFILE = 'd:\fmt.txt') ;

GO

SELECT * FROM dbo.Students;

GO

DROP TABLE dbo.Students;

--<--

FirstName LastName BDate

- - --

Juvan Bonni 1970-10-15 00:00:00.000

Andrea Montanari 1965-10-30 00:00:00.000

regards

Bulk Insert statement

Hi,

I was using a BULK INSERT statement in a stored procedure.Could any one help me out on one prob.I wanted to let the user select the file he wants to update and then i want to pass this address as a parameter in the stored procedure.

Suppose there is a parameter @.loc,so i want to use this parameter as

Bulk insert TableName from @.loc with(fieldterminator=',')
plz help me out thxI haven't test this but you may take help of Dynamic SQL, http://www.sommarskog.se/dynamic_sql.html fyi.|||Hi its been days now is there no one who could help me out?Is it because i am asking something that is impossible or something wrong plz let me know so that i would not waste my time n go ahead with something that would fulfill my task|||Did you even try dynamic sql as suggested to you?
declare @.fn varchar(255)
set @.fn='\\mypc\tmp\tmp1.txt'
exec ('bulk insert mytable from '''+@.fn+''' with(fieldterminator='','')')|||Hi its been days now is there no one who could help me out?Is it because i am asking something that is impossible or something wrong plz let me know so that i would not waste my time n go ahead with something that would fulfill my task
u haven't replied to Satya's solution.Then how we know that u got the answer or not?|||Even here u are giving the static location of the file from which u want the bulk insert the data from.What i wanted was to allow the user to define the path of the file and i wanted to pass this path as a parameter .In the example u have given its seems to me the same thing.May be if you could explain me more as i am a newbie.Sorry for the inconvienience.|||also i did try this code-:
@.loc nvarchar (50)
Bulk Insert Tablename From '''@.loc'' with(fieldterminator='','')
but i get an error while saving this stored procedure which says
"Cant find @.loc"|||post ur stored procedure|||Even here u are giving the static location
No, I am using a variable. Here are some code for you to play with
Note: UNC path used for the load as the server is not running on my local PC.
C:\tmp>echo aaa,bbb >tmp.txt

C:\tmp>more tmp.txt
aaa,bbb

C:\tmp>"C:\Program Files\Microsoft SQL Server\80\Tools\Binn\isql" -U sa -S devdb
Password:
1> use tempdb
2> go
1> create proc pdreyer_load
2> @.filename varchar(255)
3> as
4> exec ('bulk insert #t1 from '''+@.filename+''' with(fieldterminator='','')')
5> go
1> create table #t1 (f1 varchar(10),f2 varchar(10))
2> exec pdreyer_load '\\pdreyer\tmp\tmp.txt'
3> select * from #t1
4> go
f1 f2
---- ----
aaa bbb

(1 row affected)
1> drop table #t1
2> drop procedure pdreyer_load
3> go
1> exit

C:\tmp>del tmp.txt|||Is the source file changed all the time?

Bulk insert silently does nothing

Hi.. I am using a simple bulk insert statement which works on all our
servers except one. The servers are windows 2000 and are running
Sql Server SP4.
The bulk insert statement simply does nothing.
In query analyser, it returns
"The command(s) completed successfully." and yet returns no rowcount, error
etc
There are no events in the event log and no errors in the sql log
DTS bulk insert happily completes without doing a thing.
bcp of the same file does work however (from the server ) ..
Can someone point me in the right direction regarding DLL's etc to be
checkig please ?
Thanks Dave
Did you try:
-o output_file
Specifies the name of a file that receives output from bcp redirected from
the command prompt.
"gilkida" <gilkida@.discussions.microsoft.com> wrote in message
news:894DB758-93BC-432D-8167-F05F83B2467C@.microsoft.com...
> Hi.. I am using a simple bulk insert statement which works on all our
> servers except one. The servers are windows 2000 and are running
> Sql Server SP4.
> The bulk insert statement simply does nothing.
> In query analyser, it returns
> "The command(s) completed successfully." and yet returns no rowcount,
> error
> etc
> There are no events in the event log and no errors in the sql log
> DTS bulk insert happily completes without doing a thing.
> bcp of the same file does work however (from the server ) ..
> Can someone point me in the right direction regarding DLL's etc to be
> checkig please ?
> Thanks Dave
|||Sorry Chris.. probably not clear.. I am running the Sql Command Bult Insert
BULK INSERT t_BulkLoad FROM 'Some Filename"
works on every server but one... t
"ChrisR" wrote:

> Did you try:
> -o output_file
> Specifies the name of a file that receives output from bcp redirected from
> the command prompt.
>
>
> "gilkida" <gilkida@.discussions.microsoft.com> wrote in message
> news:894DB758-93BC-432D-8167-F05F83B2467C@.microsoft.com...
>
>
|||Woops, sorry.
"gilkida" <gilkida@.discussions.microsoft.com> wrote in message
news:C20BE904-1DDD-4511-ADBD-11C98CB62A98@.microsoft.com...[vbcol=seagreen]
> Sorry Chris.. probably not clear.. I am running the Sql Command Bult
> Insert
> BULK INSERT t_BulkLoad FROM 'Some Filename"
> works on every server but one... t
>
> "ChrisR" wrote:
|||I have the same issue. I found this posted today.
http://support.microsoft.com/?kbid=896425
It says it's applicable to Standard Edition SP3, I too have Enterprise
Edition SP4 + hotfix.
"ChrisR" wrote:

> Woops, sorry.
>
> "gilkida" <gilkida@.discussions.microsoft.com> wrote in message
> news:C20BE904-1DDD-4511-ADBD-11C98CB62A98@.microsoft.com...
>
>

Bulk insert silently does nothing

Hi.. I am using a simple bulk insert statement which works on all our
servers except one. The servers are windows 2000 and are running
Sql Server SP4.
The bulk insert statement simply does nothing.
In query analyser, it returns
"The command(s) completed successfully." and yet returns no rowcount, error
etc
There are no events in the event log and no errors in the sql log
DTS bulk insert happily completes without doing a thing.
bcp of the same file does work however (from the server ) ..
Can someone point me in the right direction regarding DLL's etc to be
checkig please ?
Thanks DaveDid you try:
-o output_file
Specifies the name of a file that receives output from bcp redirected from
the command prompt.
"gilkida" <gilkida@.discussions.microsoft.com> wrote in message
news:894DB758-93BC-432D-8167-F05F83B2467C@.microsoft.com...
> Hi.. I am using a simple bulk insert statement which works on all our
> servers except one. The servers are windows 2000 and are running
> Sql Server SP4.
> The bulk insert statement simply does nothing.
> In query analyser, it returns
> "The command(s) completed successfully." and yet returns no rowcount,
> error
> etc
> There are no events in the event log and no errors in the sql log
> DTS bulk insert happily completes without doing a thing.
> bcp of the same file does work however (from the server ) ..
> Can someone point me in the right direction regarding DLL's etc to be
> checkig please ?
> Thanks Dave|||Sorry Chris.. probably not clear.. I am running the Sql Command Bult Insert
BULK INSERT t_BulkLoad FROM 'Some Filename"
works on every server but one... t
"ChrisR" wrote:
> Did you try:
> -o output_file
> Specifies the name of a file that receives output from bcp redirected from
> the command prompt.
>
>
> "gilkida" <gilkida@.discussions.microsoft.com> wrote in message
> news:894DB758-93BC-432D-8167-F05F83B2467C@.microsoft.com...
> > Hi.. I am using a simple bulk insert statement which works on all our
> > servers except one. The servers are windows 2000 and are running
> > Sql Server SP4.
> >
> > The bulk insert statement simply does nothing.
> >
> > In query analyser, it returns
> > "The command(s) completed successfully." and yet returns no rowcount,
> > error
> > etc
> >
> > There are no events in the event log and no errors in the sql log
> >
> > DTS bulk insert happily completes without doing a thing.
> > bcp of the same file does work however (from the server ) ..
> >
> > Can someone point me in the right direction regarding DLL's etc to be
> > checkig please ?
> >
> > Thanks Dave
>
>|||Woops, sorry.
"gilkida" <gilkida@.discussions.microsoft.com> wrote in message
news:C20BE904-1DDD-4511-ADBD-11C98CB62A98@.microsoft.com...
> Sorry Chris.. probably not clear.. I am running the Sql Command Bult
> Insert
> BULK INSERT t_BulkLoad FROM 'Some Filename"
> works on every server but one... t
>
> "ChrisR" wrote:
>> Did you try:
>> -o output_file
>> Specifies the name of a file that receives output from bcp redirected
>> from
>> the command prompt.
>>
>>
>> "gilkida" <gilkida@.discussions.microsoft.com> wrote in message
>> news:894DB758-93BC-432D-8167-F05F83B2467C@.microsoft.com...
>> > Hi.. I am using a simple bulk insert statement which works on all our
>> > servers except one. The servers are windows 2000 and are running
>> > Sql Server SP4.
>> >
>> > The bulk insert statement simply does nothing.
>> >
>> > In query analyser, it returns
>> > "The command(s) completed successfully." and yet returns no rowcount,
>> > error
>> > etc
>> >
>> > There are no events in the event log and no errors in the sql log
>> >
>> > DTS bulk insert happily completes without doing a thing.
>> > bcp of the same file does work however (from the server ) ..
>> >
>> > Can someone point me in the right direction regarding DLL's etc to be
>> > checkig please ?
>> >
>> > Thanks Dave
>>|||I have the same issue. I found this posted today.
http://support.microsoft.com/?kbid=896425
It says it's applicable to Standard Edition SP3, I too have Enterprise
Edition SP4 + hotfix.
"ChrisR" wrote:
> Woops, sorry.
>
> "gilkida" <gilkida@.discussions.microsoft.com> wrote in message
> news:C20BE904-1DDD-4511-ADBD-11C98CB62A98@.microsoft.com...
> > Sorry Chris.. probably not clear.. I am running the Sql Command Bult
> > Insert
> >
> > BULK INSERT t_BulkLoad FROM 'Some Filename"
> > works on every server but one... t
> >
> >
> > "ChrisR" wrote:
> >
> >> Did you try:
> >>
> >> -o output_file
> >>
> >> Specifies the name of a file that receives output from bcp redirected
> >> from
> >> the command prompt.
> >>
> >>
> >>
> >>
> >>
> >> "gilkida" <gilkida@.discussions.microsoft.com> wrote in message
> >> news:894DB758-93BC-432D-8167-F05F83B2467C@.microsoft.com...
> >> > Hi.. I am using a simple bulk insert statement which works on all our
> >> > servers except one. The servers are windows 2000 and are running
> >> > Sql Server SP4.
> >> >
> >> > The bulk insert statement simply does nothing.
> >> >
> >> > In query analyser, it returns
> >> > "The command(s) completed successfully." and yet returns no rowcount,
> >> > error
> >> > etc
> >> >
> >> > There are no events in the event log and no errors in the sql log
> >> >
> >> > DTS bulk insert happily completes without doing a thing.
> >> > bcp of the same file does work however (from the server ) ..
> >> >
> >> > Can someone point me in the right direction regarding DLL's etc to be
> >> > checkig please ?
> >> >
> >> > Thanks Dave
> >>
> >>
> >>
>
>

Bulk insert silently does nothing

Hi.. I am using a simple bulk insert statement which works on all our
servers except one. The servers are Windows 2000 and are running
Sql Server SP4.
The bulk insert statement simply does nothing.
In query analyser, it returns
"The command(s) completed successfully." and yet returns no rowcount, error
etc
There are no events in the event log and no errors in the sql log
DTS bulk insert happily completes without doing a thing.
bcp of the same file does work however (from the server ) ..
Can someone point me in the right direction regarding DLL's etc to be
checkig please ?
Thanks DaveDid you try:
-o output_file
Specifies the name of a file that receives output from bcp redirected from
the command prompt.
"gilkida" <gilkida@.discussions.microsoft.com> wrote in message
news:894DB758-93BC-432D-8167-F05F83B2467C@.microsoft.com...
> Hi.. I am using a simple bulk insert statement which works on all our
> servers except one. The servers are Windows 2000 and are running
> Sql Server SP4.
> The bulk insert statement simply does nothing.
> In query analyser, it returns
> "The command(s) completed successfully." and yet returns no rowcount,
> error
> etc
> There are no events in the event log and no errors in the sql log
> DTS bulk insert happily completes without doing a thing.
> bcp of the same file does work however (from the server ) ..
> Can someone point me in the right direction regarding DLL's etc to be
> checkig please ?
> Thanks Dave|||Sorry Chris.. probably not clear.. I am running the Sql Command Bult Insert
BULK INSERT t_BulkLoad FROM 'Some Filename"
works on every server but one... t
"ChrisR" wrote:

> Did you try:
> -o output_file
> Specifies the name of a file that receives output from bcp redirected from
> the command prompt.
>
>
> "gilkida" <gilkida@.discussions.microsoft.com> wrote in message
> news:894DB758-93BC-432D-8167-F05F83B2467C@.microsoft.com...
>
>|||Woops, sorry.
"gilkida" <gilkida@.discussions.microsoft.com> wrote in message
news:C20BE904-1DDD-4511-ADBD-11C98CB62A98@.microsoft.com...[vbcol=seagreen]
> Sorry Chris.. probably not clear.. I am running the Sql Command Bult
> Insert
> BULK INSERT t_BulkLoad FROM 'Some Filename"
> works on every server but one... t
>
> "ChrisR" wrote:
>|||I have the same issue. I found this posted today.
http://support.microsoft.com/?kbid=896425
It says it's applicable to Standard Edition SP3, I too have Enterprise
Edition SP4 + hotfix.
"ChrisR" wrote:

> Woops, sorry.
>
> "gilkida" <gilkida@.discussions.microsoft.com> wrote in message
> news:C20BE904-1DDD-4511-ADBD-11C98CB62A98@.microsoft.com...
>
>

Bulk Insert Runs Twice!

I have a simple Bulk Insert statement I want to run in a stored procedure.
Here it is:
Set @.bulk_cmd = 'BULK INSERT MyTable
FROM ''C:\mydump.txt''
WITH (FIELDTERMINATOR = ''\t'',
ROWTERMINATOR = '''+CHAR(13)+CHAR(10)+''')'
EXEC(@.bulk_cmd)
It runs fine but does the insert twice. There are 35 records in the file,
you can watch it load 35 records twice. The records are field terminated wit
h
a tab, and row terminated with CR\LF. I have tried numerous other terminatio
n
characters and all does the same. The actual txt file was created using DTS
from a table.
I am using SQL Server 2000.
Any ideas would be appreciated.
MGAre you checking the target table to confirm that it really is inserting the
rows twice, or do you think that's what is happening because you see this in
the messages:
(35 rows affected)
(35 rows affected)
You see it twice because the top message is from the BULK INSERT inside the
sp, and the bottom is the sp reported how many rows where affected. If you
want to only see the message once, put SET NOCOUNT ON at the top of your sp.
"mgcap" wrote:

> I have a simple Bulk Insert statement I want to run in a stored procedure.
> Here it is:
> Set @.bulk_cmd = 'BULK INSERT MyTable
> FROM ''C:\mydump.txt''
> WITH (FIELDTERMINATOR = ''\t'',
> ROWTERMINATOR = '''+CHAR(13)+CHAR(10)+''')'
> EXEC(@.bulk_cmd)
> It runs fine but does the insert twice. There are 35 records in the file,
> you can watch it load 35 records twice. The records are field terminated w
ith
> a tab, and row terminated with CR\LF. I have tried numerous other terminat
ion
> characters and all does the same. The actual txt file was created using DT
S
> from a table.
> I am using SQL Server 2000.
> Any ideas would be appreciated.
> MG|||Mark,
Many thanks. I feel like a goof. That was it. The real issue was that I was
running the routine numerous times, not truncing the table every time, then
seeing more record in the table that 35. That along with the double message
threw me. I should have known better.
Thanks,
Mark
"Mark Williams" wrote:
> Are you checking the target table to confirm that it really is inserting t
he
> rows twice, or do you think that's what is happening because you see this
in
> the messages:
> (35 rows affected)
> (35 rows affected)
> You see it twice because the top message is from the BULK INSERT inside th
e
> sp, and the bottom is the sp reported how many rows where affected. If you
> want to only see the message once, put SET NOCOUNT ON at the top of your s
p.
>
> --
>
> "mgcap" wrote:
>

Bulk Insert question

Hello,
I use the following statement to insert a string of 35 characters into a
table (I am planning to figure out a suitable .FMT file for parsing this int
o
the right column definitions, but that is after I figure out this current
problem described below)
BULK INSERT MyTestTable FROM '<UNCname-FileLocation>\MyTextFile.txt' WITH
(FIELDTERMINATOR = '\0',ROWTERMINATOR = '\n')
MyTestTable is currently defined as
CREATE TABLE MyTestTable (Col1 CHAR(35))
and some sample test data from MyTextFile.txt is as follows
ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789
123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
(length of the data is always 36 bytes and the length of the filename is
always 14 bytes)
(As you can tell, the DDL and data is what I am using for this test)
This BULK INSERT works fine, but what I need to do is to get the name of the
file (MyTextFile.txt in this case) appended to the end of the column.
So, the table definition would change to
CREATE TABLE MyTestTable (Col1 CHAR(49))
I am clueless about how to get the name of the file (which will vary at
runtime) into each row of the MyTestTable that gets affected by this BULK
INSERT.
(I will be inserting multiple files - different file names - to the same
table one after the other and would like to have the filename stored in a
separate column or appended to the column - either way. When I build the .FM
T
file, I will split this into the appropriate columns and will change the
table definition.
Any suggestions would be appreciated. Please let me know if any further
details are needed.
Thanks!you could probably write a TSQL block to read filename before inserting the
data with bulk insert. and then insert data, and update data with appending
the file name to recently inserted records.
hth,
avnrao
http://avnrao.blogspot.com
"Bob" wrote:

> Hello,
> I use the following statement to insert a string of 35 characters into
a
> table (I am planning to figure out a suitable .FMT file for parsing this i
nto
> the right column definitions, but that is after I figure out this current
> problem described below)
> BULK INSERT MyTestTable FROM '<UNCname-FileLocation>\MyTextFile.txt' WITH
> (FIELDTERMINATOR = '\0',ROWTERMINATOR = '\n')
> MyTestTable is currently defined as
> CREATE TABLE MyTestTable (Col1 CHAR(35))
> and some sample test data from MyTextFile.txt is as follows
> ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789
> 123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
> (length of the data is always 36 bytes and the length of the filename is
> always 14 bytes)
> (As you can tell, the DDL and data is what I am using for this test)
> This BULK INSERT works fine, but what I need to do is to get the name of t
he
> file (MyTextFile.txt in this case) appended to the end of the column.
> So, the table definition would change to
> CREATE TABLE MyTestTable (Col1 CHAR(49))
> I am clueless about how to get the name of the file (which will vary at
> runtime) into each row of the MyTestTable that gets affected by this BULK
> INSERT.
> (I will be inserting multiple files - different file names - to the same
> table one after the other and would like to have the filename stored in a
> separate column or appended to the column - either way. When I build the .
FMT
> file, I will split this into the appropriate columns and will change the
> table definition.
> Any suggestions would be appreciated. Please let me know if any further
> details are needed.
> Thanks!
>|||Bob,
I'm currently doing a migration which has between 40 and 50 text files as
the datasource from a mainframe. Their names can change so I use a batch
file to handle this. eg in my batch file,
Call dir with simple header, full filename options to list the files
required into :\temp\filelist.txt
Your file list should be a single column with the complete filepath and
filename
Upload the list to the server using bcp
Note: bcp into a view which has only one column, the filename
If you need more details post back. Basicallly, even if it's a bit
old-fashioned, DOS already has the commands for working with files. There's
always DTS but I'm not a fan ... ; )
Damien
"Bob" wrote:

> Hello,
> I use the following statement to insert a string of 35 characters into
a
> table (I am planning to figure out a suitable .FMT file for parsing this i
nto
> the right column definitions, but that is after I figure out this current
> problem described below)
> BULK INSERT MyTestTable FROM '<UNCname-FileLocation>\MyTextFile.txt' WITH
> (FIELDTERMINATOR = '\0',ROWTERMINATOR = '\n')
> MyTestTable is currently defined as
> CREATE TABLE MyTestTable (Col1 CHAR(35))
> and some sample test data from MyTextFile.txt is as follows
> ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789
> 123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
> (length of the data is always 36 bytes and the length of the filename is
> always 14 bytes)
> (As you can tell, the DDL and data is what I am using for this test)
> This BULK INSERT works fine, but what I need to do is to get the name of t
he
> file (MyTextFile.txt in this case) appended to the end of the column.
> So, the table definition would change to
> CREATE TABLE MyTestTable (Col1 CHAR(49))
> I am clueless about how to get the name of the file (which will vary at
> runtime) into each row of the MyTestTable that gets affected by this BULK
> INSERT.
> (I will be inserting multiple files - different file names - to the same
> table one after the other and would like to have the filename stored in a
> separate column or appended to the column - either way. When I build the .
FMT
> file, I will split this into the appropriate columns and will change the
> table definition.
> Any suggestions would be appreciated. Please let me know if any further
> details are needed.
> Thanks!
>|||avnrao, thank you for your quick response. I had considered this option too,
but the problem is that this load process is supposed to run once a month an
d
with all the 100 files combined, I will have totally about 600 million rows
to process - I guess I was hoping that BULK INSERT would allow us to process
the filename along with it - Any other means of achieving this (instead of
Bulk Insert) would be welcome too.
Thanks!
"avnrao" wrote:
> you could probably write a TSQL block to read filename before inserting th
e
> data with bulk insert. and then insert data, and update data with appendin
g
> the file name to recently inserted records.
> hth,
> avnrao
> http://avnrao.blogspot.com
> "Bob" wrote:
>|||Damien,
I'm ok with using DOS commands too - anything that can get this done
(without too much of a performance hit). :)
I do have the list of filenames available (and I can get it into a SQL table
too), but the problem is how to update the filenames onto the MyTestTable...
I guess I didn't quite understand the suggestion - bcp into the view (and
this view should be for the MyTestTable table?) - but how would I be able to
associate the multiple records from the file with the filename that I am
BCPing?
Sorry if I misunderstood you... but could you clarify a little on this?
Thanks again!
"Damien" wrote:
> Bob,
> I'm currently doing a migration which has between 40 and 50 text files as
> the datasource from a mainframe. Their names can change so I use a batch
> file to handle this. eg in my batch file,
> Call dir with simple header, full filename options to list the files
> required into :\temp\filelist.txt
> Your file list should be a single column with the complete filepath and
> filename
> Upload the list to the server using bcp
> Note: bcp into a view which has only one column, the filename
> If you need more details post back. Basicallly, even if it's a bit
> old-fashioned, DOS already has the commands for working with files. There
's
> always DTS but I'm not a fan ... ; )
>
> Damien
>
>
> "Bob" wrote:
>|||Ah,
well I cheated a little bit here. I used SQL to write the batch file for
me, and I use osql to fire off an ALTER TABLE to set the default for the
column.
So, from Query Analyser, write a query which selects your records, but
create a bcp string. This script will create a meaninful looking batch file
but obviously you can't bcp into temp tables:
DROP TABLE #import_files
CREATE TABLE #import_files ( file_id INT UNIQUE IDENTITY NOT NULL, file_name
VARCHAR(30) NOT NULL )
DROP TABLE #raw_data
CREATE TABLE #raw_data ( record_id INT UNIQUE IDENTITY NOT NULL, file_id INT
NOT NULL, record CHAR(36) )
ALTER TABLE #raw_data ADD CONSTRAINT def_raw_data__file_id DEFAULT -1 FOR
file_id
GO
SET NOCOUNT ON
INSERT INTO #import_files ( file_name ) VALUES ( 'test1.txt' )
INSERT INTO #import_files ( file_name ) VALUES ( 'test2.txt' )
SET NOCOUNT OFF
GO
DROP TABLE #batch_file
CREATE TABLE #batch_file ( file_id INT, sort_id INT, command VARCHAR( 500 )
)
GO
--
SET NOCOUNT ON
-- Section header
INSERT INTO #batch_file
SELECT file_id, 10, 'REM bcp file ' + CAST( file_id AS VARCHAR ) + ' - ' +
file_name
FROM #import_files
-- Drop the default
INSERT INTO #batch_file
SELECT file_id, 20, 'osql -Syourserver -dyourdatabase -Ulogin_id -Ppassword
-q"ALTER TABLE #raw_data DROP CONSTRAINT def_raw_data__file_id'
FROM #import_files
-- Set the default
INSERT INTO #batch_file
SELECT file_id, 30, 'osql -Syourserver -dyourdatabase -Ulogin_id -Ppassword
-q"ALTER TABLE #raw_data ADD CONSTRAINT def_raw_data__file_id DEFAULT ' +
CAST( file_id AS CHAR ) + ' FOR file_id"'
FROM #import_files
-- bcp the file
INSERT INTO #batch_file
SELECT file_id, 40, 'bcp -iyou get the idea.txt ; )'
FROM #import_files
-- Make a gap
INSERT INTO #batch_file
SELECT file_id, 50, ''
FROM #import_files
SELECT command
FROM #batch_file
ORDER BY file_id, sort_id
SET NOCOUNT OFF
Now, save the results as a batch file, remove the dashes from the top and
away you go. I actually use a similar structure in the migration, only it's
a bit more complex, plus it's wrapped in a stored procedure and paramterized
so it's nice and flexible.
If it seems like a lot of hard work, then perhaps this isn't the solution
for you, but it's worked for me!
Let me know how you get on.
Damien|||Bob,
If you are moving to SQL Server 2005, you might consider using
the BULK rowset provider. Existing (in SQL Server 2000) text
providers might also work, but may not be as fast.
DECLARE @.f nvarchar(200)
SET @.f = 'c:\test\values.txt'
INSERT INTO MyTestTable
SELECT colFromTextFile + @.f
SELECT a.* FROM OPENROWSET( BULK 'c:\test\values.txt',
FORMATFILE = 'c:\test\values.fmt') AS a;
Steve Kass
Drew University
Bob wrote:

>Hello,
> I use the following statement to insert a string of 35 characters into
a
>table (I am planning to figure out a suitable .FMT file for parsing this in
to
>the right column definitions, but that is after I figure out this current
>problem described below)
>BULK INSERT MyTestTable FROM '<UNCname-FileLocation>\MyTextFile.txt' WITH
>(FIELDTERMINATOR = '\0',ROWTERMINATOR = '\n')
>MyTestTable is currently defined as
>CREATE TABLE MyTestTable (Col1 CHAR(35))
>and some sample test data from MyTextFile.txt is as follows
>ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789
>123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
>(length of the data is always 36 bytes and the length of the filename is
>always 14 bytes)
>(As you can tell, the DDL and data is what I am using for this test)
>This BULK INSERT works fine, but what I need to do is to get the name of th
e
>file (MyTextFile.txt in this case) appended to the end of the column.
>So, the table definition would change to
>CREATE TABLE MyTestTable (Col1 CHAR(49))
>I am clueless about how to get the name of the file (which will vary at
>runtime) into each row of the MyTestTable that gets affected by this BULK
>INSERT.
>(I will be inserting multiple files - different file names - to the same
>table one after the other and would like to have the filename stored in a
>separate column or appended to the column - either way. When I build the .F
MT
>file, I will split this into the appropriate columns and will change the
>table definition.
>Any suggestions would be appreciated. Please let me know if any further
>details are needed.
>Thanks!
>
>|||Thanks Damien - yes, this would work (now, I just have to get the DBA to
approve of building and dropping the constraint - hopefully, he will be ok
with it).
Thanks again.
"Damien" wrote:

> Ah,
> well I cheated a little bit here. I used SQL to write the batch file for
> me, and I use osql to fire off an ALTER TABLE to set the default for the
> column.
> So, from Query Analyser, write a query which selects your records, but
> create a bcp string. This script will create a meaninful looking batch fi
le
> but obviously you can't bcp into temp tables:
> DROP TABLE #import_files
> CREATE TABLE #import_files ( file_id INT UNIQUE IDENTITY NOT NULL, file_na
me
> VARCHAR(30) NOT NULL )
> DROP TABLE #raw_data
> CREATE TABLE #raw_data ( record_id INT UNIQUE IDENTITY NOT NULL, file_id I
NT
> NOT NULL, record CHAR(36) )
> ALTER TABLE #raw_data ADD CONSTRAINT def_raw_data__file_id DEFAULT -1 FOR
> file_id
> GO
>
> SET NOCOUNT ON
> INSERT INTO #import_files ( file_name ) VALUES ( 'test1.txt' )
> INSERT INTO #import_files ( file_name ) VALUES ( 'test2.txt' )
> SET NOCOUNT OFF
> GO
> DROP TABLE #batch_file
> CREATE TABLE #batch_file ( file_id INT, sort_id INT, command VARCHAR( 500
) )
> GO
> --
> SET NOCOUNT ON
> -- Section header
> INSERT INTO #batch_file
> SELECT file_id, 10, 'REM bcp file ' + CAST( file_id AS VARCHAR ) + ' - ' +
> file_name
> FROM #import_files
>
> -- Drop the default
> INSERT INTO #batch_file
> SELECT file_id, 20, 'osql -Syourserver -dyourdatabase -Ulogin_id -Ppasswor
d
> -q"ALTER TABLE #raw_data DROP CONSTRAINT def_raw_data__file_id'
> FROM #import_files
> -- Set the default
> INSERT INTO #batch_file
> SELECT file_id, 30, 'osql -Syourserver -dyourdatabase -Ulogin_id -Ppasswor
d
> -q"ALTER TABLE #raw_data ADD CONSTRAINT def_raw_data__file_id DEFAULT ' +
> CAST( file_id AS CHAR ) + ' FOR file_id"'
> FROM #import_files
> -- bcp the file
> INSERT INTO #batch_file
> SELECT file_id, 40, 'bcp -iyou get the idea.txt ; )'
> FROM #import_files
> -- Make a gap
> INSERT INTO #batch_file
> SELECT file_id, 50, ''
> FROM #import_files
>
> SELECT command
> FROM #batch_file
> ORDER BY file_id, sort_id
> SET NOCOUNT OFF
> Now, save the results as a batch file, remove the dashes from the top and
> away you go. I actually use a similar structure in the migration, only it
's
> a bit more complex, plus it's wrapped in a stored procedure and paramteriz
ed
> so it's nice and flexible.
> If it seems like a lot of hard work, then perhaps this isn't the solution
> for you, but it's worked for me!
> Let me know how you get on.
> Damien
>
>|||Steve,
Thanks for the update. At this time, the Co is not planning to move to
SQL Server 2005 (this project is expected to go live within a month), so I
guess I am stuck with 2000.
I haven't used text providers yet, so I don't fully understand the code. I
will go thru' BOL and assuming the performance drop isn't too much, I will
try to use this. Currently, I am able to push about 400 million rows into th
e
table (without the filename of course) in about an hour
Thanks again!
"Steve Kass" wrote:

> Bob,
> If you are moving to SQL Server 2005, you might consider using
> the BULK rowset provider. Existing (in SQL Server 2000) text
> providers might also work, but may not be as fast.
> DECLARE @.f nvarchar(200)
> SET @.f = 'c:\test\values.txt'
> INSERT INTO MyTestTable
> SELECT colFromTextFile + @.f
> SELECT a.* FROM OPENROWSET( BULK 'c:\test\values.txt',
> FORMATFILE = 'c:\test\values.fmt') AS a;
> Steve Kass
> Drew University
> Bob wrote:
>
>

Monday, March 19, 2012

bulk insert on remote computer

Hi there!
I am using bulk insert to enter data in tables. I try to bulk insert
data on a remote server but the bulk insert statement must access
files located on localhost computer (from where I query a bulk insert
with query analyser)..and I get a 'access denied' from sqlserver
the only way I found to prevent this, is to log on the remote computer
as the same user that the local computer, to give permissions...
is there any way to do this'
thanks a lot :)
++You need to put the file in some location accessible from the server.
If that location is on the network then make sure you specify the UNC
path name rather than use drive letters.
David Portas
SQL Server MVP
--|||On 26 Apr 2005 07:28:18 -0700, "David Portas"
<REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote:
yes, I use UNC path..but I guess this problem is due to a windows
configuration..I mean I must define the same user on both computers to
allow one to access to other whatever programs are used...
++
Vince

>You need to put the file in some location accessible from the server.
>If that location is on the network then make sure you specify the UNC
>path name rather than use drive letters.|||Just give the access rights to whatever account you use for the SQL
Server service. If you don't know what account that is then check the
Log On properties of MSSQLSERVER in the Services dialog under Control
Panel.
David Portas
SQL Server MVP
--

Thursday, March 8, 2012

Bulk Insert fails to import data files created on Unix

It seems to me that files created on Unix machines with line terminator \n, or chr(10), cannot be imported using the Bulk Insert statement. Is this a bug, or an oversight by Microsoft? Does this mean that unless one replaces all \n with \r\n, there is no way to use Bulk Insert to import Unix files? This is a very strange behavior by MSSQL. Even lessor programs such as Excel and Word automatically recognize chr(10) as a line termination character. Am I missing something, or is this just the way MSSQL is?

You will need to use a format file, in this you can specify the terminator for the last column in a row.

Have a look in BOL. This page shows an example of a file using /r/n which you can obviously change

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/ecfc546d-f708-45f4-878d-fb71b5fd1a0a.htm

|||

Of if you are using the BULK INSERT TSQL statement look at this page

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/be3984e1-5ab3-4226-a539-a9f58e1e01e2.htm

|||I figured out that the problem has to do with MSSQL's native behavior. It turns out that whenever it sees \n, it automatically converts it to \r\n, without notifiying the user. There are at least three ways to work around this strange behavior: 1) replace every \n in your file with \r\n before using Bulk Insert, 2) build your sql statement dynamically either in a stored procedure or VB.net, i.e., use & chr(10) & ,or + chr(10) +, instead of '\n' as the line terminator in your statement, and 3) load your file into a Datatable via ADO.net, and then insert the entire Datatable into MSSQL.|||

Note sure what figuring out was required, as BOL has an example that just works.

|||

I have never succeeded to convince BULK INSERT to read Unix

files, and I find one of these solutions usually works:

1. See if the process that moved the files from the Unix

machine can correct the line ends (for example, ftp can do

this)

2. Run the Unix utility unix2dos on the files before they

leave the Unix machine, or afterwards, under the Cygwin

Unix shell for Windows. (Or write a tiny command-line

Windows program to do this.)

Steve Kass

Drew University

ktto@.discussions.microsoft.com wrote:

> I figured out that the problem has to do with MSSQL's native behavior.

> It turns out that whenever it sees \n, it automatically converts it to

> \r\n, without notifiying the user. There are at least three ways to work

> around this strange behavior: 1) replace every \n in your file with \r\n

> before using Bulk Insert, 2) build your sql statement dynamically either

> in a stored procedure or VB.net, i.e., use & chr(10) & ,or + chr(10) +,

> instead of '\n' as the line terminator in your statement, and 3) load

> your file into a Datatable via ADO.net, and then insert the entire

> Datatable into MSSQL.

>

Bulk Insert fails to import data files created on Unix

It seems to me that files created on Unix machines with line terminator \n, or chr(10), cannot be imported using the Bulk Insert statement. Is this a bug, or an oversight by Microsoft? Does this mean that unless one replaces all \n with \r\n, there is no way to use Bulk Insert to import Unix files? This is a very strange behavior by MSSQL. Even lessor programs such as Excel and Word automatically recognize chr(10) as a line termination character. Am I missing something, or is this just the way MSSQL is?

You will need to use a format file, in this you can specify the terminator for the last column in a row.

Have a look in BOL. This page shows an example of a file using /r/n which you can obviously change

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/ecfc546d-f708-45f4-878d-fb71b5fd1a0a.htm

|||

Of if you are using the BULK INSERT TSQL statement look at this page

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/be3984e1-5ab3-4226-a539-a9f58e1e01e2.htm

|||I figured out that the problem has to do with MSSQL's native behavior. It turns out that whenever it sees \n, it automatically converts it to \r\n, without notifiying the user. There are at least three ways to work around this strange behavior: 1) replace every \n in your file with \r\n before using Bulk Insert, 2) build your sql statement dynamically either in a stored procedure or VB.net, i.e., use & chr(10) & ,or + chr(10) +, instead of '\n' as the line terminator in your statement, and 3) load your file into a Datatable via ADO.net, and then insert the entire Datatable into MSSQL.|||

Note sure what figuring out was required, as BOL has an example that just works.

|||

I have never succeeded to convince BULK INSERT to read Unix

files, and I find one of these solutions usually works:

1. See if the process that moved the files from the Unix

machine can correct the line ends (for example, ftp can do

this)

2. Run the Unix utility unix2dos on the files before they

leave the Unix machine, or afterwards, under the Cygwin

Unix shell for Windows. (Or write a tiny command-line

Windows program to do this.)

Steve Kass

Drew University

ktto@.discussions.microsoft.com wrote:

> I figured out that the problem has to do with MSSQL's native behavior.

> It turns out that whenever it sees \n, it automatically converts it to

> \r\n, without notifiying the user. There are at least three ways to work

> around this strange behavior: 1) replace every \n in your file with \r\n

> before using Bulk Insert, 2) build your sql statement dynamically either

> in a stored procedure or VB.net, i.e., use & chr(10) & ,or + chr(10) +,

> instead of '\n' as the line terminator in your statement, and 3) load

> your file into a Datatable via ADO.net, and then insert the entire

> Datatable into MSSQL.

>

'Bulk insert' error message

Dear All
I am carrying out a bulk insert from a csv file into a
SQL Server table using the 'Bulk insert' statement within
a stored procedure. This operation works okay whenever I
manually run this sp on the server. However it generates
the error message shown below whenever I call this sp
from a VB6 form. The db I am using is a SQL7 db which I
have restored on a SQL2000 db XP Prof machine, thereby
making it a SQL2000 db.
Thanks for your help.
Olu
-- start error message --
'A different operation is preventing this operation from
being executed'.
-- end error message --Olu,
Silly question, but what data access method are you employing in your VB6
app?
James Hokes
"Olu Falowo" <ofalowo@.hotmail.com> wrote in message
news:01a901c3ce3b$4af10e50$a301280a@.phx.gbl...
> Dear All
> I am carrying out a bulk insert from a csv file into a
> SQL Server table using the 'Bulk insert' statement within
> a stored procedure. This operation works okay whenever I
> manually run this sp on the server. However it generates
> the error message shown below whenever I call this sp
> from a VB6 form. The db I am using is a SQL7 db which I
> have restored on a SQL2000 db XP Prof machine, thereby
> making it a SQL2000 db.
> Thanks for your help.
> Olu
> -- start error message --
> 'A different operation is preventing this operation from
> being executed'.
> -- end error message --|||James,
The code I am refering to is inherited (i.e. written by
someone else). It refers to a DSN, which I have just
discovered, is wrongly configured. I have re-configured
this DSN and the system works okay. I would have expected
the error message to have narrowed down this problem!!
Anyway thanks very much for your time.
Olu
>--Original Message--
>Olu,
>Silly question, but what data access method are you
employing in your VB6
>app?
>James Hokes
>"Olu Falowo" <ofalowo@.hotmail.com> wrote in message
>news:01a901c3ce3b$4af10e50$a301280a@.phx.gbl...
>> Dear All
>> I am carrying out a bulk insert from a csv file into a
>> SQL Server table using the 'Bulk insert' statement
within
>> a stored procedure. This operation works okay whenever
I
>> manually run this sp on the server. However it
generates
>> the error message shown below whenever I call this sp
>> from a VB6 form. The db I am using is a SQL7 db which I
>> have restored on a SQL2000 db XP Prof machine, thereby
>> making it a SQL2000 db.
>> Thanks for your help.
>> Olu
>> -- start error message --
>> 'A different operation is preventing this operation
from
>> being executed'.
>> -- end error message --
>
>.
>

Bulk Insert Error

When I run a script, it gives me the following error
message: "You do not have permission to use the BULK
INSERT statement". We are using SQL Server 2000.
Does it related to Recovery Model used ?
ThanksYou need to be in sysadmin to run a bulk insert.
I think it should also work as bulkadmin but has problems.
"Peter" wrote:
> When I run a script, it gives me the following error
> message: "You do not have permission to use the BULK
> INSERT statement". We are using SQL Server 2000.
> Does it related to Recovery Model used ?
> Thanks
>|||Hi,
The user should have either "SYSADMIN" or "BULKADMIN" server fixed role
assigned.
How to assign the role:-
sp_addsrvrolemember <login_name>,'bulkadmin'
--
Thanks
Hari
MCDBA
"Peter" <anonymous@.discussions.microsoft.com> wrote in message
news:20ff201c459b1$c0ed7f10$a001280a@.phx.gbl...
> When I run a script, it gives me the following error
> message: "You do not have permission to use the BULK
> INSERT statement". We are using SQL Server 2000.
> Does it related to Recovery Model used ?
> Thanks
>|||Does it mean that we can still preform Bulk Insert even
though the recovery model is "Simple" ?
Thanks
>--Original Message--
>You need to be in sysadmin to run a bulk insert.
>I think it should also work as bulkadmin but has problems.
>"Peter" wrote:
>> When I run a script, it gives me the following error
>> message: "You do not have permission to use the BULK
>> INSERT statement". We are using SQL Server 2000.
>> Does it related to Recovery Model used ?
>> Thanks
>>
>.
>|||Hi,
BULK INSERT is possible in all recovery models.
Only thing is in BULK_LOGGED and SIMPLE recovery model the loading will not
be logged.
--
Thanks
Hari
MCDBA
"Peter" <anonymous@.discussions.microsoft.com> wrote in message
news:20ba701c459b4$a896b190$a101280a@.phx.gbl...
> Does it mean that we can still preform Bulk Insert even
> though the recovery model is "Simple" ?
> Thanks
> >--Original Message--
> >You need to be in sysadmin to run a bulk insert.
> >I think it should also work as bulkadmin but has problems.
> >
> >"Peter" wrote:
> >
> >> When I run a script, it gives me the following error
> >> message: "You do not have permission to use the BULK
> >> INSERT statement". We are using SQL Server 2000.
> >>
> >> Does it related to Recovery Model used ?
> >>
> >> Thanks
> >>
> >>
> >.
> >

Bulk Insert Error

When I run a script, it gives me the following error
message: "You do not have permission to use the BULK
INSERT statement". We are using SQL Server 2000.
Does it related to Recovery Model used ?
ThanksYou need to be in sysadmin to run a bulk insert.
I think it should also work as bulkadmin but has problems.
"Peter" wrote:

> When I run a script, it gives me the following error
> message: "You do not have permission to use the BULK
> INSERT statement". We are using SQL Server 2000.
> Does it related to Recovery Model used ?
> Thanks
>|||Hi,
The user should have either "SYSADMIN" or "BULKADMIN" server fixed role
assigned.
How to assign the role:-
sp_addsrvrolemember <login_name>,'bulkadmin'
Thanks
Hari
MCDBA
"Peter" <anonymous@.discussions.microsoft.com> wrote in message
news:20ff201c459b1$c0ed7f10$a001280a@.phx
.gbl...
> When I run a script, it gives me the following error
> message: "You do not have permission to use the BULK
> INSERT statement". We are using SQL Server 2000.
> Does it related to Recovery Model used ?
> Thanks
>|||Does it mean that we can still preform Bulk Insert even
though the recovery model is "Simple" ?
Thanks

>--Original Message--
>You need to be in sysadmin to run a bulk insert.
>I think it should also work as bulkadmin but has problems.
>"Peter" wrote:
>
>.
>|||Hi,
BULK INSERT is possible in all recovery models.
Only thing is in BULK_LOGGED and SIMPLE recovery model the loading will not
be logged.
Thanks
Hari
MCDBA
"Peter" <anonymous@.discussions.microsoft.com> wrote in message
news:20ba701c459b4$a896b190$a101280a@.phx
.gbl...[vbcol=seagreen]
> Does it mean that we can still preform Bulk Insert even
> though the recovery model is "Simple" ?
> Thanks
>

Bulk Insert Error

When I run a script, it gives me the following error
message: "You do not have permission to use the BULK
INSERT statement". We are using SQL Server 2000.
Does it related to Recovery Model used ?
Thanks
You need to be in sysadmin to run a bulk insert.
I think it should also work as bulkadmin but has problems.
"Peter" wrote:

> When I run a script, it gives me the following error
> message: "You do not have permission to use the BULK
> INSERT statement". We are using SQL Server 2000.
> Does it related to Recovery Model used ?
> Thanks
>
|||Hi,
The user should have either "SYSADMIN" or "BULKADMIN" server fixed role
assigned.
How to assign the role:-
sp_addsrvrolemember <login_name>,'bulkadmin'
Thanks
Hari
MCDBA
"Peter" <anonymous@.discussions.microsoft.com> wrote in message
news:20ff201c459b1$c0ed7f10$a001280a@.phx.gbl...
> When I run a script, it gives me the following error
> message: "You do not have permission to use the BULK
> INSERT statement". We are using SQL Server 2000.
> Does it related to Recovery Model used ?
> Thanks
>
|||Does it mean that we can still preform Bulk Insert even
though the recovery model is "Simple" ?
Thanks

>--Original Message--
>You need to be in sysadmin to run a bulk insert.
>I think it should also work as bulkadmin but has problems.
>"Peter" wrote:
>.
>
|||Hi,
BULK INSERT is possible in all recovery models.
Only thing is in BULK_LOGGED and SIMPLE recovery model the loading will not
be logged.
Thanks
Hari
MCDBA
"Peter" <anonymous@.discussions.microsoft.com> wrote in message
news:20ba701c459b4$a896b190$a101280a@.phx.gbl...[vbcol=seagreen]
> Does it mean that we can still preform Bulk Insert even
> though the recovery model is "Simple" ?
> Thanks

Saturday, February 25, 2012

Bulk Insert (type mismatch) on datetime field containing NULL

Can anyone help please, I am using bulk insert for the first time.
The statement I am running is:
BULK INSERT Titles
FROM 'c:\Titles.txt'
WITH (FIRSTROW = 3,
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n',
KEEPNULLS,
FORMATFILE = 'c:\Titles.fmt')
Titles.txt contains tab delimited data like:
ID Description StartDate ExpiryDate ParentItemID
-- --
-- -- --
440 Doctor 1 Jan 1997 0:00 NULL NULL
441 Mr 1 Jan 1990 0:00 NULL 1
If I run the bulk insert statement I get the message:
Server: Msg 4864, Level 16, State 1, Line 1
Bulk insert data conversion error (type mismatch) for row 3, column 4
(ExpiryDate)
In the file Titles.txt, if I find and replace NULL with nothing and
then execute the statement the data inserts into the Titles table.
I need to be able to insert without having to do find and replace as I
have hundreds of files to bulk insert.
The format file Titles.fmt looks like this:
8.0
5
1 SQLCHAR 0 12 "\t" 1
ID ""
2 SQLCHAR 0 100 "\t" 2
Description Latin1_General_CI_AS
3 SQLCHAR 0 24 "\t" 3
StartDate ""
4 SQLCHAR 0 24 "\t" 4
ExpiryDate ""
5 SQLCHAR 0 12 "\t" 5
ParentItemID ""If this is not a one time deal, you'd be better off making sure that when
these files are generated, the value for column ExpireDate that is null does
not contain a string NULL.
With the existing data files, personally, I'd write a little utility to
find/replace all the 'NULL' string in the ExpireDate column with an empty
string. This can be esily done with any tool that supports regular
expressions.
Linchi
"rai_sk@.hotmail.com" wrote:
> Can anyone help please, I am using bulk insert for the first time.
> The statement I am running is:
> BULK INSERT Titles
> FROM 'c:\Titles.txt'
> WITH (FIRSTROW = 3,
> FIELDTERMINATOR = '\t',
> ROWTERMINATOR = '\n',
> KEEPNULLS,
> FORMATFILE = 'c:\Titles.fmt')
> Titles.txt contains tab delimited data like:
> ID Description StartDate ExpiryDate ParentItemID
> -- --
> -- -- --
> 440 Doctor 1 Jan 1997 0:00 NULL NULL
> 441 Mr 1 Jan 1990 0:00 NULL 1
> If I run the bulk insert statement I get the message:
> Server: Msg 4864, Level 16, State 1, Line 1
> Bulk insert data conversion error (type mismatch) for row 3, column 4
> (ExpiryDate)
> In the file Titles.txt, if I find and replace NULL with nothing and
> then execute the statement the data inserts into the Titles table.
> I need to be able to insert without having to do find and replace as I
> have hundreds of files to bulk insert.
> The format file Titles.fmt looks like this:
> 8.0
> 5
> 1 SQLCHAR 0 12 "\t" 1
> ID ""
> 2 SQLCHAR 0 100 "\t" 2
> Description Latin1_General_CI_AS
> 3 SQLCHAR 0 24 "\t" 3
> StartDate ""
> 4 SQLCHAR 0 24 "\t" 4
> ExpiryDate ""
> 5 SQLCHAR 0 12 "\t" 5
> ParentItemID ""
>

bulk insert

I'm trying run a bulk insert statement to insert data into an existing table:

Here is an example of the text file data:

"BEGIN_APP_YR","2001"
"BISP_EXPD_THRU","200512"
"BISP_ITER","PRELIM"
"BISP_LAST_PUB_DT","02/14/200612:41PM"
"BISP_YR","2007"
"BISP_YRS","3"
"END_APP_YR","2006"

This is the bulk insert statement I'm using:

BULK INSERT AFR.dbo.[BISM_CONFIG]
FROM 'c:\sql\default\bism_config.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

I'm getting a syntax error near ')'

Any help is appreciated.
Thanks,
-D-hi, you run the bulk insert in the Query Analyzer?
what is the complete error?
the syntax of the statement is correct.
test change the ',' for ';' or '|' (sure you need change the txt)

abel

Friday, February 24, 2012

Bulk Insert

Hi GUYS

My name is Jackson. I have been strugling to get the BULK INSERT statement import data from a text file to the database. Have a look at this piece of coding:

DECLARE @.SQL VARCHAR(1000)

set @.SQL =
'BULK INSERT dbo.#temp
FROM ''C:\Documents and Settings\mmelestj\Desktop\Dekany.txt''
WITH (FIELDTERMINATOR = '','', ROWTERMINATOR
= ''\n'')'

EXEC (@.SQL)

select id Site_Number ,fld2 Site_Name from #temp

The thing is I get the following error message:

Server: Msg 4861, Level 16, State 1, Line 1
Could not bulk insert because file 'C:\Documents and Settings\mmelestj\Desktop\Dekany.txt' could not be opened. Operating system error code 3(The system cannot find the path specified.).My guess is it doesn't like spaces in your statement [Documents and Settings].

I don't think you would be able to select from #temp because bulk insert is a separate process and #temp is available within one process only.