Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Thursday, March 29, 2012

Bulk Loading: TempFilePath security question

I've got a program written that runs on one computer and performs bulk
updates to a SQL Server 2000 table on a different computer. I also
need transactional behavior, so I have to specify a value for
TempFilePath on my SQLXMLBulkLoad3Class object.
I do not have permissions to create folders, files, or interact in any
way with the computer hosting SQL server. This means that the value of
TempFilePath must be a UNC path that can be written to by the
application performing bulk loads, and (at a minimum) read from by the
computer hosting SQL server.
I'd like to make access to this UNC path as restricted as possible. I
am using SQL authentication, not Windows authentication.
How can I tell what ID will try from the SQL Server computer to read
from the TempFilePath?
How much permission will this ID need in the directory? Read only?
Read/write?If you do this with a connection using standard SQL login, then the thread
that is running the query will be using the account that the SQL Server
service runs under to access the file share.
Matt Neerincx [MSFT]
This posting is provided "AS IS", with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.
<isbat1@.yahoo.com> wrote in message
news:1128017182.904592.4100@.z14g2000cwz.googlegroups.com...
> I've got a program written that runs on one computer and performs bulk
> updates to a SQL Server 2000 table on a different computer. I also
> need transactional behavior, so I have to specify a value for
> TempFilePath on my SQLXMLBulkLoad3Class object.
> I do not have permissions to create folders, files, or interact in any
> way with the computer hosting SQL server. This means that the value of
> TempFilePath must be a UNC path that can be written to by the
> application performing bulk loads, and (at a minimum) read from by the
> computer hosting SQL server.
> I'd like to make access to this UNC path as restricted as possible. I
> am using SQL authentication, not Windows authentication.
> How can I tell what ID will try from the SQL Server computer to read
> from the TempFilePath?
> How much permission will this ID need in the directory? Read only?
> Read/write?
>

Sunday, March 25, 2012

Bulk Insert XML with IDENTITY Column

Hi ...
I have a program that will insert xml data into a table. When I add an IDENTITY column to the table then I get the following error:
... [Cannot insert the value NULL into column 'RecordId', table 'Alphanumericdata.dbo.MacgowanTestCust'; column does not allow nulls. INSERT fails.]
Reading another article here I have added the KeepIdentity(true) to my pISQLXMLBulkLoad object.
Below is the table, xml, xsd and code ...
Any comments are appreciated.
Thanks,
Chris

///////////////////////////////////////////////////
// The code
char progID[] = "SQLXMLBulkLoad.SQLXMLBulkload.3.0";
CLSID clsid;
wchar_t wide[80];
mbstowcs(wide, progID, 80);
CLSIDFromProgID(wide, &clsid);
ISQLXMLBulkLoad* pISQLXMLBulkLoad = NULL;
if(SUCCEEDED(CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_ISQLXMLBulkLoad, (void**)&pISQLXMLBulkLoad)))
{
hResult = pISQLXMLBulkLoad->put_ConnectionString(bstrConnect);
hResult = pISQLXMLBulkLoad->put_ErrorLogFile(bstrXmlErrorLogFile);
hResult = pISQLXMLBulkLoad->put_KeepIdentity((bool)TRUE);
hResult = pISQLXMLBulkLoad->Execute(bstrXmlSchemaFile, vXmlDataFile);
}

///////////////////////////////////////////////////
// xml data
<ROOT>
<Customers>
<CustomerID>1111</CustomerID>
<CompanyName>Sean Chai</CompanyName>
<City>NY</City>
</Customers>
<Customers>
<CustomerID>1112</CustomerID>
<CompanyName>Tom Johnston</CompanyName>
<City>LA</City>
</Customers>
<Customers>
<CustomerID>1113</CustomerID>
<CompanyName>Institute of Art</CompanyName>
</Customers>
</ROOT>

///////////////////////////////////////////////////
// xsc schema file
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:element name="Customers" sql:relation="MacgowanTestCust" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CustomerID" type="xsd:integer" sql:field="CustomerID" />
<xsd:element name="CompanyName" type="xsd:string" sql:field="CompanyName" />
<xsd:element name="City" type="xsd:string" sql:field="City" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

///////////////////////////////////////////////////
// table
CREATE TABLE [MacgowanTestCust] (
[RecordId] [int] IDENTITY (1, 1) NOT NULL ,
[CustomerID] [int] NOT NULL ,
[DataSourceId] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF_MacgowanTestCust_DataSourceId] DEFAULT ('OH'),
[CompanyName] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[City] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
PRIMARY KEY CLUSTERED
(
[RecordId]
) ON [PRIMARY]
) ON [PRIMARY]
GO

Hi ...

To allow SQL Server to set the IDENTITY column the KeepIdentity atribute needs to be set to false (KeepIdentity((bool)FALSE).

Thanks,

Chris


///////////////////////////////////////////////////
// The code
char progID[] = "SQLXMLBulkLoad.SQLXMLBulkload.3.0";
CLSID clsid;
wchar_t wide[80];
mbstowcs(wide, progID, 80);
CLSIDFromProgID(wide, &clsid);
ISQLXMLBulkLoad* pISQLXMLBulkLoad = NULL;
if(SUCCEEDED(CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_ISQLXMLBulkLoad, (void**)&pISQLXMLBulkLoad)))
{
hResult = pISQLXMLBulkLoad->put_ConnectionString(bstrConnect);
hResult = pISQLXMLBulkLoad->put_ErrorLogFile(bstrXmlErrorLogFile);
hResult = pISQLXMLBulkLoad->put_KeepIdentity((bool)FALSE);
hResult = pISQLXMLBulkLoad->Execute(bstrXmlSchemaFile, vXmlDataFile);
}

