|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Sunday, September 26, 2010 10:23 PM
Points: 21,
Visits: 44
|
|
What I have is a simple table (in SQL 2005) that provides input to a mapping program. It has four columns. I'm trying to automate the process of exporting it to .CSV because I'm tired of importing the table into Access, then exporting it to .CSV. Here's what I need: "Addr","City","State","Zip" "123 Main St.","Glendale","CA","92203" ... ...
In other words, comma delimited, double-quoted data WITH a header row. I found a post that used a created view, but since I'm trying to do this from inside a STORED PROCEDURE, I can't create a view. I haven't figured out or found how to do this with a format file. If I knew how to drive the output I want backwards through bcp and create a format file, it might produce something that would make the output correct the other direction.
If this is possible using BCP, is there a GOOD primer on it? (I've read all the bad ones already) If not, does anyone have an idea how I can produce that file format?
Thanks!
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Sunday, September 26, 2010 10:23 PM
Points: 21,
Visits: 44
|
|
I figured out that I could "can" the views in advance, outside the stored procedure, but I'd still like to use BCP a lot better without so much hassle if possible.
So any good references on it, with lots of examples would be great!
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, November 02, 2012 2:30 PM
Points: 24,
Visits: 73
|
|
Did you get any replies to this topic? I, too, prefer to contain all the logic in a stored procedure rather than fuss with an SSIS package.
Colleen
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 7:36 PM
Points: 32,931,
Visits: 26,820
|
|
This will do it.
bcp "SELECT QUOTENAME('Address',CHAR(34))+','+QUOTENAME('City',CHAR(34))+','+QUOTENAME('State',CHAR(34))+','+QUOTENAME('ZIP',CHAR(34)) d UNION ALL SELECT QUOTENAME(AddressLine1,CHAR(34))+',' + QUOTENAME(City,CHAR(34))+',' + QUOTENAME(StateProvinceID,CHAR(34))+',' + QUOTENAME(PostalCode,CHAR(34)) FROM AdventureWorks.Person.Address" queryout "C:\Temp\Contacts.txt" -c -T -SPutYourServer\InstanceNameHere
Change the "PutYourServer\InstanceNameHere" to the actual name of you Server and Instance name. This particular code runs against the AdventureWorks database. You'll need to change the FROM clause for that and the column names to match the table you're drawing from.
This code also writes to "C:\Temp\Contacts.txt" on the server. You can change that to whatever UNC you want.
Last but not least, this uses a Trusted Conection with Windows Authentication. If you only have SQL Server Authentication, then I don't recommend using this because you'll need to include the user name and password in clear text. Instead, you'd need to use %1 and %2 as batch substitution variables in a batch file and provide them at run time.
If your system is properly locked down for it, you could to this from a stored procedure using xp_CmdShell in a stored procedure or just from a job on the server.
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 7:36 PM
Points: 32,931,
Visits: 26,820
|
|
hugh.hemington (5/20/2010) I figured out that I could "can" the views in advance, outside the stored procedure, but I'd still like to use BCP a lot better without so much hassle if possible.
So any good references on it, with lots of examples would be great!
Just one example is all that's needed... if it works.
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, November 02, 2012 2:30 PM
Points: 24,
Visits: 73
|
|
Perfect! I can put this in an SP and just schedule that.
SSIS is giving me conniptions ; I'd like to stay in my comfortable T-SQL world where I am master of the universe as long as I can.
Off to try it out...
Colleen
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, November 02, 2012 2:30 PM
Points: 24,
Visits: 73
|
|
:-( Didn't work. Getting this ...is too long. Maximum length is 128. I use a view for the data, which is short enough. It's getting the column names output that is causing me problems.
I'd use SSIS but I don't have time - I've been fiddling with this problem in SSIS for six weeks now, off and on, and keep tripping over one SSIS issue after another. At least the BCP error makes sense to me.
In SSIS I have to change the file names - they have to be the date the package is run or the date from a parameter I have to pass in. This is a piece of cake from a parameter in a stored procedure but is taking a while to work through in SSIS. i.e. I haven't gotten it to work yet.
ARGH!!!!
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Yesterday @ 9:39 AM
Points: 4,247,
Visits: 9,500
|
|
cmcc (11/1/2012) :-( Didn't work. Getting this ...is too long. Maximum length is 128. I use a view for the data, which is short enough. It's getting the column names output that is causing me problems.
I'd use SSIS but I don't have time - I've been fiddling with this problem in SSIS for six weeks now, off and on, and keep tripping over one SSIS issue after another. At least the BCP error makes sense to me.
In SSIS I have to change the file names - they have to be the date the package is run or the date from a parameter I have to pass in. This is a piece of cake from a parameter in a stored procedure but is taking a while to work through in SSIS. i.e. I haven't gotten it to work yet.
ARGH!!!!
Haven't seen you posting these questions in the SSIS forum
You could also, perhaps (if the columns are static), create a text file somewhere which contains only the column headings.
Then, after your BCP has run, use a simple DOS Copy to append one file to the other.
____________________________________________________________________________________________
Help us to help you. For better, quicker and more focused answers to your questions, consider following the advice in this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
If you are asking for help and your post does not contain a question, you should expect responses which do not contain any answers. Put a question mark in there somewhere - it's not rocket science.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 7:36 PM
Points: 32,931,
Visits: 26,820
|
|
cmcc (11/1/2012) :-( Didn't work. Getting this ...is too long. Maximum length is 128. I use a view for the data, which is short enough. It's getting the column names output that is causing me problems.
I'd use SSIS but I don't have time - I've been fiddling with this problem in SSIS for six weeks now, off and on, and keep tripping over one SSIS issue after another. At least the BCP error makes sense to me.
In SSIS I have to change the file names - they have to be the date the package is run or the date from a parameter I have to pass in. This is a piece of cake from a parameter in a stored procedure but is taking a while to work through in SSIS. i.e. I haven't gotten it to work yet.
ARGH!!!!
Post your BCP command for this because, as you can see, my command is larger than 128. What may be wrong is a very large column name that QUOTENAME is tripping over. There's a fairly easy fix for that but it would help to see the actually BCP command you're using.
Also... do any of the columns in the BCP command have a datatype larger than VARCHAR(128) or NVARCHAR(64)???
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Yesterday @ 4:59 PM
Points: 960,
Visits: 1,924
|
|
|
|
|