Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Extract to a file Expand / Collapse
Author
Message
Posted Monday, September 14, 2009 9:41 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 20, 2010 10:31 AM
Points: 23, Visits: 62
Hello,
Has anyone ever tried to run a query to get information from SQL 2005 and put the results to a file? I have address information in a table and I want to create a delimited file for label printing with the address information.

So, I would like to run a script on my local machine that gets the address information and puts it to a CSV '|' [Pipe] delimited file. Then I can email the file with column headings to a person to do a mail merge for label creation.

If not the what is the best way to do this?

Thank you,
Patrick
Post #787565
Posted Monday, September 14, 2009 10:02 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 6:09 PM
Points: 4,000, Visits: 6,058
If you can run your script from SMSS, try this:

Tools\Options\Query Results\SQL Server\Results to Text ... then choose your options.

At least that's what it is in 2008. I can't remember if that's it exactly in 2005, but it's similar. Then select output to text when you run your script in SMSS.

Also you can use BCP to execute a query, and BCP options can control the output format.


__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #787583
Posted Monday, September 14, 2009 10:45 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, May 30, 2014 6:27 PM
Points: 2,808, Visits: 7,175
Another way to do this would be to create a SSIS package that can extract the file and write to a csv file, you can even get the SSIS package to email the file once it has been created, the wizard will be able to get you started on this or check out BOL.
Post #787611
Posted Monday, September 14, 2009 10:55 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 20, 2010 10:31 AM
Points: 23, Visits: 62
Where could I find more information on 'BoL'?

Also, Besides the ways discussed previously; Could I do this through report manager?

Just curious, then the client could run a label when ever they want.

Post #787618
Posted Monday, September 14, 2009 11:05 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, May 30, 2014 6:27 PM
Points: 2,808, Visits: 7,175
Bol = books on line = SQL sever help . search for SSIS tutorials to get you started.

If you want a way for the client to run this automaically then (assuming they are using Excel or Word to do the mail merge) you can create a database connection via ODBC to get the data from the database.

Post #787624
Posted Monday, September 14, 2009 3:22 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 20, 2010 10:31 AM
Points: 23, Visits: 62
Hello,
As anyone used SaveDelimitedColumns or SaveDelimitedColumnsNullQuoted?
These are some stored procedures that can to exactly what I am looking for but I am having a hard time getting fields with mixed data into the CSV file.

Thank you,
PN.
Post #787781
Posted Monday, September 14, 2009 5:50 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, August 23, 2013 1:28 PM
Points: 10, Visits: 222
You could also try using the command line with BCP. Here's one for the AdventureWorks DB.

At the command line cd to C:\Program Files\Microsoft SQL Server\90\Tools\Binn

Then type
bcp "SELECT CONT.FirstName,C
ONT.LastName,ADDR.AddressLine1,ADDR.AddressLine2,ADDR.City,SP.StateProvinceCode,
ADDR.PostalCode FROM AdventureWorks.HumanResources.Employee EMP JOIN AdventureWo
rks.HumanResources.EmployeeAddress EA ON EMP.EmployeeID = EA.EmployeeID JOIN Adv
entureWorks.Person.Address ADDR ON EA.AddressID = ADDR.AddressID JOIN AdventureW
orks.Person.StateProvince SP ON ADDR.StateProvinceID = SP.StateProvinceID JOIN A
dventureWorks.Person.Contact CONT ON CONT.ContactID = EMP.ContactID ORDER BY CO
NT.LastName" queryout C:\Users\YourDesktop\Desktop\fileexport.txt -t "|" -S Server -T -c

You'll have to modify below for your server,database,query,authentication type etc.

You'll also want to checkout the rest of the parameters to tailor it specifically for your needs.
Post #787833
Posted Tuesday, September 15, 2009 9:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 20, 2010 10:31 AM
Points: 23, Visits: 62
Hello,
I got it to work with the Free ware: SaveDelimitedColumns. Works pretty nice. I can put it on the network drive and the client pick it up.

I tried the BCP and recieved a bunch of errors. Probably missing something to turn on.

I thank you all for the ideas and I have gotten good information pieces from you.

Best regards.
Post #788291
Posted Tuesday, September 15, 2009 11:25 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, October 6, 2009 4:10 PM
Points: 85, Visits: 182
Patrickn,

I'm doing the same thing now with my project. In RS, I link to my datasource and insert my query and I do all the formatting I need in there to make it look pretty for the users. Then I have SSIS packages that execute the report (or CSV) on regular intervals to a reports server. And you can also store it on a network drive.

-M
Post #788391
Posted Tuesday, September 15, 2009 11:30 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 20, 2010 10:31 AM
Points: 23, Visits: 62
I be interested in seeing this a liitle closer. Where did you dig up the research? How do you handle parm data (parameters passed into the script)?

The thing I worked on is a quick script to run on demand.
Post #788393
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse