Showing posts with label columns. Show all posts
Showing posts with label columns. Show all posts

Thursday, March 29, 2012

Bulk Updates taking a long time

We have a huge table with around 25 Million records. We want to reset two int Columns of all records to 0. Currently its taking around 1.5 hours... What are the best practises we can follow to reduce the total update time.

Initially we used - Update <TableName> set <Column1>=0, <Column2>=0.
Later we modified the query to include a WHERE clause and did the update in batch mode such as

DECLARE @.maxCount Int
DECLARE @.iCount Int
SELECT @.MaxCount = Max(ID) FROM organizationsource
SET @.iCount = 0
WHILE (@.iCount<@.MaxCount)
BEGIN
UPDATE <tableName> set <Column1> = 0, <Column2>=0
WHERE ID between @.iCount and @.iCount+1000000
SET @.iCount = @.iCount + 1000000
END

Can you please suggest some tips to improve the update performance.
Can we do something at the SQL Server level / are there any settings at the database level for performing faster updates.

Thanks,
Loonysan

Hard to determine based on the information you've provided so far:

Is the ID field the primary index?|||

Thanks for your interest.

To Answer your Questions

1) Yes - ID field is the primary key in my table.
2) This table will not be accessed by other applications during the update process.
3) Yeah - I have the Data and Log files in different drives. (Should I keep them in different disks for better performance)
4) We are using SQL Server 2005 :)
5) The code is resides in a Stored Procedure

Thanks,
Loonysan

|||

Ok.

Are the columns you are updating indexed also?

This can slow down updates. If so, drop the index and recreate after the update has happened.

Also - make sure the following is turned off to improve performance:

auto create statistics

Bulk repopulation of a table

