Solved

export union query results to text file

Posted on 2014-10-29
2
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 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