Beruflich Dokumente
Kultur Dokumente
com
Page 1 of 3
Search:
Go
Active Forum Topics | Popular Articles | All Articles by Tag | SQL Server Books | About
Offering custom-written SQL Server classes focusing on real-world expertise. Classes never cancelled.
SubscribetoSQLTeam.com
Weekly SQL Server newsletter with articles, forum posts, and blog posts via email:
This article demonstrates how to programmatically control bcp to generate text files. It uses dynamic SQL and xp_cmdshell to execute a call to bcp from within Query Analyzer. In a previous article I showed you how to use BULK INSERT to import data stored in a text file into a table in SQL Server. BULK INSERT makes an "under-the-hood" call to bcp (Bulk Copy Program) to import the data. bcp is a command-line utility that can be used to import/export data in either character or native SQL Server format. Most developers use DTS (Data Transformation Services) to import/export data, but there are times when bcp is a more convenient approach. In this article I want to show you how to programmatically use bcp and xp_cmdshell to create a text file based on data stored in SQL Server. The approach shown here is used on a project I am currently working on to create text files during a nightly batch process. The batch process uses a stored procedure to accumulate daily transactions for EDI processing. More specifically, it retrieves and formats the daily transactions and then exports them to a text file which is ftp'd to the target vendors. The procedure creates several text files, which is why the approach shown in this article is more efficient than the one discussed in OSQL: Storing result of a Stored Procedure in a file
Subscribe
SQLTeam.com Articles via RSS SQLTeam.com Weblog via RSS
- Advertisement -
bcp in Action
Let's start off with a simple bcp call so you can see how it works. If you want to see the full syntax for bcp take a look the topic: bcp Utility, in Books Online (BOL). The following command exports the data stored in the authors table in the pubs database to authors.txt.
To execute the statement, go to the Command Prompt and type it in as shown replacing "garth" and "pw" with a valid login/password. All we are doing is passing arguments to the bcp executable. The first argument is the SELECT statement that specifies the source data. The database name is referenced in the SELECT because by default bcp will try to run the statement in the default database associated with the "garth" login. If the default database associated with the login does not contain the target object(s), an error message is generated. The second argument (queryout) tells bcp the direction of data flow. In this case it indicates the data is flowing out of SQL Server using a query to specify the source data. The third argument (authors.txt) specifies the file that will store the data. The file will be created in the directory in which the command is executed if no other is specified. The fourth and fifth arguments (-U and -P) specify the login and password. The arguments are casesensitive, so make sure they are capitalized. You should know that the -T argument can be used in place of -U and -P when using a trusted connection. The last argument (-c) specifies the format of the data coming out of SQL Server. When -c is used the data is stored in character format using the tab character as the column separator and the newline character as the row terminator. After you execute the statement open authors.txt in Notepad and take a look at the format.
Become a Microsoft Certified Master (MCM) How do senior IT professionals differentiate themselves from the competition? The MCM program, through intensive training from world-class experts, plus Or call 866-462-8388 extensive written and lab(US and Canada) based testing, certifies only the most qualified candidates. As an MCM you'll join an elite community of Microsoft software and technology experts, and you'll be recognized as one of the top Microsoft technology experts in the world.
SQLShare.comVideos
Introduction to Deploying SSIS Packages
In this session, you'll learn the basics on how to deploy an SSIS package to the package store. You'll learn the benefits of whether to use the MSDB database or file system to store your packages and how to use configuration files to simplify migrations.
http://www.sqlteam.com/article/exporting-data-programatically-with-bcp-and-xp_cmdsh... 8.6.2010
Page 2 of 3
The GETDATE() function is used to determines today's date, CONVERT changes the date into a more friendly format (e.g., 06/30/01) and REPLACE replaces the "/" characters with a "-". Once the file name is created, it is concatenated with the bcp command to create a valid call. The following shows how this is done.
DECLARE @FileName varchar(50), @bcpCommand varchar(2000) SET @FileName = REPLACE('c:\authors_'+CONVERT(char(8),GETDATE(),1)+'.txt','/','-') SET @bcpCommand = 'bcp "SELECT * FROM pubs..authors ORDER BY au_lname" queryout "' SET @bcpCommand = @bcpCommand + @FileName + '" -U garth -P pw -c' PRINT @bcpCommand -- Results -bcp "SELECT * FROM pubs..authors ORDER BY au_lname" queryout "c:\authors_06-30-01.txt" -U garth -P pw -c
Resources
SQL Server Resources
Advertise on SQLTeam.com SQL Server Books SQLTeam.com Newsletter Write for SQLTeam.com Contact Us About the Site
EXEC master..xp_cmdshell 'dir' -- Results (partial) -output -----------------------------Volume in drive C has no label. Volume Serial Number is 2876-AD19 NULL Directory of C:\WINNT\system32 NULL 06/27/01 08:38p <DIR> 06/27/01 08:38p <DIR> ...
. ..
The operating system command "dir" is executed and the results are shown in the Results pane of Query Analyzer. Note that I referenced the master database in the EXEC statement. To execute an extended stored procedure that starts with "xp_", either the master database must be active or referenced by EXEC. There are a few extended stored procedures that start with the "sp_" prefix. Those procedures, like all system stored procedures, can be executed without specifying the database because of the default behavior of SQL Server. When you execute a stored procedure that starts with "sp_", SQL Server looks for the procedure in the master database first. If the procedure is not found in master, it looks in the active database. If you have a stored procedure that you want to access from all your databases, create it in master and use a name that includes the "sp_" prefix. To create the data file we simply execute the command stored in @bcpCommand with xp_cmdshell. The following shows all the statements needed to create the file.
DECLARE @FileName varchar(50), @bcpCommand varchar(2000) SET @FileName = REPLACE('c:\authors_'+CONVERT(char(8),GETDATE(),1)+'.txt','/','-') SET @bcpCommand = 'bcp "SELECT * FROM pubs..authors ORDER BY au_lname" queryout "' SET @bcpCommand = @bcpCommand + @FileName + '" -U garth -P pw -c' EXEC master..xp_cmdshell @bcpCommand -- Results -output --------------------NULL Starting copy...
http://www.sqlteam.com/article/exporting-data-programatically-with-bcp-and-xp_cmdsh... 8.6.2010
Page 3 of 3
NULL 23 rows copied. Network packet size (bytes): 4096 Clock Time (ms.): total 61 Avg NULL
In the nightly batch procedure this code is referenced multiple times to create the various text files. For each reference, the SELECT is modified per the target tables and a new file name is generated.
Discuss this article: 23 Comments so far. Print this Article. This page has been read 245,460 times.
If you like this article you can sign up for our newsletter. We send it out each week that we post a new article. There's an opt-out link at the bottom of each newsletter so it's easy to unsubscribe at any time. Email Address:
Subscribe
Email This Subscribe to this feed Kick it Save to del.icio.us View blog reactions
RelatedArticles
Using BULK INSERT to Load a Text File (19 March 2001)
OtherRecentForumPosts
Need to hard code 2 customers (4 Replies) Special Character Issue (2 Replies) Export to Excel (495 Replies) need help for a query that contains variables (2 Replies) Trigger DELETED table columns-> temptablevariable (2 Replies) complicated Select (10 Replies) Login failed for user ... Please, what am I doing (4 Replies) Sum/Ratio of other Sums (6 Replies)
http://www.sqlteam.com/article/exporting-data-programatically-with-bcp-and-xp_cmdsh... 8.6.2010