Hi, I have a table T with 3 int columns, I need to update it every X
hours with a query that returns the same 3 int columns. Currently I'm
doing it using a cursor iterating over each row and checking whether I
need to DELETE/UPDATE/INSERT it, this however isn't very efficient.
So I tested 2 additional methods, one involving deleting all the rows
from the target table and then inserting everything and the other by
dropping the table, recreating and inseting.
The results are not very surprising:
PROC,CPU,READS,WRITES,DURATION
DROP,2720,177757,218,4000
DELE,3150,183060,230,5300
CURS,8200,504818,247,8240
Which indicates that DROPing and reCREATing is the way to go, any
suggestions?
I don't like the DROP/CREATE scheme since it might involve the TABLEs
downtime (even when I CREATE a T1 then DROP T and sp_rename T1->T). If
there is no better way, will there be problems due to sp_rename
changing the name but not the ID of an object?
P.S. it's SQL2005, and the query itself executes in (2300,11596,0,2730)
[same column names as above].
Thanks.Can you give some more information so we can provide a more realistic
answer? The DDL for the existing table would help along with how or where
you are getting the data to update the table with. If the new data is a
complete up to date set then you have several options. I am assuming it is a
flat file but since you didn't provide that information it is only a guess.
I would look at using BULK INSERT to load the new data into a table that you
create. Lets call it X. Then you can prep this table to get it exactly like
you want it to be, drop the original table and rename X to what the old one
was. You may find that renaming the old one first, renaming X and then
dropping might yield the least time that the original table is off-line to
the users. But either way it should be a matter of a second or less.
Andrew J. Kelly SQL MVP
<johnsolver@.gmail.com> wrote in message
news:1137942204.960433.107600@.g49g2000cwa.googlegroups.com...
> Hi, I have a table T with 3 int columns, I need to update it every X
> hours with a query that returns the same 3 int columns. Currently I'm
> doing it using a cursor iterating over each row and checking whether I
> need to DELETE/UPDATE/INSERT it, this however isn't very efficient.
> So I tested 2 additional methods, one involving deleting all the rows
> from the target table and then inserting everything and the other by
> dropping the table, recreating and inseting.
> The results are not very surprising:
> PROC,CPU,READS,WRITES,DURATION
> DROP,2720,177757,218,4000
> DELE,3150,183060,230,5300
> CURS,8200,504818,247,8240
> Which indicates that DROPing and reCREATing is the way to go, any
> suggestions?
> I don't like the DROP/CREATE scheme since it might involve the TABLEs
> downtime (even when I CREATE a T1 then DROP T and sp_rename T1->T). If
> there is no better way, will there be problems due to sp_rename
> changing the name but not the ID of an object?
> P.S. it's SQL2005, and the query itself executes in (2300,11596,0,2730)
> [same column names as above].
> Thanks.
>|||Instead of deleting all rows from the table you can truncate it. Its not
a good idea to drop and recreate table.
Or
you can first store the data in one temporary and then update related
data from temporary table in permanent table using TSQL.
Please post data and how you want to update it.
Regards
Amish Shah
*** Sent via Developersdex http://www.examnotes.net ***|||Hi, sorry for not providing enough info:
the table:
CREATE TABLE [dbo].[T](
[tid] [int] NOT NULL,
[pid] [int] NOT NULL DEFAULT (0),
[r] [int] NOT NULL,
CONSTRAINT [PK_T] PRIMARY KEY CLUSTERED
(
[t] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY];
The query returns the exact 3 columns, simplistically: SELECT tid,pid,r
FROM A (it returns ~90k rows) I don't think that the exact query
matters... I'll be happy to provide additional info if needed.
Thanks.|||As to updating data, assuming T contains the following:
tid,pid,r
1,3,6
2,3,40
5,4,60
and the query (Q) returns
tid,pid,r
1,3,50
8,1,1000
Then I would like T to contain exactly what Q returned, namely:
DELETE FROM T WHERE tid=2
DELETE FROM T WHERE tid=5
UPDATE T SET r=50 WHERE tid=1
INSERT INTO T (tid,pid,r) VALUES(8,1,1000)
etc.|||If you are updating the value for r in table T, based on a match of both tid
and pid from the source table, then
--create a temporary table that has the data to be imported.
SELECT tid, pid, r INTO #importtable FROM (...the rest of your import query
...)
--Delete records from T that aren't in the new import batch
DELETE FROM T WHERE NOT EXISTS
(SELECT i.tid FROM #importtable i WHERE i.tid = T.tid)
--update the records that have matching tid and pid values
UPDATE T SET r = i.r FROM #importtable i
INNER JOIN T ON T.tid=i.tid AND T.pid = i.pid
--INSERT new records into T that didn't exist before
INSERT INTO T
SELECT tid,pid,r FROM #importtable i
WHERE NOT EXISTS
(SELECT tid FROM T WHERE T.tid = i.tid)
This gets more complicated if the PK on T is composite; I couldn't really
tell from the DDL you posted earlier.
Of course, it may be faster to just truncate the table and import in the new
data, as suggested earlier by Amish.
Another suggestion is to encapsulate the above into an INSTEAD OF trigger,
and then you could simply run
INSERT INTO T SELECT ...rest of your import query here ...
The INSTEAD OF trigger would fire before any primary key constraints would
be checked, so it could acheive what you want.
"johnsolver@.gmail.com" wrote:

> As to updating data, assuming T contains the following:
> tid,pid,r
> 1,3,6
> 2,3,40
> 5,4,60
> and the query (Q) returns
> tid,pid,r
> 1,3,50
> 8,1,1000
> Then I would like T to contain exactly what Q returned, namely:
> DELETE FROM T WHERE tid=2
> DELETE FROM T WHERE tid=5
> UPDATE T SET r=50 WHERE tid=1
> INSERT INTO T (tid,pid,r) VALUES(8,1,1000)
> etc.
>|||Thanks for the reply Mark,
I've tried your suggestion, performance-wise it's better than the
CURSOR solution (not surprising) but it's not as good as DROP or
TRUNCATE approaches. The key isn't a composite it's only one column as
you guessed, tid.
Amish: Why isn't it a good idea to DROP/CREATE the table? (except for
the minimal downtime due to sp_rename)?
Thanks.|||Johnsol
I also suggested you
you can first store the data in one temporary and then update related
data from temporary table in permanent table using TSQL.
But I was unable to give solution untill go post DDL.
Second
when you change the table name causes the sp, view and references
invalid which uses this table until you recreate new table with old
name.
If you drop and recreate table you have to recreate all relations ,
indexes again. You can not guarantee referential integrity of data
of othe tables if this table is part of any foregin key relationship
with them, since this table drops many times.
In some cases we have seen that updating data in the table was not
possible using TSQL easily and you have to create large number of temp
tables and check number of conditions and check data from number of
tables.
So I have gave you all the options but untill I get the data I was not
able to give you some solution.
Regards
Amish Shah|||Thanks for the reply Amish,
in the end I'll probably go with CREATE and DROP simply because it's
performance is about 1.5 times faster than the temp table solution, I
don't have any foreign keys/views built on the table.
So I've opted for the following (DDL follows), btw. should I wrap the
whole sequence in BEGIN TRANS ... COMIT TRANS?
CREATE TABLE [dbo].[T_1](
[tid] [int] NOT NULL,
[pid] [int] NOT NULL DEFAULT ((0)),
[r] [int] NOT NULL,
CONSTRAINT [PK_T_1] PRIMARY KEY CLUSTERED
(
[tid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO T_1 SELECT tid,pid,r FROM A;
DROP TABLE T;
EXEC sp_rename 'T_1','T';
EXEC sp_rename 'T.PK_T_1','PK_T','INDEX';|||Yes at least you should do all in transaction.
Regards
Amish Shah

Tuesday, March 27, 2012

Bulk Load Identity Propagation

Hello,
I'm working with the SQLXML3.0 Bulk Loader SP3 & I am having a problem
propagating identity columns. I can get the identity to propagate to one
child, but the grandchild ends up getting 0s in the column that I expect the
identity value to be in.
Has anyone else had this problem, or can any of you see what I'm doing
wrong?
TIA,
Aelk
Here is a snippet from my xsd:
<xs:annotation>
<xs:appinfo>
<sql:relationship name="Batch-Test"
parent="XMLDM_Batch"
parent-key="BatchID"
child="XMLDM_Test"
child-key="BatchID" />
</xs:appinfo>
</xs:annotation>
<xs:annotation>
<xs:appinfo>
<sql:relationship name="Test-Child2"
parent="XMLDM_Test"
parent-key="BatchID ID"
child="XMLDM_TestChild2"
child-key="BatchID Child2ID" />
</xs:appinfo>
</xs:annotation>
<xs:annotation>
<xs:appinfo>
<sql:relationship name="Child2-GrandChild"
parent="XMLDM_TestChild2"
parent-key="BatchID Child2ID"
child="XMLDM_TestGrandChild"
child-key="BatchID Child2ID" />
</xs:appinfo>
</xs:annotation>
<xs:element name="TEST" sql:relation="XMLDM_Test"
sql:relationship="Batch-Test" >
<xs:complexType>
<xs:sequence>
<xs:element name="CHILD2" minOccurs="0" maxOccurs="unbounded"
sql:relation="XMLDM_TestChild2" sql:relationship="Test-Child2">
<xs:complexType>
<xs:sequence>
<xs:element name="GRANDCHILD" minOccurs="0"
maxOccurs="unbounded" sql:relation="XMLDM_TestGrandChild"
sql:relationship="Child2-GrandChild" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
And here is a sample of my XML that I am trying to load:
<TEST>
<CHILD2>
<GRANDCHILD>my grandchild</GRANDCHILD>
</CHILD2>
</TEST>
I'm getting the following in my tables:
XMLDM_Test:
BatchID ID
30 6
XMLDM_TestChild2:
BatchID Child2ID
30 6
XMLDM_TestGrandChild:
BatchID Child2ID Grandchild
30 0 my grandchild
This looks like a limitation in SqlXml3 Sp3.
Bertan ARI
This posting is provided "AS IS" with no warranties, and confers no rights.
"aelk" <aelk@.discussions.microsoft.com> wrote in message
news:D9B0719F-89DF-437A-A06F-43D6257817F1@.microsoft.com...
> Hello,
> I'm working with the SQLXML3.0 Bulk Loader SP3 & I am having a problem
> propagating identity columns. I can get the identity to propagate to one
> child, but the grandchild ends up getting 0s in the column that I expect
> the
> identity value to be in.
> Has anyone else had this problem, or can any of you see what I'm doing
> wrong?
> TIA,
> Aelk
> Here is a snippet from my xsd:
> <xs:annotation>
> <xs:appinfo>
> <sql:relationship name="Batch-Test"
> parent="XMLDM_Batch"
> parent-key="BatchID"
> child="XMLDM_Test"
> child-key="BatchID" />
> </xs:appinfo>
> </xs:annotation>
> <xs:annotation>
> <xs:appinfo>
> <sql:relationship name="Test-Child2"
> parent="XMLDM_Test"
> parent-key="BatchID ID"
> child="XMLDM_TestChild2"
> child-key="BatchID Child2ID" />
> </xs:appinfo>
> </xs:annotation>
> <xs:annotation>
> <xs:appinfo>
> <sql:relationship name="Child2-GrandChild"
> parent="XMLDM_TestChild2"
> parent-key="BatchID Child2ID"
> child="XMLDM_TestGrandChild"
> child-key="BatchID Child2ID" />
> </xs:appinfo>
> </xs:annotation>
> <xs:element name="TEST" sql:relation="XMLDM_Test"
> sql:relationship="Batch-Test" >
> <xs:complexType>
> <xs:sequence>
> <xs:element name="CHILD2" minOccurs="0" maxOccurs="unbounded"
> sql:relation="XMLDM_TestChild2" sql:relationship="Test-Child2">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="GRANDCHILD" minOccurs="0"
> maxOccurs="unbounded" sql:relation="XMLDM_TestGrandChild"
> sql:relationship="Child2-GrandChild" />
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
>
> And here is a sample of my XML that I am trying to load:
> <TEST>
> <CHILD2>
> <GRANDCHILD>my grandchild</GRANDCHILD>
> </CHILD2>
> </TEST>
> I'm getting the following in my tables:
> XMLDM_Test:
> BatchID ID
> 30 6
> XMLDM_TestChild2:
> BatchID Child2ID
> 30 6
> XMLDM_TestGrandChild:
> BatchID Child2ID Grandchild
> 30 0 my grandchild

Bulk inserting into table with computed columns

Using SS2K, I'm getting the following error while bulk inserting:

Column 'warranty_expiration_date' cannot be modified because it is a
computed column.

Here is my bulk insert statement:

BULK INSERT dbo.TestData
FROM 'TestData.dat'
WITH (CHECK_CONSTRAINTS,
FIELDTERMINATOR='|',
MAXERRORS = 1,
FORMATFILE='TestData.fmt')

The computed column is not referenced in the format file and the data file
does not contain the computed data.

Thankstperovic (tperovic@.compumation.com) writes:
> Using SS2K, I'm getting the following error while bulk inserting:
> Column 'warranty_expiration_date' cannot be modified because it is a
> computed column.
> Here is my bulk insert statement:
> BULK INSERT dbo.TestData
> FROM 'TestData.dat'
> WITH (CHECK_CONSTRAINTS,
> FIELDTERMINATOR='|',
> MAXERRORS = 1,
> FORMATFILE='TestData.fmt')
> The computed column is not referenced in the format file and the data file
> does not contain the computed data.

Could you provide more information, for instance a CREATE TABLE statement,
a sample data file and a sample format file that demonstrates the problem.

To wit, I created this table:

create table c (a int NOT NULL,
b as sqrt(a))

And I created this format file:

8.0
1
1 SQLCHAR 0 0 "\r\n" 1 dda_num ""

And used this data file:

12
4144
356

And this command:

bulk insert c FROM 'E:\temp\slask.bcp'
WITH (CHECK_CONSTRAINTS,
FIELDTERMINATOR='|',
MAXERRORS = 1,
FORMATFILE='E:\temp\slask.fmt')

And my load was successful.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Facing a deadline, I dropped the computed column and added it to a view.
Maybe later we can revisit this issue. Thanks.

"Erland Sommarskog" <sommar@.algonet.se> wrote in message
news:Xns948A483591F3Yazorman@.127.0.0.1...
> tperovic (tperovic@.compumation.com) writes:
> > Using SS2K, I'm getting the following error while bulk inserting:
> > Column 'warranty_expiration_date' cannot be modified because it is a
> > computed column.
> > Here is my bulk insert statement:
> > BULK INSERT dbo.TestData
> > FROM 'TestData.dat'
> > WITH (CHECK_CONSTRAINTS,
> > FIELDTERMINATOR='|',
> > MAXERRORS = 1,
> > FORMATFILE='TestData.fmt')
> > The computed column is not referenced in the format file and the data
file
> > does not contain the computed data.
> Could you provide more information, for instance a CREATE TABLE statement,
> a sample data file and a sample format file that demonstrates the problem.
> To wit, I created this table:
> create table c (a int NOT NULL,
> b as sqrt(a))
> And I created this format file:
> 8.0
> 1
> 1 SQLCHAR 0 0 "\r\n" 1 dda_num ""
> And used this data file:
> 12
> 4144
> 356
> And this command:
> bulk insert c FROM 'E:\temp\slask.bcp'
> WITH (CHECK_CONSTRAINTS,
> FIELDTERMINATOR='|',
> MAXERRORS = 1,
> FORMATFILE='E:\temp\slask.fmt')
> And my load was successful.
> --
> Erland Sommarskog, SQL Server MVP, sommar@.algonet.se
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techin.../2000/books.asp

Sunday, March 25, 2012

Bulk Insert With Identity Field

Hi All,
Can I Bulk Insert to to SQL Table with a Identity Column in it?
My Source is a text file with 17 Columns and My Target is a SQL Server table
with 18 Columns (all 17 column of the source + 1 Identity Column as Primary
KEY).
So In this Situation How can i Bulk Insert to the SQL Table from the text
file. Please give small example if possible.
Also If my SQL table is have 2 More Extra Column Can I Boul Insert from the
above source?
Ex: Total 19 Columns ( all 17 columns of the source text file + 1 Identity
Column + 1 Extra column). If I want to Insert into the 17 columns and I want
the Indetity column to generate auto numbers and the Last Extra Column to be
Filled with Some "Char (1)" Value. Is that Possible?
Thanks for any Help or suggestions
Prabhat
using bulk insert there is a keepidentity parameter, using bcp it is -E...
Both are documented in books on line
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
"Prabhat" <not_a_mail@.hotmail.com> wrote in message
news:OKa8j8unEHA.3868@.TK2MSFTNGP11.phx.gbl...
> Hi All,
> Can I Bulk Insert to to SQL Table with a Identity Column in it?
> My Source is a text file with 17 Columns and My Target is a SQL Server
table
> with 18 Columns (all 17 column of the source + 1 Identity Column as
Primary
> KEY).
> So In this Situation How can i Bulk Insert to the SQL Table from the text
> file. Please give small example if possible.
> Also If my SQL table is have 2 More Extra Column Can I Boul Insert from
the
> above source?
> Ex: Total 19 Columns ( all 17 columns of the source text file + 1 Identity
> Column + 1 Extra column). If I want to Insert into the 17 columns and I
want
> the Indetity column to generate auto numbers and the Last Extra Column to
be
> Filled with Some "Char (1)" Value. Is that Possible?
> Thanks for any Help or suggestions
> Prabhat
>
|||Thanks for the Hint. I have seen that in BOL but did not get any Example.
Can you suggest any site or give me a small Example where the Target table
has a Identity Field but the Source does not have the value for Identity
Column.
Thanks
Prabhat
"Wayne Snyder" <wayne.nospam.snyder@.mariner-usa.com> wrote in message
news:#m#6mmwnEHA.3900@.TK2MSFTNGP10.phx.gbl...[vbcol=seagreen]
> using bulk insert there is a keepidentity parameter, using bcp it is -E...
> Both are documented in books on line
> --
> 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
> "Prabhat" <not_a_mail@.hotmail.com> wrote in message
> news:OKa8j8unEHA.3868@.TK2MSFTNGP11.phx.gbl...
> table
> Primary
text[vbcol=seagreen]
> the
Identity[vbcol=seagreen]
> want
to
> be
>

Bulk Insert With Identity Field

Hi All,
Can I Bulk Insert to to SQL Table with a Identity Column in it?
My Source is a text file with 17 Columns and My Target is a SQL Server table
with 18 Columns (all 17 column of the source + 1 Identity Column as Primary
KEY).
So In this Situation How can i Bulk Insert to the SQL Table from the text
file. Please give small example if possible.
Also If my SQL table is have 2 More Extra Column Can I Boul Insert from the
above source?
Ex: Total 19 Columns ( all 17 columns of the source text file + 1 Identity
Column + 1 Extra column). If I want to Insert into the 17 columns and I want
the Indetity column to generate auto numbers and the Last Extra Column to be
Filled with Some "Char (1)" Value. Is that Possible?
Thanks for any Help or suggestions
Prabhatusing bulk insert there is a keepidentity parameter, using bcp it is -E...
Both are documented in books on line
--
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
"Prabhat" <not_a_mail@.hotmail.com> wrote in message
news:OKa8j8unEHA.3868@.TK2MSFTNGP11.phx.gbl...
> Hi All,
> Can I Bulk Insert to to SQL Table with a Identity Column in it?
> My Source is a text file with 17 Columns and My Target is a SQL Server
table
> with 18 Columns (all 17 column of the source + 1 Identity Column as
Primary
> KEY).
> So In this Situation How can i Bulk Insert to the SQL Table from the text
> file. Please give small example if possible.
> Also If my SQL table is have 2 More Extra Column Can I Boul Insert from
the
> above source?
> Ex: Total 19 Columns ( all 17 columns of the source text file + 1 Identity
> Column + 1 Extra column). If I want to Insert into the 17 columns and I
want
> the Indetity column to generate auto numbers and the Last Extra Column to
be
> Filled with Some "Char (1)" Value. Is that Possible?
> Thanks for any Help or suggestions
> Prabhat
>|||Thanks for the Hint. I have seen that in BOL but did not get any Example.
Can you suggest any site or give me a small Example where the Target table
has a Identity Field but the Source does not have the value for Identity
Column.
Thanks
Prabhat
"Wayne Snyder" <wayne.nospam.snyder@.mariner-usa.com> wrote in message
news:#m#6mmwnEHA.3900@.TK2MSFTNGP10.phx.gbl...
> using bulk insert there is a keepidentity parameter, using bcp it is -E...
> Both are documented in books on line
> --
> 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
> "Prabhat" <not_a_mail@.hotmail.com> wrote in message
> news:OKa8j8unEHA.3868@.TK2MSFTNGP11.phx.gbl...
> > Hi All,
> >
> > Can I Bulk Insert to to SQL Table with a Identity Column in it?
> >
> > My Source is a text file with 17 Columns and My Target is a SQL Server
> table
> > with 18 Columns (all 17 column of the source + 1 Identity Column as
> Primary
> > KEY).
> >
> > So In this Situation How can i Bulk Insert to the SQL Table from the
text
> > file. Please give small example if possible.
> >
> > Also If my SQL table is have 2 More Extra Column Can I Boul Insert from
> the
> > above source?
> > Ex: Total 19 Columns ( all 17 columns of the source text file + 1
Identity
> > Column + 1 Extra column). If I want to Insert into the 17 columns and I
> want
> > the Indetity column to generate auto numbers and the Last Extra Column
to
> be
> > Filled with Some "Char (1)" Value. Is that Possible?
> >
> > Thanks for any Help or suggestions
> > Prabhat
> >
> >
>sql

Bulk Insert with Added Columns

Hi, I'd like to do a bulk copy insert inside a dts package but the table i'm
inserting to has more columns than the text file. I know you can change the
format file to take care of this, but how do you insert actual values into
those extra columns instead of just NULLS? The values for those extra column
s
are uniquely identified by the text file.
What I could do is do a bulk insert and then run an execute sql command
afterwards which updates the table by replacing the NULL values with actual
values. However, this db is multi-user with each user having different value
s
to insert, so if two imports are done at the same time, someone could be
updating the rows of another person's import. Locking the table is also not
a
possibility because the imports take a long time and we don't want one user
waiting on another one to finish.
Right now, I'm using an ActiveX script in a transform data task in the DTS
package. So there's two transformations going on in that task: one is the
copy column from the text file, and another is adding values to the extra
columns. This works well, however, it requires row by row processing and thu
s
it takes over 3 times longer than a bulk insert.
Does anyone have a solution to setting values of extra columns in a bulk
insert? Any help is appreciated. Thanks!Where do those extra values come from?
ML|||These extra values are global variables in the dts package
"ML" wrote:

> Where do those extra values come from?
>
> ML|||In that case you can design a set-based solution. You need something like th
is:
1) you need access to the file - one way to achieve this is linking the file
as a linked server. Read more on this here:
http://msdn.microsoft.com/library/d... />
a_8gqa.asp
http://msdn.microsoft.com/library/d...ma_ini_file.asp
2) you need access to your global variables - the best way would be to
create a procedure that accesses the linked file and accepts the values of
those global variables as parameters;
3) in your procedure you then combine the values from the source file and
the values from the DTS, and insert them into the destination table(s).
You could even use a folder-sniffer to start the job automatically, as soon
as the source file becomes available in the source folder.
ML