Bulk Insert XML with IDENTITY Column

Hi ...
I have a program that will insert xml data into a table. When I add an IDENTITY column to the table then I get the following error:
... [Cannot insert the value NULL into column 'RecordId', table 'Alphanumericdata.dbo.MacgowanTestCust'; column does not allow nulls. INSERT fails.]
Reading another article here I have added the KeepIdentity(true) to my pISQLXMLBulkLoad object.
Below is the table, xml, xsd and code ...
Any comments are appreciated.
Thanks,
Chris

///////////////////////////////////////////////////
// The code
char progID[] = "SQLXMLBulkLoad.SQLXMLBulkload.3.0";
CLSID clsid;
wchar_t wide[80];
mbstowcs(wide, progID, 80);
CLSIDFromProgID(wide, &clsid);
ISQLXMLBulkLoad* pISQLXMLBulkLoad = NULL;
if(SUCCEEDED(CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_ISQLXMLBulkLoad, (void**)&pISQLXMLBulkLoad)))
{
hResult = pISQLXMLBulkLoad->put_ConnectionString(bstrConnect);
hResult = pISQLXMLBulkLoad->put_ErrorLogFile(bstrXmlErrorLogFile);
hResult = pISQLXMLBulkLoad->put_KeepIdentity((bool)TRUE);
hResult = pISQLXMLBulkLoad->Execute(bstrXmlSchemaFile, vXmlDataFile);
}

///////////////////////////////////////////////////
// xml data
<ROOT>
<Customers>
<CustomerID>1111</CustomerID>
<CompanyName>Sean Chai</CompanyName>
<City>NY</City>
</Customers>
<Customers>
<CustomerID>1112</CustomerID>
<CompanyName>Tom Johnston</CompanyName>
<City>LA</City>
</Customers>
<Customers>
<CustomerID>1113</CustomerID>
<CompanyName>Institute of Art</CompanyName>
</Customers>
</ROOT>

///////////////////////////////////////////////////
// xsc schema file
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xsd:element name="Customers" sql:relation="MacgowanTestCust" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CustomerID" type="xsd:integer" sql:field="CustomerID" />
<xsd:element name="CompanyName" type="xsd:string" sql:field="CompanyName" />
<xsd:element name="City" type="xsd:string" sql:field="City" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

///////////////////////////////////////////////////
// table
CREATE TABLE [MacgowanTestCust] (
[RecordId] [int] IDENTITY (1, 1) NOT NULL ,
[CustomerID] [int] NOT NULL ,
[DataSourceId] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF_MacgowanTestCust_DataSourceId] DEFAULT ('OH'),
[CompanyName] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[City] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
PRIMARY KEY CLUSTERED
(
[RecordId]
) ON [PRIMARY]
) ON [PRIMARY]
GOHi ...
To allow SQL Server to set the IDENTITY column the KeepIdentity atribute needs to be set to false (KeepIdentity((bool)FALSE).
Thanks,
Chris

///////////////////////////////////////////////////
// The code
char progID[] = "SQLXMLBulkLoad.SQLXMLBulkload.3.0";
CLSID clsid;
wchar_t wide[80];
mbstowcs(wide, progID, 80);
CLSIDFromProgID(wide, &clsid);
ISQLXMLBulkLoad* pISQLXMLBulkLoad = NULL;
if(SUCCEEDED(CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_ISQLXMLBulkLoad, (void**)&pISQLXMLBulkLoad)))
{
hResult = pISQLXMLBulkLoad->put_ConnectionString(bstrConnect);
hResult = pISQLXMLBulkLoad->put_ErrorLogFile(bstrXmlErrorLogFile);
hResult = pISQLXMLBulkLoad->put_KeepIdentity((bool)FALSE);
hResult = pISQLXMLBulkLoad->Execute(bstrXmlSchemaFile, vXmlDataFile);
}

Wednesday, March 7, 2012

Bulk Insert and Decimal type

Hi there

I am trying to write a program which will bulk load data from a bcp file into a newly made database on the users PC.

I create the data from an existing DB using SQL-DMO BulkCopy.
I then load it into the users DB using "Bulk Insert " transact SQL.

It all works fine on SQL Server 2000. However on SQL Server 7.0 whenever the .bcp file is being loaded into a table with a field of type decimal, it throws an OLEDB stream error. Even when the .bcp file is empty.

I have tried exporting/importing the data as tab delimited and as native, but it seems to make no difference.

This has really got me stumped and I am running out of time. Can anyone help?

Thanks.

justinOK I just discovered from the Microsoft web site that there is a bug in SQL Server 7.0. Using Bulk Insert on a table that includes a default value for decimal or numeric data typed fields, throws an error.

There is no solution. It is incurable. The workaround is to "use bcp instead."

Programatically that would be an issue, so i will have to use DMO.

bulk insert accepting param

I cant figure out how to set the lastrow parameter to a var. This var would be sent in from a c# program. Below is what I'm trying to do but I get a syntax error on the @.lastRow.

is there a way to do this


ALTER PROCEDURE [dbo].[p_temp]
--@.lastRow int
AS
BEGIN
--SELECT <@.Param1, sysname, @.p1>, <@.Param2, sysname, @.p2>
BULK INSERT PHX_WCISFile
FROM 'C:\100.TXT'
WITH
(
FIRSTROW = 2,

LASTROW = @.lastRow;

MAXERRORS = 20,
FIELDTERMINATOR = '|',
ROWTERMINATOR = '\n'
)
END

