Long story short we have a database here that manages some employee data such as email, first name, last name, etc. Our company bought into this SAP Based expense report system :| that needs an export of our employee based data in a very strange format. Without getting into too many details the export of this data needs a total of 137 columns, with many of these columns having an empty value.
Simple I put together a query that basically pulled this information from our database, and I set some constants to what was needed. It's not relevant what this query is in this question, its simply a SELECT
statement that pulls some data.
I then needed to export this out on a daily basis with a specific file name and with a pipe delimited format, something to this effect:
--employee export
DECLARE @FileName varchar(500)
SET @FileName = (SELECT '\\someFileServer\Public\someFolder\employee_p06010603ace_305_202105_' + REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(19), CONVERT(DATETIME, getdate(), 112), 126), '-', ''), 'T', ''), ':', '') + '.txt')
DECLARE @sql varchar(8000)
DECLARE @header varchar(8000)
SET @sql= 'bcp "exec [MyDBServer].[MyDbName].dbo.ConcurEmployeeExport" queryout ' + @FileName + ' -c -T -t "|"'
Exec master..xp_cmdshell @sql
Perfect, I get rows like this:
305|Jon| |Doe|10217487|10217487@t|
305|Steve| |Smith|10217522|10217522@t|
With about 130 more columns, didn't want to display all of this since it is irrelevant. I thought I was done until the implementation coordinator said yes looks good except the first row needs to start with this strange row with some values.
Meaning the resultant query that I have should have one additional row with some values in it..simple I thought I would union what they wanted..and so I did.
Except the UNION ALL
I had to put in some additional empty values to match the number of columns in my query. The issue is when I run the export I end up with a bunch of empty columns that are pipe delimited, I end up with this:
100|0|SSO|UPDATE|EN|N|N| | | | | | | | | | | | | | | | |
Imagine that going on for about 120 more columns. I submitted this to them and they said well everything looks good except your first row. We just need the first 7 values, that is they only want this:
100|0|SSO|UPDATE|EN|N|N|
But I cannot do that with my UNION ALL, as my union all requires the same number of columns from both queries. So I thought I could somehow after generating this file just replace the |'s in the first line after the last N
from above.
Is this possible without having to write another app / interface to do this? I want to keep what I have but only modify that first row such that the result I get is fixed, currently here is a sample of three rows:
100|0|SSO|UPDATE|EN|N|N| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
305|Jon| |Doe|11111|11111@t| |[email protected]|en_US|USA| |TK_Symbolic|USD| |Y|USA|IS|0000202105|0120|CC|371000000| | | |030257| | | | | | | | | | | | |030257| | | |USA0000202105|N|N| | |N|N|N|N|N|N|N| | | | | |10217495| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |EOL
305|Steve| |Smith|22222|22222@t| |[email protected]|en_US|USA| |TK_Symbolic|USD| |Y|USA|IS|0000202105|0120|CC|371000000| | | |030177| | | | | | | | | | | | |030177| | | |USA0000202105|N|N| | |N|N|N|N|N|N|N| | | | | |10217495| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |EOL
I need to get it to this:
100|0|SSO|UPDATE|EN|N|N|
305|Jon| |Doe|11111|11111@t| |[email protected]|en_US|USA| |TK_Symbolic|USD| |Y|USA|IS|0000202105|0120|CC|371000000| | | |030257| | | | | | | | | | | | |030257| | | |USA0000202105|N|N| | |N|N|N|N|N|N|N| | | | | |10217495| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |EOL
305|Steve| |Smith|22222|22222@t| |[email protected]|en_US|USA| |TK_Symbolic|USD| |Y|USA|IS|0000202105|0120|CC|371000000| | | |030177| | | | | | | | | | | | |030177| | | |USA0000202105|N|N| | |N|N|N|N|N|N|N| | | | | |10217495| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |EOL
Notice the first row how I removed all those |'s. Ideally I would like to do this in maybe the query that exports the file. Something to the effect of removing all of this | | | on the first row after the export?
The first row is static, generated using:
SELECT
'100' AS [Transaction Type], --1
'0' AS [Error Threshold], --2
'SSO' AS [Password Generation], --3
'UPDATE' AS [Existing Record Handling], --4
'EN' AS [Language Code], --5
'N' AS [Validate Expense Group], --6
'N' AS [Validate Payment Group], --7
Unfortunately, my brain keeps telling me this is not a good idea / is not possible. I know I can easily do this in .net but I'd hate to have yet another program sitting out there doing these sorts of things.