Thursday, March 22, 2012

Bulk Insert Task - Where Does The First Row Begin?

Hi,

I am using the Bulk Insert Task to bulk copy data from a flat file to a SQL Server 2005 table. The flat file contains pipe ( | ) delimited columns and several thousand records which are {CR}{LF} delimited. The file also contains a header record and trailer record, which contain several words but also contains a ( | ) symbol among those words.

E.g.:

HEDR | yadi yadi yada
500 | Data Data | More Data | 600
460 | Datum | More More | 705
550 | Data | Data | 603
FOOTR | yadi yadi yada

I need to ignore the header and trailer records and just pickup the proper records. But even though I manually set the First Row property to 2 and the Last Row property to 4, It does not pickup the first true record i.e. the record which begins with 500, and if I set the First Row to 1, it throws me an error citing 'truncation error' or similar. I think it is taking the first record (i.e. header row along with the first row as one, and since there are now more pipes ( | ) the error is thrown)

I've tried setting different values for these properties but to no avail... Any help will be deeply appreciated...

Thanks
Gogula

Bulk insert task expects the header and footer records also to be in the same format as other Data Rows, so to do, what you are trying to do, your data needs to be in this format:

HEDR | yadi | yadi | yada
500 | Data Data | More Data | 600
460 | Datum | More More | 705
550 | Data | Data | 603
FOOTR| yadi | yadi | yada

