bcp append a line at the end of file - sql-server

I am trying to export a .txt file with bcp:
EXEC xp_CMDSHELL 'BCP "SELECT v.ItemId
, v.InventLocationId
, CAST(v.AvailPhysical AS int)
FROM D_R1.dbo.vwStockOnHand AS v
JOIN D_R1.DBO.vwProducts AS m
ON v.ItemId = m.ITEMID
WHERE NOT EXISTS
(SELECT 1
FROM D_R1.DBO.ExportExcludedFamilies AS magExport
WHERE magExport.REFRECID = m.FamilyRecId)
AND AVAILPHYSICAL > 0 AND v.PICKFROMZ = 1
ORDER BY v.InventLocationId, v.ITEMID"
queryout "C:\temp\1.txt" -c -t"|" -T -S D_R1'
All good, but i would need a last line in the file like "END OF FILE" and i am not able to figure it out ..
Can someone give me a hint on this?

1) First solution
SELECT CONVERT(VARCHAR(11), v.ItemId) AS ItemId
, v.InventLocationId
, CAST(v.AvailPhysical AS int)
, 1 AS Priority
FROM ...
UNION ALL
SELECT 'End of export file', NULL, NULL, 2 AS Priority
ORDER BY Priority, InventLocationId, ItemID
or
SELECT... ;
SELECT 'End of report' ;
or
SELECT... ;
PRINT 'End of report' ;
2) Second solution
bcp "the same SQL query", ...
echo End of export
echo command

Related

How to export Sybase table contents to a text file?

I want to export the tables with contents from a Sybase database to a text file. I am using Sybase ASE 15.5 and that can't be changed/upgraded.
I have tried using sp_tables and different Select commands, but they don't give the result I'm looking for.
The output format I'm looking for is something like this:
FIRST_TABLE
Column_1 Column_2 Column_3
Roger Male 51
Anne Female 46
SECOND_TABLE
Column_1 Column_2 Column_3
BMW German Car
Schwinn American Bicycles
etc.etc.
Create a view that generates the output you want and use bcp to copy the data from the view.
Consider the following table, view and data:
create table t1 (
k int not null,
v varchar(255) null)
go
create view v1 as
select
'k' as k,
'v' as v
union all
select
convert(varchar, k),
v
from
t1
go
insert into t1 (k, v) values (1, 'Line_1')
insert into t1 (k, v) values (2, 'Line_2')
insert into t1 (k, v) values (3, 'Line_3')
go
Check the data returned from the view, notice the column names are in the result set. They need to here. Ideally you would query against syscolumns, but there is no pivot statement in ASE, so you need to know the names in advance :-(
select * from v1
go
k v
1 Line_1
2 Line_2
3 Line_3
(4 rows affected)
Now copy the data from the view into the text file:
$ bcp <db_name>..v1 out v1.txt -c -U login_name -S server_name
Password:
Starting copy...
4 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total : 1 Average : (4000.0 rows per sec.)
$ cat v1.txt
k v
1 Line_1
2 Line_2
3 Line_3
Without using BCP
declare cur cursor for
select sc.name from sysobjects so, syscolumns sc
where so.id = sc.id and so.name = 'FIRST_TABLE'
order by sc.colid
go
declare #l_name varchar(30), #l_sql varchar(2000)
begin
open cur
fetch cur into #l_name
while ##sqlstatus = 0
begin
set #l_sql = #l_sql + '''' + #l_name + ''','
fetch cur into #l_name
end
close cur
deallocate cur
set #l_sql = 'select ' + substring(#l_sql,1,len(#l_sql)-1)
set #l_sql = #l_sql + ' union all select * from FIRST_TABLE'
exec (#l_sql)
end
go

Use findstr to replace null values with empty strings from sqlcmd command

I have an sqlcmd command which generates a csv file from a view. Is it possible to replace the NULL values to empty string using the findstr command?
Here is what I tried.
sqlcmd -S . -d SAMPLEDB -U sa -P pass -s"|" -W -Q "SET NOCOUNT ON SET ANSI_WARNINGS OFF select * from view_Table" > Sample.csv -h -1 | findstr /v /c:"NULL"
You can easily build the extraction SQL for each view using the system management views. This simple query:
SELECT v.[name]
,c.[name]
,c.[column_id]
,c.[is_nullable]
FROM sys.views V
INNER JOIN sys.columns C
ON V.[object_id] = C.[object_id];
will return everything we need to perform the task:
the view name
the column name
the column order
if the column is nullable
So, we need only to build the extraction SQL statements:
SELECT v.[name]
,'SELECT ' + DS.[definition] + ' FROM ' + v.[name]
FROM sys.views V
CROSS APPLY
(
SELECT STUFF
(
(
SELECT ',' + CASE WHEN c.[is_nullable] = 1 THEN 'ISNULL(' + c.[name] + ','''')' ELSE c.[name] END
FROM sys.columns C
WHERE V.[object_id] = C.[object_id]
ORDER BY c.[column_id]
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1
,1
,''
)
) DS ([definition]);
Depending on your SQL version you can reduce the code above - for example using IIF or STRING_AGG.
Also, you can add WHERE clause to filter the query for specific views.
Change the sql in the command string to select fields individually and do an ISNULL on them
select * from view_Table
To
select ISNULL([FIELD_1],''''),ISNULL([FIELD_2],'''') from view_Table
in you calling string
use the following code to build it and check to see the final statement runs
DECLARE #Temp VARCHAR(MAX)
SET #Temp = 'SELECT ISNULL([FIELD_1],''''),ISNULL([FIELD_2],'''') FROM view_Table'
SELECT #Temp

Convert SQL cmd statement to stored procedure

I have a .BAT file that contains a SQL statement. And I am not understanding how to convert this to a stored procedure.
This is the statement:
ECHO 2, MDCESS_TRAKG_NUM, 1 , 2, dropoff, 3, 4, ORDER_NUM, MERCHANT_ORDER_NUM, MASTER_BARCODE, BARCODE, SERIAL_NUM, 5, NET_WEIGHT, GROSS_WEIGHT, DIM_WEIGHT, DRIVER, TRAILER, DELIVERY_DATE >\\c:\\Manifest.txt
SQLCMD -S SQLSERVEREXPRESS -d MyDb -U sa -P triple -Q "SET NOCOUNT ON select 2 AS '2', ORDE_.billgrp AS ENSENDA_TRAKG_NUM, 1 AS '1', 2 AS '2', dropoff AS dropoff, 3 AS '3', 4 AS '4', orde_.ordernum AS ORDER_NUM, CASE WHEN (Left(daddr,11)='1955 VAUGHN' AND READYDATE=CONVERT(varchar(8),GETDATE(),112)) THEN 'TBLK_ATL_'+ DRV1NUM + '_' + REPLACE(CONVERT(varchar(8),GETDATE(),10) ,'-', '') ELSE left(orde_.ref, 18) END AS MERCHANGE_ORDER_NUM, GFIELD4 AS MASTER_BARCODE, CASE WHEN orde_.CLIENTNUM in ('CDSLOG45', 'NEWAGE20', 'ENATLPEL') THEN orde_.BILLGRP ELSE GFIELD2 END AS BARCODE, left(GFIELD3,30) AS SERIAL_NUM, 5 AS '5', isnull(INVNUM,0) AS NET_WEIGHT,isnull(INVNUM,0) AS GROSS_WEIGHT,isnull(INVNUM,0) AS DIM_WEIGHT, orde_.DRV1NUM AS DRIVER, orde_.CALLER AS TRAILER, CONVERT(VARCHAR(10), orde_.READYDATE, 120) AS DELIVERY_DATE FROM GENERICFIELDS FULL OUTER JOIN ORDE_ ON GENERICFIELDS.ORDERID=ORDE_.ORDERID FULL OUTER JOIN CLIENT ON ORDE_.CLIENTID=CLIENT.CLIENTID WHERE orde_.droptime is null and (GFIELD2 is not null or orde_.CLIENTNUM in ('CDSLOG45', 'NEWAGE20', 'ENATLPEL') ) and ORDE_.CANCELLED='0' and dispid in ('65')" -h-1 -b -o "\\ADV\SCANNERMANIFEST.TXT" -s "," -W
copy \\intro\Atlanta5\*.txt \\intro\My.txt /y /b
del \\intro\My.csv
ren \\intro\My.txt My.csv
Can you please tell me how I can convert this to a stored procedure?
You can use xp_cmdshell for executing all the copy, delete and rename after executing sql query
EXEC xp_cmdshell 'copy \\intro\Atlanta5\*.txt \\intro\My.txt /y /b';
To copy the data from sql server you can try bcp out utility like below:
bcp "SELECT * from table " queryout D:\BCP\output.txt -t, -c -T,-S SQLSERVER-d MyDb -U sa -P triple

Return data in pipe del format

I have the below example and trying to run a script in SQL to return data in required format. Just need some help with the "Header", "Data" and "Trailer" part.
H|Peter|Blum|2012/07/30|1
D|11399011005434578|Jason|Smith|8002235091083|0126531988|199 Lever Road Centurion Gauteng 0067|23.45|Your next payment is due on 2012/09/02|2012/07/29|Active
D|11399022005434578|Mike|Smith|8004235091083|0126531988|299 Lever Road Centurion Gauteng 0067|55.00|Your next payment is due on 2012/09/03|2012/06/29|Active
D|11399033005434578|Peter|Smith|8052235091083|0126531988|399 Lever Road Centurion Gauteng 0067|77.99|Your next payment is due on 2012/09/04|2012/05/29|Active
T|3
From the example you have provided I gather that you are trying to create a file.
You need to use a bcp utility to create files.
The way you would approach it is as follows:
CREATE TABLE ##FileData( ID INT IDENTITY, RowString VARCHAR( MAX ))
INSERT INTO ##FileData
SELECT 'H|Peter|Blum|2012/07/30|1'
INSERT INTO ##FileData
SELECT 'D|' + CONVERT( VARCHAR, Col1 ) + '|' + CONVERT( VARCHAR, Col2 ) + '|' + ....
FROM MyTable
/* Get record count */
DECLARE #RowCount = ( SELECT COUNT(*) FROM #FileData ) - 1
INSERT INTO ##FileData
SELECT 'T|' + CONVERT( VARCHAR, #RowCount )
EXEC master..xp_cmdshell
N'bcp "SELECT RowString FROM ##FileData" queryout C:\MyFile.txt -c -r\r\n -S[ServerName] -U[username] -P[password] '
You can add the Header and Trailer with UNION ALL:
SELECT {Query that generates Header}, 0 AS ord
UNION ALL
SELECT {Query that generates Data Rows}, 1 AS ord
UNION ALL
SELECT {Query that generates Trailer}, 2 AS ord
ORDER BY ord ASC

SQL Export Data from a Query to Use Tilde (~)

I have a basic Select query which brings me back a set of results (roughly around 100,000 records) which I currently have to export to .CSV Format , issue is I then have to remove the commas out of the results and replace it with a (~). Which I do using a file format application that I got off the web.
But I'm trying to go about making this automated (if possible) to save time. Like e.g run off a stored procedure that can do this for me export the file in a (~) format.
Does anyone have any tips how this stored procedure can be written or an pointers would be appreciated.
p.s I have tried to use the export wizard but it just just crashes due to too many records.
Expected Result
Test1~Test2~Test3
5~6~7
(sql Script which I am running)
select
'SPK' as [AGENCY_CODE], -- should be set to SPK
'OBCALL' as [MEDIA_CODE], -- should be set to OBCALL
isnull(c.salutation,'') as [TITLE],
isnull(c.otherName,'') as [FORENAME],
isnull(c.name,'') as [SURNAME],
isnull(c.attTXT64,'') as [STANDARDISED_NAME],
replace(isnull(c.addr1, ''), ',', '.') AS [BEST_ADDRESS_LINE_1],
replace(isnull(c.addr2, ''), ',', '.') AS [BEST_ADDRESS_LINE_2],
replace(isnull(c.addr3, ''), ',', '.') AS [BEST_ADDRESS_LINE_3],
replace(isnull(c.addr4, ''), ',', '.') AS [BEST_ADDRESS_LINE_4],
isnull(c.postCode,'') as [BEST_POSTCODE],
--'0' + isnull(c.phone1,'') as [TELEPHONE_N2O],-- should be populated with the spare field ORIG_TEL .
RIGHT('0' + CONVERT(VARCHAR(11), c.phone1), 11) as [TELEPHONE_NO],-- should be populated with the spare field ORIG_TEL .
convert(varchar(100),c.attDT03,120) as [DATE_TIMESTAMP],
isnull(c.attTXT10,'') as [SM_CONTACT_KEY],
isnull(c.attTXT89,'') as [ SM_ADDRESS_KEY],
isnull(c.attTXT11,'') as [ CAMPAIGN_IDENTIFIER],
isnull(c.attTXT12,'') as [ WAVE_ID],
isnull(c.attTXT13,'') as [OLDSTACK_NEWSTACK_FLAG],
isnull(c.attTXT14,'') as [MARKET_3_FLAG],
isnull(c.attTXT15,'') as [ADSL_2_FLAG],
isnull(c.attTXT16,'') as [FIBRE_FLAG],
isnull(c.attTXT17,'') as [LOAD_ID],
isnull(c.attTXT18,'') as [CONTACT_POINT_KEY],
isnull(c.attTXT19,'') as [DATA_POOL_URN],
isnull(c.attTXT20,'') as [EVENT_KEY],
isnull(c.attTXT21,'') as [BILLING_ACCOUNT_KEY] ,
isnull(c.attTXT22,'') as [CAMPAIGN_SOURCE] ,
isnull(c.attTXT23,'') as [CAMPAIGN_CODE] ,
isnull(c.attTXT24,'') as [ CMT_ROLE_KEY],
isnull(c.attTXT25,'') as [ CMT_LOCATION_KEY],
isnull(c.attTXT26,'') as [BILL_ACCNT_NUM],
isnull(c.attTXT27,'') as [BILLING_ACCOUNT_TYPE],
--All other fields are as per the import record values
--DATE/TIME_OF_CONTRACT to CONTRACT_END_DATE_SUPPLIER3
CASE WHEN dx.datetime IS NULL THEN convert(varchar,getdate(),120) ELSE CONVERT(varchar, dx.datetime, 120) END
as [DATE/TIMEOF CONTACT],
Case when uc.campaignid = 3 then 'CT001' when uc.campaignid = 22 then 'CT001' when uc.campaignid = 18 then 'CT011'
when uc.campaignid = 26 then 'CT013'
end as [CAMPAIGN_TYPE], -- map to BT/DATA/10.CAMPAIGN_Code (return CAMPAIGN_TYPE)
ISNULL(( CASE
WHEN dx.[Abandon] = 1 THEN 'OC039'
ELSE d.code END),'OC042')
AS [OUTCOME_CODE], -- populate with (OC001-OCxxx)
'TM' as [CHANNEL_MEDIA_CODE], --= (TBC)
isnull(c.email,'') as [EMAIL_ADDRESS], -- populate with EMAIL_ADDRESS
'' as [EMAIL_CONSENT], --= populate with EMAIL_CONSENT ***************
'' as [INBOUND_TELEPHONE_NUMBER], --*****************
'' as [COMPETITOR_SUPPLIER_1], -- CONT CONTRACT_END_DATE_SUPPLIER3 – poplulate ***********
'' as [PRODUCT_FROM_SUPPLIER1], -- populate ************
'' as [CONTRACT_START_DATE_SUPPLIER1], --*****
'' as [CONTRACT_END_DATE_SUPPLIER1], --*****
'' as [COMPETITOR_SUPPLIER2], --****
'' as [PRODUCT_FROM_SUPPLIER2], --****
'' as [CONTRACT_START_DATE_SUPPLIER2],-- *****
'' as [CONTRACT_END_DATE_SUPPLIER2], --*****
'' as [COMPETITOR_SUPPLIER3],-- ****
'' as [PRODUCT_FROM_SUPPLIER3],--****
'' as [CONTRACT_START_DATE_SUPPLIER3],-- *****
'' as [CONTRACT_END_DATE_SUPPLIER3],-- *****
--ORDER_NUM to NUMBER_OF_CALLS_MADE
isnull(c.attTXT02,'') as [ORDER_NUM], -- Captured by Operator
isnull(dx.duration,0) as [CALL_DURATION],
-------------dxi.talk as [CALL_DURATION], -- populate difference start/end time (seconds)******
isnull(c.attTXT09,'') as [WARMTH_RATING_NOW], -- captured by agent
isnull(c.attTXT80,'') as [WARMTH_RATING_FUTURE_CAMPAIGNS], -- captured by agent
isnull(dx.callid,'') [SOURCE_INTERACTION_ID],
-------------isnull(a.id,'') as [SOURCE_INTERACTION_ID], -- Tpoints unique call Identifier --- Activity ID *******
isnull(uc.callcount,'') as [NUMBER_OF_CALLS_MADE],
(select left(ethnicOrigin, 1)) as [CALL_CONSENT_VALIDATE_FLG], -- ******** gift aid status - first byte only
(select left(nationality, 5)) as [CALL_CONSENT_OUTCOME_CD], -- ******** nationality - first 5 bytes only
--QUESTION _1 to ANSWER_3
isnull(c.attTXT56,'') as [QUESTION_1], -- populate from SPARE_FIELD_28
isnull(c.attTXT51,'') as [ANSWER_1], -- captured by the agent if SPARE_FIELD_1 populated
isnull(c.attTXT29,'') as [QUESTION_2], -- poaddingpulate from SPARE_FIELD_28
isnull(c.attTXT67,'') as [ANSWER_2], -- captured by the agent if SPARE_FIELD_2 populated
isnull(c.attTXT37,'') as [QUESTION_3], -- populate from SPARE_FIELD_28
isnull(c.attTXT58,'') as [ANSWER_3] -- captured by the agent if SPARE_FIELD_3 populated
--isnull(c.attdt18,GETDATE()) as [Export_Date]
from
u_contact c with (nolock)
inner join u_campaigncontact uc with (nolock) on uc.contactid = c.id
inner join u_dispcode d with (nolock) on d.id = uc.resultcodeid
outer apply (select top 1 duration, callid,
case when outcome = 113 then 1 else 0 end [Abandon], [datetime] from dxi_cdrlog cdr where cdr.urn = c.id order by callid desc) dx
where
uc.campaignid in (3, 18, 22, 26) and d.dmc = 1 and c.created between DATEADD(week, -1, getdate()) AND getdate()
and c.importid > 0
Since you are using SQL-Server you could use the command line tool sqlcmd which comes with the package. You might have to re-run the installation of the SSMS to acticvate the option.
Open cmd.exe. When you use the sqlcmd command with the options
sqlcmd -S host -d dbname -U username -P password -W -s ~ -h -1 -Q "SET NOCOUNT ON;SELECT 1,'hello world'"
It should get you
1~hello world
-W removes blanks between the columns, -s ~ sets ~ as the column separator, -h -1 removes the header line at the top and -Q then expects the actual query statement as the next argument.
Obviously for the actual job you need to call your SELECt statement. It might be a good idea to define a view (myview) for the job and in the sqlcmd just do a SELECT * FROM myview since writing a multiline SQL command within a cmd environment is not so much fun .... Of course it is possible but it is not really worth the hassle.
The command should then redirect its output directly into a file like
set sql=SET NOCOUNT ON;SELECT * FROM myview
set scmd=sqlcmd -S host -d dbname -U username -P password -W -s ~ -h -1 -Q
%scmd% "%sql" > exportfile.csv

Resources