I believe that BCP is unable to utilize parameters in that manner.

You could, however, create the entire BULK INSERT statement as a string and execute it using EXEC(). That would allow you to pass in parameters to substitute in the statement.

|||Arnie is correct. Unforunitely you will need to use dynamic SQL to do this.

Saturday, February 25, 2012

Bulk Insert

I am trying to Bulk Insert into Database from a program in VC++ on to sql2000 database.My code was successful if i use the credentials of SA user.
but i need to execute the same process with different user which has DBO privileges on the database in which i have the table.Can anyone tell me what privileges need to be granted for this dbo user to make bulk insert work.
thanks a lot for your help.
annaYou need insert privleges on the table, which are included for dbo. You should be "good to go" for everything you've described so far.

-PatP

Friday, February 24, 2012

Bulk import stored procedure

I am trying to devise the best way to perform the following:
Import/export word documents into and out of a table in SQL 2005 for a VB
program to access. The user of the program would be able to choose what doc
they want to import into the table thus the location of the file and name of
the file can change. I have made a simple inserst statement that works when I
explicitly state the file path, but I need a variable file path, but I can't
seem to find how to do this or if there is a better way. The following is
what I have come up with so far even thought I can't execute it to create the
stored procedure because it won't accept the @.doclink in the Bulk statement.
Anyone have ideas on how to do this or a better way?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_DocInsert]
(
@.PermitID int,
@.DocLink char(255),
@.Title nchar(100)
)
AS
BEGIN
SET NOCOUNT ON;
Insert into documents(doclink,title,created,createdby,doc)
Select @.doclink,@.title,getdate(),'John Doe',* from openrowset(bulk
@.doclink,single_blob,codepage='raw') as doc
END
GO"Joe S." <joecrew@.news.postalias> wrote in message
news:4E3401D2-24BA-41B6-ACFE-144D7D15772B@.microsoft.com...
>I am trying to devise the best way to perform the following:
> Import/export word documents into and out of a table in SQL 2005 for a VB
> program to access. The user of the program would be able to choose what
> doc
> they want to import into the table thus the location of the file and name
> of
> the file can change. I have made a simple inserst statement that works
> when I
> explicitly state the file path, but I need a variable file path, but I
> can't
> seem to find how to do this or if there is a better way. The following is
> what I have come up with so far even thought I can't execute it to create
> the
> stored procedure because it won't accept the @.doclink in the Bulk
> statement.
> Anyone have ideas on how to do this or a better way?
>
This isn't going to work in the general case. For this to work the SQL
Server must be able to access the file location. If the file is on the
client location, the pathing would be different and the server's account
wouldn't be able to open the file.
Instead of passing the file path to the procedure, pass the file bits. Just
declare the procedure to take a parameter of type varbinary(max) and pass
the file bits from VB. From VB load the file into a byte array and pass it
to through a SqlParameter.
David

Sunday, February 19, 2012

Bulk export/import of the table with nullable and computed fields

I have a program that is doing significant refresh of data in tables with nullable and computed (based on values of other fields of the sdame table) fields at least 2 times a week.

I found that it maybe the most efficient way to do this is to dump unchanged rows to flat files of the native format, to add changed rows to the same files,to truncate tables,to drop indices,to bulk insert from flat files and to restore indices.

Can somebody explain to me what value has to be assigned in the native format of the bulk export to the prefix of nullable field if it is null?

What will happened when you bulk insert without format file to the table with computed fields?

You would use a fmt file to define what should be imported. Here is an old post that should get you started.

http://groups.google.com/group/microsoft.public.sqlserver.programming/browse_thread/thread/6a02e9de79a21a81/50045cbec46f5a8f

|||

Dear oj,

Thank you for your attempt to help, but it is completely unrelated:

1.Guys are completely confused native data file format with char, I want to do native.

2.They are trying to skip identity field which I don't have.

3.They don't trying to create programmatically native format which I am trying to do.

4.I am talking about computed fields(from other fields of the same table.

5.They arte trying to skip identity field with fmt file which by default will be skipped anyway without fmt.

6.Maybe, this isn't relevant because bcp is the same, but I am using sql2k5 vs them sql2k

|||

I found solution on question 1:

In order to insert null to the nullable field you have to put -1 value to the prefix of that field in the native format datafile.

Regarding question 2 I found that without fmt file if you simply dumped content of the table in native format with computed fields

it will be succesfully bulk inserted to the table.

If you changed value of computed field it got ignored.

I am not sure if this has anything to do with caching of execution plans, but I truncated table and changed name of datafile and result the same.

Bulk Copy Program

Hi Techies,

I have a bcp which generates .txt file perfectly. I just wanted to know how can i generate a text file in distributed environment.

Assuming that my Sql Server is running in machine A. I wanted the bcp to generate in Machine B. What are the permission's i should give in order to generate it in Machine B.

Regards
--Tanveerthe user who executes the bcp-command would need write access. read access is usually handy to validate the file has been written but I don't think its required. In case machine B only has a filesystem, that user obviously needs filesystem write access. In case machine B is a sqlserver that user would need write access on the table the file is insert into.|||Thanks for the reply... I guess i have not mentioned that the above program would be executed through stored procedure. In this scenario what are the permission i should give and to which users.

--Tanveer|||Assuming bcp and the copy is executed using xp_cmdshell, the user is the user configured to run sqlserver (exec master..xp_cmdshell 'set'). If it's local user (only known to the server) you'll find it difficult to do the windows-copy. If it's a domain user it'll be easier to grant the write access. Do you have difficulty creating the .txt file using the stored procedure?

Bulk Copy Program

Hi,

can anyone show me a link to a bcp return values table please?

I have tried googling, but rather than getting info on lookup tables, I'm just getting articles on how to use it :(

tia,

don't know exactly what you want...

but here are the bcp switches at dos prompt

The bcp utility copies data between an instance of Microsoft? SQL Server? 2000 and a data file in a user-specified format.

Syntax

bcp {[[database_name.][owner].]{table_name | view_name} | "query"}
{in | out | queryout | format} data_file
[-m max_errors] [-f format_file] [-e err_file]
[-F first_row] [-L last_row] [-b batch_size]
[-n] [-c] [-w] [-N] [-V (60 | 65 | 70)] [-6]
[-q] [-C code_page] [-t field_term] [-r row_term]
[-i input_file] [-o output_file] [-a packet_size]
[-S server_name[\instance_name]] [-U login_id] [-P password]
[-T] [-v] [-R] [-k] [-E] [-h "hint [,...n]"]

Arguments

database_name

Is the name of the database in which the specified table or view resides. If not specified, this is the default database for the user.

Owner

Is the name of the owner of the table or view. owner is optional if the user performing the bulk copy operation owns the specified table or view. If owner is not specified and the user performing the bulk copy operation does not own the specified table or view, Microsoft? SQL Server? 2000 returns an error message, and the bulk copy operation is canceled.

table_name

Is the name of the destination table when copying data into SQL Server (in), and the source table when copying data from SQL Server (out).

view_name

Is the name of the destination view when copying data into SQL Server (in), and the source view when copying data from SQL Server (out). Only views in which all columns refer to the same table can be used as destination views. For more information on the restrictions for copying data into views, see INSERT.

Query

Is a Transact-SQL query that returns a result set. If the query returns multiple result sets, such as a SELECT statement that specifies a COMPUTE clause, only the first result set is copied to the data file; subsequent result sets are ignored. Use double quotation marks around the query and single quotation marks around anything embedded in the query. queryout must also be specified when bulk copying data from a query.

in | out | queryout | format

Specifies the direction of the bulk copy. in copies from a file into the database table or view. out copies from the database table or view to a file. queryout must be specified only when bulk copying data from a query. format creates a format file based on the option specified (-n, -c, -w, -6, or -N) and the table or view delimiters. If format is used, the -f option must be specified as well.

Note The bcp utility included with Microsoft SQL Server 6.5 does not support bulk copying into tables that contain the sql_variant or bigint data types.

data_file

Is the full path of the data file used when bulk copying a table or view to or from a disk. When bulk copying data into SQL Server, the data file contains the data to be copied into the specified table or view. When bulk copying data from SQL Server, the data file contains the data copied from the table or view. The path can have from 1 through 255 characters.

-m max_errors

Specifies the maximum number of syntax errors and compilation errors that can occur before the bulk copy operation is canceled. Each row that cannot be copied by bcp is ignored and counted as one error. If this option is not included, the default is 10.

Note The max_errors option does not apply to constraint checks (or to converting money and bigint data types).

-f format_file

Specifies the full path of the format file that contains stored responses from a previous use of bcp on the same table or view. Use this option when using a format file created with the format option to bulk copy data in or out. Creation of the format file is optional. After prompting you with format questions, bcp prompts whether to save the answers in a format file. The default file name is Bcp.fmt. bcp can refer to a format file when bulk copying data; therefore, reentering previous format responses interactively is not necessary. If this option is not used and -n, -c, -w, -6, or -N is not specified, bcp prompts for format information.

-e err_file

Specifies the full path of an error file used to store any rows bcp is unable to transfer from the file to the database. Error messages from bcp go to the user's workstation. If this option is not used, an error file is not created.

-F first_row

Specifies the number of the first row to bulk copy. The default is 1, indicating the first row in the specified data file.

-L last_row

Specifies the number of the last row to bulk copy. The default is 0, indicating the last row in the specified data file.

-b batch_size

Specifies the number of rows per batch of data copied. Each batch is copied to the server as one transaction. SQL Server commits or rolls back, in the case of failure, the transaction for every batch. By default, all data in the specified data file is copied in one batch. Do not use in conjunction with the -h "ROWS_PER_BATCH = bb" option.

-n

Performs the bulk copy operation using the native (database) data types of the data. This option does not prompt for each field; it uses the native values.

-c

Performs the bulk copy operation using a character data type. This option does not prompt for each field; it uses char as the storage type, no prefixes, \t (tab character) as the field separator, and \n (newline character) as the row terminator.

-w

Performs the bulk copy operation using Unicode characters. This option does not prompt for each field; it uses nchar as the storage type, no prefixes, \t (tab character) as the field separator, and \n (newline character) as the row terminator. Cannot be used with SQL Server version 6.5 or earlier.

-N

Performs the bulk copy operation using the native (database) data types of the data for noncharacter data, and Unicode characters for character data. This option offers a higher performance alternative to the -w option, and is intended for transferring data from one SQL Server to another using a data file. It does not prompt for each field. Use this option when you are transferring data that contains ANSI extended characters and you want to take advantage of the performance of native mode. -N cannot be used with SQL Server 6.5 or earlier.

-V (60 | 65 | 70)

Performs the bulk copy operation using data types from an earlier version of SQL Server. Use this option in conjunction with character (-c) or native (-n) format. This option does not prompt for each field; it uses the default values. For example, to bulk copy date formats supported by the bcp utility provided with SQL Server 6.5 (but no longer supported by ODBC) into SQL Server 2000, use the -V 65 parameter.

Important When bulk copying data from SQL Server into a data file, the bcp utility does not generate SQL Server 6.0 or SQL Server 6.5 date formats for any datetime or smalldatetime data, even if -V is specified. Dates are always written in ODBC format. Additionally, null values in bit columns are written as the value 0 because SQL Server versions 6.5 and earlier do not support nullable bit data.

-6

Performs the bulk copy operation using SQL Server 6.0 or SQL Server 6.5 data types. Supported for backward compatibility only. Use the -V option instead.

-q

Executes the SET QUOTED_IDENTIFIERS ON statement in the connection between the bcp utility and an instance of SQL Server. Use this option to specify a database, owner, table, or view name that contains a space or a quotation mark. Enclose the entire three-part table or view name in double quotation marks (" ").

-C code_page

Supported for backward compatibility only. Instead, specify a collation name for each column in the format file or in interactive bcp.

Specifies the code page of the data in the data file. code_page is relevant only if the data contains char, varchar, or text columns with character values greater than 127 or less than 32.

Code page value

Description

ACP

ANSI/Microsoft Windows? (ISO 1252).

OEM

Default code page used by the client. This is the default code page used by bcp if -C is not specified.

RAW

No conversion from one code page to another occurs. This is the fastest option because no conversion occurs.

<value>

Specific code page number, for example, 850.

-t field_term

Specifies the field terminator. The default is \t (tab character). Use this parameter to override the default field terminator.

-r row_term

Specifies the row terminator. The default is \n (newline character). Use this parameter to override the default row terminator.

-i input_file

Specifies the name of a response file, containing the responses to the command prompt questions for each field when performing a bulk copy using interactive mode (-n, -c, -w, -6, or -N not specified).

-o output_file

Specifies the name of a file that receives output from bcp redirected from the command prompt.

-a packet_size

Specifies the number of bytes, per network packet, sent to and from the server. A server configuration option can be set by using SQL Server Enterprise Manager (or the sp_configure system stored procedure). However, the server configuration option can be overridden on an individual basis by using this option. packet_size can be from 4096 to 65535 bytes; the default is 4096.

Increased packet size can enhance performance of bulk copy operations. If a larger packet is requested but cannot be granted, the default is used. The performance statistics generated by bcp show the packet size used.

-S server_name[\instance_name]

Specifies the instance of SQL Server to connect to. Specify server_name to connect to the default instance of SQL Server on that server. Specify server_name\instance_name to connect to a named instance of SQL Server 2000 on that server. If no server is specified, bcp connects to the default instance of SQL Server on the local computer. This option is required when executing bcp from a remote computer on the network.

-U login_id

Specifies the login ID used to connect to SQL Server.

Security Note When possible, use the -T option (trusted connection).

-P password

Specifies the password for the login ID. If this option is not used, bcp prompts for a password. If this option is used at the end of the command prompt without a password, bcp uses the default password (NULL).

Security Note NULL passwords are not recommended.

Security Note To mask your password, do not specify the -P option along with the -U option. Instead, after specifying bcp along with the -U option and other switches (do not specify -P), press ENTER, and bcp will prompt you for a password. This method ensures that your password will be masked when it is entered.

-T

Specifies that bcp connects to SQL Server with a trusted connection, using the security credentials of the network user. login_id and password are not required.

-v

Reports the bcp utility version number and copyright.

-R

Specifies that currency, date, and time data is bulk copied into SQL Server using the regional format defined for the locale setting of the client computer. By default, regional settings are ignored.

-k

Specifies that empty columns should retain a null value during the bulk copy operation, rather than have any default values for the columns inserted.

-E

Specifies that the values for an identity column are present in the file being imported. If -E is not given, the identity values for this column in the data file being imported are ignored, and SQL Server 2000 automatically assigns unique values based on the seed and increment values specified during table creation. If the data file does not contain values for the identity column in the table or view, use a format file to specify that the identity column in the table or view should be skipped when importing data; SQL Server 2000 automatically assigns unique values for the column. For more information, see DBCC CHECKIDENT.

-h "hint [,...n]"

Specifies the hint(s) to be used during a bulk copy of data into a table or view. This option cannot be used when bulk copying data into SQL Server 6.x or earlier.

Hint

Description

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

Sort order of the data in the data file. Bulk copy performance is improved if the data being 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 hint is ignored. The names of the columns supplied must be valid columns in the destination table. By default, bcp assumes the data file is unordered.

ROWS_PER_BATCH = bb

Number of rows of data per batch (as bb). Used when -b is not specified, resulting in the entire data file being sent to the server as a single transaction. The server optimizes the bulk load according to the value bb. By default, ROWS_PER_BATCH is unknown.

KILOBYTES_PER_BATCH = cc

Approximate number of kilobytes (KB) of data per batch (as cc). By default, KILOBYTES_PER_BATCH is unknown.

TABLOCK

A table-level lock is acquired for the duration of the bulk copy operation. This hint significantly improves performance because holding a lock only for the duration of the bulk copy operation reduces lock contention on the table. A table can be loaded concurrently by multiple clients if the table has no indexes and TABLOCK is specified. By default, locking behavior is determined by the table option table lock on bulk load.

CHECK_CONSTRAINTS

Any constraints on the destination table are checked during the bulk copy operation. By default, constraints are ignored. Note that the max_errors option does not apply to constraint checking.

FIRE_TRIGGERS

Specified with the in argument, any insert triggers defined on the destination table will execute during the bulk copy operation. If FIRE_TRIGGERS is not specified, no insert triggers will execute. FIRE_TRIGGERS is ignored for the out, queryout, and format arguments.


Remarks

Values in the data file being imported for computed or timestamp columns are ignored, and SQL Server 2000 automatically assigns values. If the data file does not contain values for the computed or timestamp columns in the table, use a format file to specify that the computed or timestamp columns in the table should be skipped when importing data; SQL Server automatically assigns values for the column.

Computed and timestamp columns are bulk copied from SQL Server to a data file as usual.

SQL Server identifiers, including database names, table or view names, logins, and passwords, can include characters such as embedded spaces and quotation marks. When you specify an identifier or file name at the command prompt that includes a space or quotation mark, enclose the identifier in double quotation marks (" "). Additionally, for owner, table, or view names that contain embedded spaces or quotation marks, you can either specify the -q option or enclose the owner, table, or view name in brackets ([ ]) inside of the double quotation marks.

For example, the Northwind database has the table Jane's Orders, which is owned by user Jane Doe. To bulk copy this table from the Northwind database to the Orders.txt file using the login Jane Doe and the password go dba, execute one of these commands:

bcp "Northwind.Jane Doe.Jane's Orders" out "Jane's Orders.txt" -c -q -U"Jane Doe" -P"go dba"

Security Note When possible, use the -T option (trusted connection).

bcp "Northwind.[Jane Doe].[Jane's Orders]" out "Jane's Orders.txt" -c -U"Jane Doe" -P"go dba"

To specify a database name that contains a space or quotation mark, you must use the –q option.

|||

I see!

thank you for that,

I run bcp and it fails, it returns the value 96 what does this mean?

how many other error message values are there?

is there somewhere I can list all error messages from bcp?

|||does bcp can be done if i want to import text files to a specific tables?|||

hi,

yes, probided that the text file

is delimited such as

CSV or (comma separated values)

and then importing them to their corresponding fields

in the table.

But,

if you are up to loading the the entire text file as a single

BLOb (binary large object)

this is not the tool.

regards,

joey

Bulk Copy Program

Hi,

can anyone show me a link to a bcp return values table please?

I have tried googling, but rather than getting info on lookup tables, I'm just getting articles on how to use it :(

tia,

don't know exactly what you want...

but here are the bcp switches at dos prompt

The bcp utility copies data between an instance of Microsoft? SQL Server? 2000 and a data file in a user-specified format.

Syntax

bcp {[[database_name.][owner].]{table_name | view_name} | "query"}
{in | out | queryout | format} data_file
[-m max_errors] [-f format_file] [-e err_file]
[-F first_row] [-L last_row] [-b batch_size]
[-n] [-c] [-w] [-N] [-V (60 | 65 | 70)] [-6]
[-q] [-C code_page] [-t field_term] [-r row_term]
[-i input_file] [-o output_file] [-a packet_size]
[-S server_name[\instance_name]] [-U login_id] [-P password]
[-T] [-v] [-R] [-k] [-E] [-h "hint [,...n]"]

Arguments

database_name

Is the name of the database in which the specified table or view resides. If not specified, this is the default database for the user.

Owner

Is the name of the owner of the table or view. owner is optional if the user performing the bulk copy operation owns the specified table or view. If owner is not specified and the user performing the bulk copy operation does not own the specified table or view, Microsoft? SQL Server? 2000 returns an error message, and the bulk copy operation is canceled.

table_name

Is the name of the destination table when copying data into SQL Server (in), and the source table when copying data from SQL Server (out).

view_name

Is the name of the destination view when copying data into SQL Server (in), and the source view when copying data from SQL Server (out). Only views in which all columns refer to the same table can be used as destination views. For more information on the restrictions for copying data into views, see INSERT.

Query

Is a Transact-SQL query that returns a result set. If the query returns multiple result sets, such as a SELECT statement that specifies a COMPUTE clause, only the first result set is copied to the data file; subsequent result sets are ignored. Use double quotation marks around the query and single quotation marks around anything embedded in the query. queryout must also be specified when bulk copying data from a query.

in | out | queryout | format

Specifies the direction of the bulk copy. in copies from a file into the database table or view. out copies from the database table or view to a file. queryout must be specified only when bulk copying data from a query. format creates a format file based on the option specified (-n, -c, -w, -6, or -N) and the table or view delimiters. If format is used, the -f option must be specified as well.

Note The bcp utility included with Microsoft SQL Server 6.5 does not support bulk copying into tables that contain the sql_variant or bigint data types.

data_file

Is the full path of the data file used when bulk copying a table or view to or from a disk. When bulk copying data into SQL Server, the data file contains the data to be copied into the specified table or view. When bulk copying data from SQL Server, the data file contains the data copied from the table or view. The path can have from 1 through 255 characters.

-m max_errors

Specifies the maximum number of syntax errors and compilation errors that can occur before the bulk copy operation is canceled. Each row that cannot be copied by bcp is ignored and counted as one error. If this option is not included, the default is 10.

Note The max_errors option does not apply to constraint checks (or to converting money and bigint data types).

-f format_file

Specifies the full path of the format file that contains stored responses from a previous use of bcp on the same table or view. Use this option when using a format file created with the format option to bulk copy data in or out. Creation of the format file is optional. After prompting you with format questions, bcp prompts whether to save the answers in a format file. The default file name is Bcp.fmt. bcp can refer to a format file when bulk copying data; therefore, reentering previous format responses interactively is not necessary. If this option is not used and -n, -c, -w, -6, or -N is not specified, bcp prompts for format information.

-e err_file

Specifies the full path of an error file used to store any rows bcp is unable to transfer from the file to the database. Error messages from bcp go to the user's workstation. If this option is not used, an error file is not created.

-F first_row

Specifies the number of the first row to bulk copy. The default is 1, indicating the first row in the specified data file.

-L last_row

Specifies the number of the last row to bulk copy. The default is 0, indicating the last row in the specified data file.

-b batch_size

Specifies the number of rows per batch of data copied. Each batch is copied to the server as one transaction. SQL Server commits or rolls back, in the case of failure, the transaction for every batch. By default, all data in the specified data file is copied in one batch. Do not use in conjunction with the -h "ROWS_PER_BATCH = bb" option.

-n

Performs the bulk copy operation using the native (database) data types of the data. This option does not prompt for each field; it uses the native values.

-c

Performs the bulk copy operation using a character data type. This option does not prompt for each field; it uses char as the storage type, no prefixes, \t (tab character) as the field separator, and \n (newline character) as the row terminator.

-w

Performs the bulk copy operation using Unicode characters. This option does not prompt for each field; it uses nchar as the storage type, no prefixes, \t (tab character) as the field separator, and \n (newline character) as the row terminator. Cannot be used with SQL Server version 6.5 or earlier.

-N

Performs the bulk copy operation using the native (database) data types of the data for noncharacter data, and Unicode characters for character data. This option offers a higher performance alternative to the -w option, and is intended for transferring data from one SQL Server to another using a data file. It does not prompt for each field. Use this option when you are transferring data that contains ANSI extended characters and you want to take advantage of the performance of native mode. -N cannot be used with SQL Server 6.5 or earlier.

-V (60 | 65 | 70)

Performs the bulk copy operation using data types from an earlier version of SQL Server. Use this option in conjunction with character (-c) or native (-n) format. This option does not prompt for each field; it uses the default values. For example, to bulk copy date formats supported by the bcp utility provided with SQL Server 6.5 (but no longer supported by ODBC) into SQL Server 2000, use the -V 65 parameter.

Important When bulk copying data from SQL Server into a data file, the bcp utility does not generate SQL Server 6.0 or SQL Server 6.5 date formats for any datetime or smalldatetime data, even if -V is specified. Dates are always written in ODBC format. Additionally, null values in bit columns are written as the value 0 because SQL Server versions 6.5 and earlier do not support nullable bit data.

-6

Performs the bulk copy operation using SQL Server 6.0 or SQL Server 6.5 data types. Supported for backward compatibility only. Use the -V option instead.

-q

Executes the SET QUOTED_IDENTIFIERS ON statement in the connection between the bcp utility and an instance of SQL Server. Use this option to specify a database, owner, table, or view name that contains a space or a quotation mark. Enclose the entire three-part table or view name in double quotation marks (" ").

-C code_page

Supported for backward compatibility only. Instead, specify a collation name for each column in the format file or in interactive bcp.

Specifies the code page of the data in the data file. code_page is relevant only if the data contains char, varchar, or text columns with character values greater than 127 or less than 32.

Code page value Description ACP ANSI/Microsoft Windows? (ISO 1252). OEM Default code page used by the client. This is the default code page used by bcp if -C is not specified. RAW No conversion from one code page to another occurs. This is the fastest option because no conversion occurs. <value> Specific code page number, for example, 850.

-t field_term

Specifies the field terminator. The default is \t (tab character). Use this parameter to override the default field terminator.

-r row_term

Specifies the row terminator. The default is \n (newline character). Use this parameter to override the default row terminator.

-i input_file

Specifies the name of a response file, containing the responses to the command prompt questions for each field when performing a bulk copy using interactive mode (-n, -c, -w, -6, or -N not specified).

-o output_file

Specifies the name of a file that receives output from bcp redirected from the command prompt.

-a packet_size

Specifies the number of bytes, per network packet, sent to and from the server. A server configuration option can be set by using SQL Server Enterprise Manager (or the sp_configure system stored procedure). However, the server configuration option can be overridden on an individual basis by using this option. packet_size can be from 4096 to 65535 bytes; the default is 4096.

Increased packet size can enhance performance of bulk copy operations. If a larger packet is requested but cannot be granted, the default is used. The performance statistics generated by bcp show the packet size used.

-S server_name[\instance_name]

Specifies the instance of SQL Server to connect to. Specify server_name to connect to the default instance of SQL Server on that server. Specify server_name\instance_name to connect to a named instance of SQL Server 2000 on that server. If no server is specified, bcp connects to the default instance of SQL Server on the local computer. This option is required when executing bcp from a remote computer on the network.

-U login_id

Specifies the login ID used to connect to SQL Server.

Security Note When possible, use the -T option (trusted connection).

-P password

Specifies the password for the login ID. If this option is not used, bcp prompts for a password. If this option is used at the end of the command prompt without a password, bcp uses the default password (NULL).

Security Note NULL passwords are not recommended.

Security Note To mask your password, do not specify the -P option along with the -U option. Instead, after specifying bcp along with the -U option and other switches (do not specify -P), press ENTER, and bcp will prompt you for a password. This method ensures that your password will be masked when it is entered.

-T

Specifies that bcp connects to SQL Server with a trusted connection, using the security credentials of the network user. login_id and password are not required.

-v

Reports the bcp utility version number and copyright.

-R

Specifies that currency, date, and time data is bulk copied into SQL Server using the regional format defined for the locale setting of the client computer. By default, regional settings are ignored.

-k

Specifies that empty columns should retain a null value during the bulk copy operation, rather than have any default values for the columns inserted.

-E

Specifies that the values for an identity column are present in the file being imported. If -E is not given, the identity values for this column in the data file being imported are ignored, and SQL Server 2000 automatically assigns unique values based on the seed and increment values specified during table creation. If the data file does not contain values for the identity column in the table or view, use a format file to specify that the identity column in the table or view should be skipped when importing data; SQL Server 2000 automatically assigns unique values for the column. For more information, see DBCC CHECKIDENT.

-h "hint [,...n]"

Specifies the hint(s) to be used during a bulk copy of data into a table or view. This option cannot be used when bulk copying data into SQL Server 6.x or earlier.

Hint Description ORDER (column [ASC | DESC] [,...n]) Sort order of the data in the data file. Bulk copy performance is improved if the data being 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 hint is ignored. The names of the columns supplied must be valid columns in the destination table. By default, bcp assumes the data file is unordered. ROWS_PER_BATCH = bb Number of rows of data per batch (as bb). Used when -b is not specified, resulting in the entire data file being sent to the server as a single transaction. The server optimizes the bulk load according to the value bb. By default, ROWS_PER_BATCH is unknown. KILOBYTES_PER_BATCH = cc Approximate number of kilobytes (KB) of data per batch (as cc). By default, KILOBYTES_PER_BATCH is unknown. TABLOCK A table-level lock is acquired for the duration of the bulk copy operation. This hint significantly improves performance because holding a lock only for the duration of the bulk copy operation reduces lock contention on the table. A table can be loaded concurrently by multiple clients if the table has no indexes and TABLOCK is specified. By default, locking behavior is determined by the table option table lock on bulk load. CHECK_CONSTRAINTS Any constraints on the destination table are checked during the bulk copy operation. By default, constraints are ignored. Note that the max_errors option does not apply to constraint checking. FIRE_TRIGGERS Specified with the in argument, any insert triggers defined on the destination table will execute during the bulk copy operation. If FIRE_TRIGGERS is not specified, no insert triggers will execute. FIRE_TRIGGERS is ignored for the out, queryout, and format arguments.


Remarks

Values in the data file being imported for computed or timestamp columns are ignored, and SQL Server 2000 automatically assigns values. If the data file does not contain values for the computed or timestamp columns in the table, use a format file to specify that the computed or timestamp columns in the table should be skipped when importing data; SQL Server automatically assigns values for the column.

Computed and timestamp columns are bulk copied from SQL Server to a data file as usual.

SQL Server identifiers, including database names, table or view names, logins, and passwords, can include characters such as embedded spaces and quotation marks. When you specify an identifier or file name at the command prompt that includes a space or quotation mark, enclose the identifier in double quotation marks (" "). Additionally, for owner, table, or view names that contain embedded spaces or quotation marks, you can either specify the -q option or enclose the owner, table, or view name in brackets ([ ]) inside of the double quotation marks.

For example, the Northwind database has the table Jane's Orders, which is owned by user Jane Doe. To bulk copy this table from the Northwind database to the Orders.txt file using the login Jane Doe and the password go dba, execute one of these commands:

bcp "Northwind.Jane Doe.Jane's Orders" out "Jane's Orders.txt" -c -q -U"Jane Doe" -P"go dba"

Security Note When possible, use the -T option (trusted connection).

bcp "Northwind.[Jane Doe].[Jane's Orders]" out "Jane's Orders.txt" -c -U"Jane Doe" -P"go dba"

To specify a database name that contains a space or quotation mark, you must use the –q option.

|||

I see!

thank you for that,

I run bcp and it fails, it returns the value 96 what does this mean?

how many other error message values are there?

is there somewhere I can list all error messages from bcp?

|||does bcp can be done if i want to import text files to a specific tables?
|||

hi,

yes, probided that the text file

is delimited such as

CSV or (comma separated values)

and then importing them to their corresponding fields

in the table.

But,

if you are up to loading the the entire text file as a single

BLOb (binary large object)

this is not the tool.

regards,

joey

Bulk Copy Program

Hi
I have a flat file that contains records many records. The first field
states the following for each record:
'I' = insert
'U' = update
'D' = delete
Is it possible for the BCP to achieve this objective? I know there be some
coding involved. I also noticed that there's an ODBC API that I can use in
..NET.
I'm not to sure but can BCP delete or update records?
Thanks in advance
Ross
Hi,
BCP can just copy the data into a file and then using a BCP IN loading back
to a table. Using this you cant dor a delete or update.
Thanks
Hari
MCDBA
"Ross Pellegrino" wrote:

> Hi
> I have a flat file that contains records many records. The first field
> states the following for each record:
> 'I' = insert
> 'U' = update
> 'D' = delete
> Is it possible for the BCP to achieve this objective? I know there be some
> coding involved. I also noticed that there's an ODBC API that I can use in
> ..NET.
> I'm not to sure but can BCP delete or update records?
> Thanks in advance
> Ross
>
>
|||Thanks for the info.
Ross
"Hari Prasad" <HariPrasad@.discussions.microsoft.com> wrote in message
news:4352FAE8-F9EE-4C92-8E89-E372F442538B@.microsoft.com...
> Hi,
> BCP can just copy the data into a file and then using a BCP IN loading
back[vbcol=seagreen]
> to a table. Using this you cant dor a delete or update.
> Thanks
> Hari
> MCDBA
>
> "Ross Pellegrino" wrote:
some[vbcol=seagreen]
in[vbcol=seagreen]

Sunday, February 12, 2012

building a program model

could someone show me how to go about modeling an object oriented data access application using SQL server and .Net. what I'm looking for is a somewhat extensive project (such as northwind traders) with which to play around and get some ideas. good links to look up would also be appreciated

Thread moved to SQL Server Data Access as this forum is about coding the CLR running inside SQL Server.

Niels