This is the same behavior as Transact-SQL bulk insert, and if you try something like the following with your file, you will encounter errors as well. Therefore, this is by design, according to what bulk insert supports.

BULK INSERT tempdb.dbo.Table_1

FROM 'C:\\File.txt'

WITH

(

FIELDTERMINATOR ='|',

ROWTERMINATOR ='\n',

firstrow = 2,

lastrow = 4

)

|||Hi Rangeeta,

Thanks for the reply. But what if in case there might be more than one header and footer (which I programmatic ally count and take into consideration) and those headers cannot be separated by the delimiters (since the client needs to send it to us)? It seems a little unfair that Bulk Insert expects the headers to be in the same format as the other rows...
Are there any alternate methods to do this?

Thanks again
Gogula

Tuesday, March 20, 2012

bulk insert question

Hi! We are in the process of adding few columns to our large tables (200
million rows) and altering few columns from varchar to char. One of the
option I am thinking is to bcp data out, change the schema and bulk insert
data in. Looks like with either bcp.exe or bulk insert command, you won't be
able to load the data in, if the schema of table get changed. Is this true?
I know that I can use DTS export/import to do this task but since bulk
insert is the fastest method I would like to try that option if possible.
Besides, Bulk insert I could also change schema with Alter table command. I
don't know if thats better than unloading/change/reload method that I
mentioned above.
I would appreiciate it, if anyone who have export/change schema/import large
table, give me some direction here.
thanks
If you use the Native mode I don't think you can do it but have you actually
tried? BCP out a few thousand rows, create anew table and Bulk Insert it
back in. If native wont work I am pretty sure char mode will.
Andrew J. Kelly SQL MVP
"james" <kush@.brandes.com> wrote in message
news:%23j3fNPKrFHA.3640@.tk2msftngp13.phx.gbl...
> Hi! We are in the process of adding few columns to our large tables (200
> million rows) and altering few columns from varchar to char. One of the
> option I am thinking is to bcp data out, change the schema and bulk insert
> data in. Looks like with either bcp.exe or bulk insert command, you won't
> be
> able to load the data in, if the schema of table get changed. Is this
> true?
> I know that I can use DTS export/import to do this task but since bulk
> insert is the fastest method I would like to try that option if possible.
> Besides, Bulk insert I could also change schema with Alter table command.
> I
> don't know if thats better than unloading/change/reload method that I
> mentioned above.
> I would appreiciate it, if anyone who have export/change schema/import
> large
> table, give me some direction here.
> thanks
>
|||I have tried both native and character mode and both erroed out. Which is
kind of expected, since schema got changed, the program doesn't have any way
of knowing which column in datafile (native or char) maps to which column in
table.
I haven't tried format file so far, which is what I am going to do next and
see if I can alter format file and make this thing work.
Thanks for your Input.
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:uWZ$MoLrFHA.2624@.TK2MSFTNGP15.phx.gbl...
> If you use the Native mode I don't think you can do it but have you
actually[vbcol=seagreen]
> tried? BCP out a few thousand rows, create anew table and Bulk Insert it
> back in. If native wont work I am pretty sure char mode will.
> --
> Andrew J. Kelly SQL MVP
>
> "james" <kush@.brandes.com> wrote in message
> news:%23j3fNPKrFHA.3640@.tk2msftngp13.phx.gbl...
insert[vbcol=seagreen]
won't[vbcol=seagreen]
possible.[vbcol=seagreen]
command.
>
|||Yes if you change the columns around you need to use a format file. But I
don't see why it wont work with the format file.
Andrew J. Kelly SQL MVP
"james" <kush@.brandes.com> wrote in message
news:%23Z9GTdWrFHA.2624@.TK2MSFTNGP15.phx.gbl...
>I have tried both native and character mode and both erroed out. Which is
> kind of expected, since schema got changed, the program doesn't have any
> way
> of knowing which column in datafile (native or char) maps to which column
> in
> table.
> I haven't tried format file so far, which is what I am going to do next
> and
> see if I can alter format file and make this thing work.
> Thanks for your Input.
> "Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
> news:uWZ$MoLrFHA.2624@.TK2MSFTNGP15.phx.gbl...
> actually
> insert
> won't
> possible.
> command.
>
|||With the format file both native and character mode worked. Thanks again for
your time.
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:%23ov1hlWrFHA.3352@.TK2MSFTNGP14.phx.gbl...[vbcol=seagreen]
> Yes if you change the columns around you need to use a format file. But I
> don't see why it wont work with the format file.
> --
> Andrew J. Kelly SQL MVP
>
> "james" <kush@.brandes.com> wrote in message
> news:%23Z9GTdWrFHA.2624@.TK2MSFTNGP15.phx.gbl...
column[vbcol=seagreen]
it[vbcol=seagreen]
the[vbcol=seagreen]
bulk[vbcol=seagreen]
schema/import
>

bulk insert question

Hi! We are in the process of adding few columns to our large tables (200
million rows) and altering few columns from varchar to char. One of the
option I am thinking is to bcp data out, change the schema and bulk insert
data in. Looks like with either bcp.exe or bulk insert command, you won't be
able to load the data in, if the schema of table get changed. Is this true?
I know that I can use DTS export/import to do this task but since bulk
insert is the fastest method I would like to try that option if possible.
Besides, Bulk insert I could also change schema with Alter table command. I
don't know if thats better than unloading/change/reload method that I
mentioned above.
I would appreiciate it, if anyone who have export/change schema/import large
table, give me some direction here.
thanksIf you use the Native mode I don't think you can do it but have you actually
tried? BCP out a few thousand rows, create anew table and Bulk Insert it
back in. If native wont work I am pretty sure char mode will.
Andrew J. Kelly SQL MVP
"james" <kush@.brandes.com> wrote in message
news:%23j3fNPKrFHA.3640@.tk2msftngp13.phx.gbl...
> Hi! We are in the process of adding few columns to our large tables (200
> million rows) and altering few columns from varchar to char. One of the
> option I am thinking is to bcp data out, change the schema and bulk insert
> data in. Looks like with either bcp.exe or bulk insert command, you won't
> be
> able to load the data in, if the schema of table get changed. Is this
> true?
> I know that I can use DTS export/import to do this task but since bulk
> insert is the fastest method I would like to try that option if possible.
> Besides, Bulk insert I could also change schema with Alter table command.
> I
> don't know if thats better than unloading/change/reload method that I
> mentioned above.
> I would appreiciate it, if anyone who have export/change schema/import
> large
> table, give me some direction here.
> thanks
>|||I have tried both native and character mode and both erroed out. Which is
kind of expected, since schema got changed, the program doesn't have any way
of knowing which column in datafile (native or char) maps to which column in
table.
I haven't tried format file so far, which is what I am going to do next and
see if I can alter format file and make this thing work.
Thanks for your Input.
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:uWZ$MoLrFHA.2624@.TK2MSFTNGP15.phx.gbl...
> If you use the Native mode I don't think you can do it but have you
actually
> tried? BCP out a few thousand rows, create anew table and Bulk Insert it
> back in. If native wont work I am pretty sure char mode will.
> --
> Andrew J. Kelly SQL MVP
>
> "james" <kush@.brandes.com> wrote in message
> news:%23j3fNPKrFHA.3640@.tk2msftngp13.phx.gbl...
insert[vbcol=seagreen]
won't[vbcol=seagreen]
possible.[vbcol=seagreen]
command.[vbcol=seagreen]
>|||Yes if you change the columns around you need to use a format file. But I
don't see why it wont work with the format file.
Andrew J. Kelly SQL MVP
"james" <kush@.brandes.com> wrote in message
news:%23Z9GTdWrFHA.2624@.TK2MSFTNGP15.phx.gbl...
>I have tried both native and character mode and both erroed out. Which is
> kind of expected, since schema got changed, the program doesn't have any
> way
> of knowing which column in datafile (native or char) maps to which column
> in
> table.
> I haven't tried format file so far, which is what I am going to do next
> and
> see if I can alter format file and make this thing work.
> Thanks for your Input.
> "Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
> news:uWZ$MoLrFHA.2624@.TK2MSFTNGP15.phx.gbl...
> actually
> insert
> won't
> possible.
> command.
>|||With the format file both native and character mode worked. Thanks again for
your time.
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:%23ov1hlWrFHA.3352@.TK2MSFTNGP14.phx.gbl...
> Yes if you change the columns around you need to use a format file. But I
> don't see why it wont work with the format file.
> --
> Andrew J. Kelly SQL MVP
>
> "james" <kush@.brandes.com> wrote in message
> news:%23Z9GTdWrFHA.2624@.TK2MSFTNGP15.phx.gbl...
column[vbcol=seagreen]
it[vbcol=seagreen]
the[vbcol=seagreen]
bulk[vbcol=seagreen]
schema/import[vbcol=seagreen]
>sql

bulk insert question

Hi! We are in the process of adding few columns to our large tables (200
million rows) and altering few columns from varchar to char. One of the
option I am thinking is to bcp data out, change the schema and bulk insert
data in. Looks like with either bcp.exe or bulk insert command, you won't be
able to load the data in, if the schema of table get changed. Is this true?
I know that I can use DTS export/import to do this task but since bulk
insert is the fastest method I would like to try that option if possible.
Besides, Bulk insert I could also change schema with Alter table command. I
don't know if thats better than unloading/change/reload method that I
mentioned above.
I would appreiciate it, if anyone who have export/change schema/import large
table, give me some direction here.
thanksIf you use the Native mode I don't think you can do it but have you actually
tried? BCP out a few thousand rows, create anew table and Bulk Insert it
back in. If native wont work I am pretty sure char mode will.
--
Andrew J. Kelly SQL MVP
"james" <kush@.brandes.com> wrote in message
news:%23j3fNPKrFHA.3640@.tk2msftngp13.phx.gbl...
> Hi! We are in the process of adding few columns to our large tables (200
> million rows) and altering few columns from varchar to char. One of the
> option I am thinking is to bcp data out, change the schema and bulk insert
> data in. Looks like with either bcp.exe or bulk insert command, you won't
> be
> able to load the data in, if the schema of table get changed. Is this
> true?
> I know that I can use DTS export/import to do this task but since bulk
> insert is the fastest method I would like to try that option if possible.
> Besides, Bulk insert I could also change schema with Alter table command.
> I
> don't know if thats better than unloading/change/reload method that I
> mentioned above.
> I would appreiciate it, if anyone who have export/change schema/import
> large
> table, give me some direction here.
> thanks
>|||I have tried both native and character mode and both erroed out. Which is
kind of expected, since schema got changed, the program doesn't have any way
of knowing which column in datafile (native or char) maps to which column in
table.
I haven't tried format file so far, which is what I am going to do next and
see if I can alter format file and make this thing work.
Thanks for your Input.
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:uWZ$MoLrFHA.2624@.TK2MSFTNGP15.phx.gbl...
> If you use the Native mode I don't think you can do it but have you
actually
> tried? BCP out a few thousand rows, create anew table and Bulk Insert it
> back in. If native wont work I am pretty sure char mode will.
> --
> Andrew J. Kelly SQL MVP
>
> "james" <kush@.brandes.com> wrote in message
> news:%23j3fNPKrFHA.3640@.tk2msftngp13.phx.gbl...
> > Hi! We are in the process of adding few columns to our large tables (200
> > million rows) and altering few columns from varchar to char. One of the
> > option I am thinking is to bcp data out, change the schema and bulk
insert
> > data in. Looks like with either bcp.exe or bulk insert command, you
won't
> > be
> > able to load the data in, if the schema of table get changed. Is this
> > true?
> > I know that I can use DTS export/import to do this task but since bulk
> > insert is the fastest method I would like to try that option if
possible.
> > Besides, Bulk insert I could also change schema with Alter table
command.
> > I
> > don't know if thats better than unloading/change/reload method that I
> > mentioned above.
> > I would appreiciate it, if anyone who have export/change schema/import
> > large
> > table, give me some direction here.
> >
> > thanks
> >
> >
>|||Yes if you change the columns around you need to use a format file. But I
don't see why it wont work with the format file.
--
Andrew J. Kelly SQL MVP
"james" <kush@.brandes.com> wrote in message
news:%23Z9GTdWrFHA.2624@.TK2MSFTNGP15.phx.gbl...
>I have tried both native and character mode and both erroed out. Which is
> kind of expected, since schema got changed, the program doesn't have any
> way
> of knowing which column in datafile (native or char) maps to which column
> in
> table.
> I haven't tried format file so far, which is what I am going to do next
> and
> see if I can alter format file and make this thing work.
> Thanks for your Input.
> "Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
> news:uWZ$MoLrFHA.2624@.TK2MSFTNGP15.phx.gbl...
>> If you use the Native mode I don't think you can do it but have you
> actually
>> tried? BCP out a few thousand rows, create anew table and Bulk Insert it
>> back in. If native wont work I am pretty sure char mode will.
>> --
>> Andrew J. Kelly SQL MVP
>>
>> "james" <kush@.brandes.com> wrote in message
>> news:%23j3fNPKrFHA.3640@.tk2msftngp13.phx.gbl...
>> > Hi! We are in the process of adding few columns to our large tables
>> > (200
>> > million rows) and altering few columns from varchar to char. One of the
>> > option I am thinking is to bcp data out, change the schema and bulk
> insert
>> > data in. Looks like with either bcp.exe or bulk insert command, you
> won't
>> > be
>> > able to load the data in, if the schema of table get changed. Is this
>> > true?
>> > I know that I can use DTS export/import to do this task but since bulk
>> > insert is the fastest method I would like to try that option if
> possible.
>> > Besides, Bulk insert I could also change schema with Alter table
> command.
>> > I
>> > don't know if thats better than unloading/change/reload method that I
>> > mentioned above.
>> > I would appreiciate it, if anyone who have export/change schema/import
>> > large
>> > table, give me some direction here.
>> >
>> > thanks
>> >
>> >
>>
>|||With the format file both native and character mode worked. Thanks again for
your time.
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:%23ov1hlWrFHA.3352@.TK2MSFTNGP14.phx.gbl...
> Yes if you change the columns around you need to use a format file. But I
> don't see why it wont work with the format file.
> --
> Andrew J. Kelly SQL MVP
>
> "james" <kush@.brandes.com> wrote in message
> news:%23Z9GTdWrFHA.2624@.TK2MSFTNGP15.phx.gbl...
> >I have tried both native and character mode and both erroed out. Which is
> > kind of expected, since schema got changed, the program doesn't have any
> > way
> > of knowing which column in datafile (native or char) maps to which
column
> > in
> > table.
> > I haven't tried format file so far, which is what I am going to do next
> > and
> > see if I can alter format file and make this thing work.
> > Thanks for your Input.
> >
> > "Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
> > news:uWZ$MoLrFHA.2624@.TK2MSFTNGP15.phx.gbl...
> >> If you use the Native mode I don't think you can do it but have you
> > actually
> >> tried? BCP out a few thousand rows, create anew table and Bulk Insert
it
> >> back in. If native wont work I am pretty sure char mode will.
> >>
> >> --
> >> Andrew J. Kelly SQL MVP
> >>
> >>
> >> "james" <kush@.brandes.com> wrote in message
> >> news:%23j3fNPKrFHA.3640@.tk2msftngp13.phx.gbl...
> >> > Hi! We are in the process of adding few columns to our large tables
> >> > (200
> >> > million rows) and altering few columns from varchar to char. One of
the
> >> > option I am thinking is to bcp data out, change the schema and bulk
> > insert
> >> > data in. Looks like with either bcp.exe or bulk insert command, you
> > won't
> >> > be
> >> > able to load the data in, if the schema of table get changed. Is this
> >> > true?
> >> > I know that I can use DTS export/import to do this task but since
bulk
> >> > insert is the fastest method I would like to try that option if
> > possible.
> >> > Besides, Bulk insert I could also change schema with Alter table
> > command.
> >> > I
> >> > don't know if thats better than unloading/change/reload method that I
> >> > mentioned above.
> >> > I would appreiciate it, if anyone who have export/change
schema/import
> >> > large
> >> > table, give me some direction here.
> >> >
> >> > thanks
> >> >
> >> >
> >>
> >>
> >
> >
>

