Solved

export union query results to text file

Posted on 2014-10-29
2
137 Views
Last Modified: 2014-10-29
sql server 2008

I need to export this query to an PI[PE DELIMITED text file ?
C:\SQL_Export\

from this sql ?

Is this possible:


insert into OneToOneResults (WWGSKU, MODELNUMBER, MODELNAME)
select WWGSKU, MODELNUMBER, MODELNAME
from (
  SELECT MIN(SKU) AS WWGSKU
       ,[MfrNumCondensed] AS MODELNUMBER
       ,[CompMfrName] AS MODELNAME
   FROM [QA].[dbo].[bkp_vsm]
   WHERE SKU > '' and [MfrNumCondensed] > '' and [CompMfrName] > '' and
         XRef_Comment NOT IN ('CHILD CROSSES', 'BRAND XREFS','BRAND FROM 1 TO 18')
   GROUP BY  
        [CompMfrName]
   ,[MfrNumCondensed]
   HAVING COUNT(SKU) = 1
UNION ALL
   SELECT MIN(SKU)
       ,[CompMfrNumCondensed]
       ,[VdrBrandName]
   FROM [QA].[dbo].[bkp_vsm]
   WHERE SKU > '' and [CompMfrNumCondensed] > '' and [VdrBrandName] > '' and
         XRef_Comment NOT IN ('CHILD CROSSES', 'BRAND XREFS','BRAND FROM 1 TO 18')
   GROUP BY  
        [VdrBrandName]
   ,[CompMfrNumCondensed]
   HAVING COUNT(SKU) = 1
)a
0
Comment
Question by:fordraiders
2 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
ID: 40411714
try this code:

DECLARE @ExportString VARCHAR(8000) 
DECLARE @ExportPath VARCHAR(1000) 
DECLARE @ExportName VARCHAR(1000) 
DECLARE @Server VARCHAR(1000) 
DECLARE @SQL VARCHAR(1000) 

SET @ExportServer = 'myServer' 
SET @ExportSQL = 'select WWGSKU, MODELNUMBER, MODELNAME
from (
  SELECT MIN(SKU) AS WWGSKU
       ,[MfrNumCondensed] AS MODELNUMBER
       ,[CompMfrName] AS MODELNAME
   FROM [QA].[dbo].[bkp_vsm]
   WHERE SKU > '' and [MfrNumCondensed] > '' and [CompMfrName] > '' and
         XRef_Comment NOT IN ('CHILD CROSSES', 'BRAND XREFS','BRAND FROM 1 TO 18')
   GROUP BY  
        [CompMfrName]
   ,[MfrNumCondensed]
   HAVING COUNT(SKU) = 1
UNION ALL
   SELECT MIN(SKU)
       ,[CompMfrNumCondensed]
       ,[VdrBrandName]
   FROM [QA].[dbo].[bkp_vsm]
   WHERE SKU > '' and [CompMfrNumCondensed] > '' and [VdrBrandName] > '' and
         XRef_Comment NOT IN ('CHILD CROSSES', 'BRAND XREFS','BRAND FROM 1 TO 18')
   GROUP BY  
        [VdrBrandName]
   ,[CompMfrNumCondensed]
   HAVING COUNT(SKU) = 1
)a' 

SET @FilePath = 'C:\SQL_Export\' 
SET @FileName = 'ExportPipe.tmp' 

SET @ExportString = 'bcp "' + @ExportSQL + '" queryout '; 

SET @ExportString = @ExportString + @ExportPath + @ExportName; 

SET @ExportString = @ExportString + ' -S ' + @ExportServer; 

SET @ExportString = @ExportString + ' -T -c -t^| -U username -P password'; 

SELECT @ExportString ; 

Open in new window

0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 40412511
Thanks very much !
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question