BULK INSERT Performance with format files

Hi,
We have a scenario where we want to bulk insert data into only a select
number of columns in a table.
We are currently generating a BCP file with
- data for all columns, NULL in case of columns which are not needed (These
columns are nullable in the table)
- column data is ordered as it is in the table.
We use the BULK INSERT statement.
Now, we want to use a format file and write data (into the bcp file) for
only those columns which we need. This will definitely give us a faster bcp
file creation process.
However we want to know whether using a format file will slow down the "bulk
insert" itself. Also, if there are any other issues with format files.
Thanks,
NitinAlternatively try creating a view on your
table that selects only the columns you want to insert into.
You should then be able to bulk insert directly into this view
without a format file.|||Alternatively try creating a view on your
table that selects only the columns you want to insert into.
You should then be able to bulk insert directly into this view
without a format file.|||"Nitin M" <nitin@.nowhere.com> wrote in message
news:ugKwd8UAGHA.264@.tk2msftngp13.phx.gbl...
> Hi,
> We have a scenario where we want to bulk insert data into only a select
> number of columns in a table.
> We are currently generating a BCP file with
> - data for all columns, NULL in case of columns which are not needed
> (These columns are nullable in the table)
> - column data is ordered as it is in the table.
> We use the BULK INSERT statement.
> Now, we want to use a format file and write data (into the bcp file) for
> only those columns which we need. This will definitely give us a faster
> bcp file creation process.
> However we want to know whether using a format file will slow down the
> "bulk insert" itself. Also, if there are any other issues with format
> files.
> Thanks,
> Nitin
>
We use this successfully where I work all of the time. I have a large flat
file (about 4 GB) that has each row with a length of about 2000 bytes. We
only need about 500 of the bytes from various positions within that file.
We use a format file and can pull in the 3 million records in about 15
minutes on our test system.
One note.. Setting a ROWS_PER_BATCH size to about 1000 seemed to speed up
the process a lot for our usage.
Rick Sawtell
MCT, MCSD, MCDBAsql

Monday, March 19, 2012

Bulk insert into table with more columns than data within file

Hey all

I have a bulk insert situation that would be nice to be able to pull off. I have a flat file with 46 columns that are to go into a table. The table, I want to have a 47th column to be updated later on by means of a stored proc saying if the import into the system was sucessful or not. I have the rowterminator set as '"\n' thinking that would tell SQL to begin on the next row, leaving the importstatus column null but i still receive an error.

First of all, is this idea possible within this insert statement. Secondly, if so, what would be the syntax to tell the insert statement to skip that particular column. It is the last column listed in the table so it just needs to start on the next row after it inserts the last bit of data in the flatfile.

If this is not possible, is it possible to bulk insert into a temp table?

Thanksyou can do this if you specify a format file. read up on bcp in BOL about format files.

alternatively, you could bulk insert into a staging table or temp table and then do insert/select from there.

finally, if you can generate the file over again, you can include a null like this (assuming comma separated:

1,2,3,,5,

note the extra commas. in this case, a null would be inserted in the 4th and 6th positions.

Wednesday, March 7, 2012

Bulk Insert and Default Values

I am experiencing an issue with bulk insert and default values.
Say that i have a table with thirteen. My format file species all columns
except 14. Column 14 is an int and has a default value of '9'. When i insert
a record manually it picks up the default value fine, but when i bulk insert
all of the records have a default value of 0. Any thoughts?
string|string|s|59|aa|aaaaa|N|N|N|N|N|Y|Y
8.0
13
1 SQLCHAR 0 4 "|" 1 col1 SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 25 "|" 2 col2 SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 1 "|" 3 col3 SQL_Latin1_General_CP1_CI_AS
4 SQLCHAR 0 2 "|" 4 col4 SQL_Latin1_General_CP1_CI_AS
5 SQLCHAR 0 4 "|" 5 col5 SQL_Latin1_General_CP1_CI_AS
6 SQLCHAR 0 6 "|" 6 col6 SQL_Latin1_General_CP1_CI_AS
7 SQLCHAR 0 1 "|" 7 col7 SQL_Latin1_General_CP1_CI_AS
8 SQLCHAR 0 1 "|" 8 col8 SQL_Latin1_General_CP1_CI_AS
9 SQLCHAR 0 1 "|" 9 col9 SQL_Latin1_General_CP1_CI_AS
10 SQLCHAR 0 1 "|" 10 col10 SQL_Latin1_General_CP1_CI_AS
11 SQLCHAR 0 1 "|" 11 col11 SQL_Latin1_General_CP1_CI_AS
12 SQLCHAR 0 1 "|" 12 col12 SQL_Latin1_General_CP1_CI_AS
13 SQLCHAR 0 1 "\r\n" 13 col13 SQL_Latin1_General_CP1_CI_AS
BULK INSERT db..table FROM 'file' WITH
(
FORMATFILE='fmt.fmt',
CODEPAGE='RAW',
ROWS_PER_BATCH=141,
MAXERRORS=10,
TABLOCK
)
GO
Thoughts?
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200802/1> When i insert
> a record manually it picks up the default value fine, but when i bulk
> insert
> all of the records have a default value of 0. Any thoughts?
I tried your format file, data file and BULK INSERT with the table below and
the default value was assigned properly under both SQL 2000 and SQL 2005. I
find it strange that a value of zero is assigned in your environment...
CREATE TABLE dbo.table1
(
col1 varchar(10),
col2 varchar(10),
col3 varchar(10),
col4 varchar(10),
col5 varchar(10),
col6 varchar(10),
col7 varchar(10),
col8 varchar(10),
col9 varchar(10),
col10 varchar(10),
col11 varchar(10),
col12 varchar(10),
col13 varchar(10),
col14 varchar(10) NULL CONSTRAINT DF_table_col14 DEFAULT '9'
)
--
Hope this helps.
Dan Guzman
SQL Server MVP
"lotek via SQLMonster.com" <u16539@.uwe> wrote in message
news:7f4a9d35a33d0@.uwe...
>I am experiencing an issue with bulk insert and default values.
> Say that i have a table with thirteen. My format file species all columns
> except 14. Column 14 is an int and has a default value of '9'. When i
> insert
> a record manually it picks up the default value fine, but when i bulk
> insert
> all of the records have a default value of 0. Any thoughts?
> string|string|s|59|aa|aaaaa|N|N|N|N|N|Y|Y
> 8.0
> 13
> 1 SQLCHAR 0 4 "|" 1 col1 SQL_Latin1_General_CP1_CI_AS
> 2 SQLCHAR 0 25 "|" 2 col2 SQL_Latin1_General_CP1_CI_AS
> 3 SQLCHAR 0 1 "|" 3 col3 SQL_Latin1_General_CP1_CI_AS
> 4 SQLCHAR 0 2 "|" 4 col4 SQL_Latin1_General_CP1_CI_AS
> 5 SQLCHAR 0 4 "|" 5 col5 SQL_Latin1_General_CP1_CI_AS
> 6 SQLCHAR 0 6 "|" 6 col6 SQL_Latin1_General_CP1_CI_AS
> 7 SQLCHAR 0 1 "|" 7 col7 SQL_Latin1_General_CP1_CI_AS
> 8 SQLCHAR 0 1 "|" 8 col8 SQL_Latin1_General_CP1_CI_AS
> 9 SQLCHAR 0 1 "|" 9 col9 SQL_Latin1_General_CP1_CI_AS
> 10 SQLCHAR 0 1 "|" 10 col10 SQL_Latin1_General_CP1_CI_AS
> 11 SQLCHAR 0 1 "|" 11 col11 SQL_Latin1_General_CP1_CI_AS
> 12 SQLCHAR 0 1 "|" 12 col12 SQL_Latin1_General_CP1_CI_AS
> 13 SQLCHAR 0 1 "\r\n" 13 col13 SQL_Latin1_General_CP1_CI_AS
> BULK INSERT db..table FROM 'file' WITH
> (
> FORMATFILE='fmt.fmt',
> CODEPAGE='RAW',
> ROWS_PER_BATCH=141,
> MAXERRORS=10,
> TABLOCK
> )
> GO
> Thoughts?
> --
> Message posted via SQLMonster.com
> http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200802/1
>|||Col14 is an int where you have a varchar. That might make the difference...
Thanks for your time.
-Matt
Dan Guzman wrote:
>> When i insert
>> a record manually it picks up the default value fine, but when i bulk
>> insert
>> all of the records have a default value of 0. Any thoughts?
>I tried your format file, data file and BULK INSERT with the table below and
>the default value was assigned properly under both SQL 2000 and SQL 2005. I
>find it strange that a value of zero is assigned in your environment...
>CREATE TABLE dbo.table1
>(
>col1 varchar(10),
>col2 varchar(10),
>col3 varchar(10),
>col4 varchar(10),
>col5 varchar(10),
>col6 varchar(10),
>col7 varchar(10),
>col8 varchar(10),
>col9 varchar(10),
>col10 varchar(10),
>col11 varchar(10),
>col12 varchar(10),
>col13 varchar(10),
>col14 varchar(10) NULL CONSTRAINT DF_table_col14 DEFAULT '9'
>)
>>I am experiencing an issue with bulk insert and default values.
>[quoted text clipped - 34 lines]
>> Thoughts?
--
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200802/1|||> Col14 is an int where you have a varchar. That might make the
> difference...
I ran another test with the int and it works in my environment. Below is
the full repro. The only change I made from the info your original post
(other than file paths) was to shorten the test data to avoid truncation.
You might try the CHECK_CONSTRAINTS option of BULK INSERT but it wasn't
necessary in my environment.
CREATE TABLE dbo.table1
(
col1 varchar(10),
col2 varchar(10),
col3 varchar(10),
col4 varchar(10),
col5 varchar(10),
col6 varchar(10),
col7 varchar(10),
col8 varchar(10),
col9 varchar(10),
col10 varchar(10),
col11 varchar(10),
col12 varchar(10),
col13 varchar(10),
col14 int NULL CONSTRAINT DF_table_col14 DEFAULT 9
)
GO
--c:\temp\fmt.fmt
8.0
13
1 SQLCHAR 0 4 "|" 1 col1 SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 25 "|" 2 col2 SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 1 "|" 3 col3 SQL_Latin1_General_CP1_CI_AS
4 SQLCHAR 0 2 "|" 4 col4 SQL_Latin1_General_CP1_CI_AS
5 SQLCHAR 0 4 "|" 5 col5 SQL_Latin1_General_CP1_CI_AS
6 SQLCHAR 0 6 "|" 6 col6 SQL_Latin1_General_CP1_CI_AS
7 SQLCHAR 0 1 "|" 7 col7 SQL_Latin1_General_CP1_CI_AS
8 SQLCHAR 0 1 "|" 8 col8 SQL_Latin1_General_CP1_CI_AS
9 SQLCHAR 0 1 "|" 9 col9 SQL_Latin1_General_CP1_CI_AS
10 SQLCHAR 0 1 "|" 10 col10 SQL_Latin1_General_CP1_CI_AS
11 SQLCHAR 0 1 "|" 11 col11 SQL_Latin1_General_CP1_CI_AS
12 SQLCHAR 0 1 "|" 12 col12 SQL_Latin1_General_CP1_CI_AS
13 SQLCHAR 0 1 "\r\n" 13 col13 SQL_Latin1_General_CP1_CI_AS
--c:\temp\file.txt
a|b|s|59|aa|aaaaa|N|N|N|N|N|Y|Y
BULK INSERT dbo.table1 FROM 'c:\temp\file.txt' WITH
(
FORMATFILE='c:\temp\fmt.fmt',
CODEPAGE='RAW',
ROWS_PER_BATCH=141,
MAXERRORS=10,
TABLOCK
)
GO
SELECT * FROM dbo.table1
GO
Hope this helps.
Dan Guzman
SQL Server MVP
"lotek via SQLMonster.com" <u16539@.uwe> wrote in message
news:7f4ee474dd997@.uwe...
> Col14 is an int where you have a varchar. That might make the
> difference...
> Thanks for your time.
> -Matt
> Dan Guzman wrote:
>> When i insert
>> a record manually it picks up the default value fine, but when i bulk
>> insert
>> all of the records have a default value of 0. Any thoughts?
>>I tried your format file, data file and BULK INSERT with the table below
>>and
>>the default value was assigned properly under both SQL 2000 and SQL 2005.
>>I
>>find it strange that a value of zero is assigned in your environment...
>>CREATE TABLE dbo.table1
>>(
>>col1 varchar(10),
>>col2 varchar(10),
>>col3 varchar(10),
>>col4 varchar(10),
>>col5 varchar(10),
>>col6 varchar(10),
>>col7 varchar(10),
>>col8 varchar(10),
>>col9 varchar(10),
>>col10 varchar(10),
>>col11 varchar(10),
>>col12 varchar(10),
>>col13 varchar(10),
>>col14 varchar(10) NULL CONSTRAINT DF_table_col14 DEFAULT '9'
>>)
>>I am experiencing an issue with bulk insert and default values.
>>[quoted text clipped - 34 lines]
>> Thoughts?
> --
> Message posted via SQLMonster.com
> http://www.sqlmonster.com/Uwe/Forums.aspx/sql-server/200802/1
>

Saturday, February 25, 2012

BULK INSERT - Inserting txt file with columns in different order than define in table

How can I use bulk insert to insert a text file where the columns in the text file is in different order than the columns in the table?

I have a ZIP table with Zip_Code, Zip_City, Zip_State and the text file has the fields in Zip_City, Zip_State, Zip_Code. The instructions were to keep the order as defined in the Entity Definition which would be the first order.

My code for the bulk insert is usually

Code: ( text )

    BULK INSERT DB2914.dbo.[ZIP] FROM 'C:\Documents and Settings\Jthep\My Documents\SQL Server Management Studio\Projects\S2914-HW3\ZIP_data.txt'WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')

Is there a way to format the columns so I can actually set any column in the text file to any column in the table using Bulk Insert?You can insert into a temp table first and map as you wish...

Good Luck.|||Thanks, I figured I couldnt do it using bulk insert unless I created a temp. However, I'm now allowed to create the table with attributes of the same order as the data in the text file as many of my classmates were asking the professor about it. =D

Bulk Insert

I have a problem with bulk insert importation. I have a txt file with 3400 columns and 1 table with two fields.

data - varchar(5000)
id - numeric (identity yes - 1)

when I import the file the system return this error message:

Bulk Insert fails. Column is too long in the data file for row 1, column 1. Make sure the field terminator and row terminator are specified correctly.

my bulk insert sintaxe is:

BULK INSERT eflex.dbo.Import_Data
FROM 'C:\HP\HP_Receive\FBSJ01D5.txt'
WITH (FIELDTERMINATOR = '\n')

can anybody help-me?

thanks,Sure
1. What is your row terminator?
2. Maybe the data row exceeds your definition.
3-n any number of data or schema errors

Try this ... create a input table with 1 column defined as varchar(8000), then bcp in the file. You can then use SQL Server to inspect column lengths, and even parse the data into your working table.

Friday, February 24, 2012

Bulk Insert

Hi,
I'm trying to use bulk insert to import a unformated text file into a table
with 6 columns using the following code in query analyzer:
BULK INSERT tblLocalizerTemp from 'c:\temp\textfile.txt'
WITH (CODEPAGE='RAW', ROWTERMINATOR='\n')
Since I don't have column delimiters, I left out FIELDTERMINATOR but I'm
getting an error saying that the column from the text file is too long. How
can I specify how this gets imported. The docs are not very helpful. Also,
is there a way to just import specific bytes of the file into the table
without having to import the whole record.
ThanksI got this to work with the following but now I'm having trouble using a
variable to use may my path information. I want to eventually call a stored
proc from a VB6 app
declare @.cLocalizerPath varchar(100)
declare @.cFmtPath varchar(100)
select @.cLocalizerPath = 'c:\temp\temp.txt'
select @.cFmtPath = 'c:\temp\temp.fmt'
BULK INSERT tblLocalizerTemp from @.cLocalizerPath
WITH (CODEPAGE='RAW', FORMATFILE=@.cFmtPath)
"Ellie" <nospam@.nospam.net> wrote in message
news:OsWLJZjPGHA.420@.tk2msftngp13.phx.gbl...
> Hi,
> I'm trying to use bulk insert to import a unformated text file into a
> table with 6 columns using the following code in query analyzer:
> BULK INSERT tblLocalizerTemp from 'c:\temp\textfile.txt'
> WITH (CODEPAGE='RAW', ROWTERMINATOR='\n')
> Since I don't have column delimiters, I left out FIELDTERMINATOR but I'm
> getting an error saying that the column from the text file is too long.
> How can I specify how this gets imported. The docs are not very helpful.
> Also, is there a way to just import specific bytes of the file into the
> table without having to import the whole record.
> Thanks
>

Sunday, February 12, 2012

Building a time sensitive full-text search

I have spent a little time trying to build a time sensitive full-text search on a fairly large database (16M rows, with one of the full text columns an ntext column).

The goal for the search results is to have them time-sensitive - as a basic approach I am taking the relevance of the result, and dividing it by the number of days since the result was entered into the database. this works quite well, but I suspect that I can do something better.

The current approach returns the top 1000 most relevant results and then weights the relevance using the age of the result. It also limits the top 1000 to results that have occurred in the last X weeks.

I am only really interested in the top 100 or so of these weighted results.

As I increase the number of results to use as the starting point, the performance degrades quite quickly, and the results change - there are less relevant search results that actually come to the top of the weighted result.

So...the question is...is there a way to build the time-sensitivity into the relevance itself, or, any other suggestions as to how I could get good time sensitive results from full-text search without sacrificing performance.

thanks in advance

Nick, you've got quite a neat approach there. Unfortunately, the ranking algorithm in SQL Server is not customizable so it is not possible to integrate the value from another column into the ranking value. An alternative workaround would be to encode the date into a special string format which can later be used during full text search to limit the hits in a certain time range by using prefix search. This can reduce the performance degradation that you are experiencing.

Related to the performance, it isn’t clear from your message if you see the performance degrading when you increase the TOP n beyond 1000 or 100 rows?

Also, when you get the initial 1000 most relevant results, are the bottom rows in the initial ranking showing up in the top or bottom 10~20 in the final weighted 100 rows? If they’re showing up in the top 10~20 weighted rows then it could be that the time sensitivity is over weighted which may cause the less relevant results to bubble to the top after being weighted. Of course, you’d be the best judge of the relevance of the results.

I hope this helps.

Thanks

Sara

|||The degradation comes after about 1000 rows, although the FTS is set up with a good amount of RAM for caching, so it does depend a lot on whether the results are cached.

As to the weighting of the time sensitivity, we can get items from the bottom rows in the initial result set showing up at the top of the weighted result set. This is not an unwanted result, but the question of whether the weighting is "correct" is a valid one. I dont pretend to know the answer at present - it feels like a bit of a black art.|||Nick,
In addition to what Sara has asked for, could you post the full output of SELECT @.@.version as for other than SQL Server 2005, the OS platform is an important data point... Could you post your CONTAINSTABLE or FREETEXTABLE code? Do you see the performance degradation if you use the Top_N_Rank parameter?

Yes, this is somewhat of a black art Smile, but ther are other methods of doing this that do not involving joining other tables to get a "best bet" result.

Regards,
John
SQL Full Text Search Blog
http://spaces.msn.com/members/jtkane/

Friday, February 10, 2012

build sql script in excel

Hi,
I have been given an excel spreadsheet with two columns with data.
There are about 20 records in this excel sheet.
Would like to write an insert query to insert these data.
I am thinking of writing an insert query for the first line iin the excel sheet and then drag it down to the last row of data so that it automatically writes the values of the columns in the insert query and i just copy and paste the script into sql to run.
This is what I have but the values of the cells do not get reflected.
Any thoughts pls?
'insert into TBLData (RE_ID, EMAIL) values (' & c2 & ',' & d2 ')'I used a similar thing just today.

I wrote the first cell as:
"INSERT INTO into TBLData (RE_ID, EMAIL) "

then dragged the following formula down:

=" SELECT '" & c2 & "', '" & d2 & "' UNION ALL "

Copy -> Paste to QA and remove the last UNION ALL

HTH

Build Dynamic Table Columns Issue

How I can build a dynamic temp table based upon the dynamic coulmn info from the other table? Please see my attached file as an example. Thanks!
J827use a hughe varchar variable and fill it with a create table statement. To determine which columnnames, try information_schema.tables. Then execute it using exec.|||You need this

http://www.sqlteam.com/item.asp?ItemID=2955|||Brett,

Thanks for the Link and it works for my case.

J827|||Hey, thank Rob Volk...he's the author...

I'm just the messenger...

Lots of good articles over there...

Good Luck