Hi i have a work task to output data into a text file but each row does not have the same meaning ( apologies if this isn't clear but im finding it hard to articulate my problem)
Basically a very simplistic example is below where i need to use data form both the policy and drivers table and output into a single text file in the format shown. Note columns in policy table means something different to columns in drivers table but the rows from both have a common column polnum that is used to group the records in the text file. Also my full task contains data with lots of polnums and lots more tables
So my question is, how do i go about creating a text file in this way? i know there are various ways of outputting full sql tables to text but for this i will need to somehow handle each row output differently
any help on how i could do this would be much appreciated
Can you post usable data, such as #policy and #drivers CREATE TABLE and INSERT statements, rather than just a "splat" of data on the screen? (We can't write code against a "splat" )
Are those empty columns there to align with the number of columns in the drivers rows, or does a policy row always have a max of 4 values (p1, p2, p3, p4)?
What if there are 5 or 6 policy values (p5, p6)?
Do drivers only ever have a, b, and c columns? Are there ever d or more?
This will gen the results you want. You would just need to output this result to a text file using, for example, bcp or perhaps sqlcmd.
;WITH cte_polnums AS (
SELECT DISTINCT polnum
FROM #policy_table
)
SELECT string
FROM (
SELECT polnum, 0 AS drivenum,
polnum + '|policy_row|' + (
SELECT ISNULL(col_a, ' ') + '|'
FROM #policy_table
WHERE polnum = cp.polnum
FOR XML PATH(''), TYPE).value('.', 'varchar(max)') AS string
FROM cte_polnums cp
UNION ALL
SELECT polnum, drivenum,
polnum + '|driver_row|' + ISNULL(col_x, ' ') + '|' + ISNULL(col_y, ' ') + '|' + ISNULL(col_z, ' ') + '|' AS string
FROM #drivers_table
) AS derived
ORDER BY polnum, drivenum
Apologies . i might not have not explained my problem too well. The output is supposed to represent data rows from a variety of tables ( in my real life scenario) e.g a main policy table , driver table, vehivle table,address table . and there is no concept of columns in the text output. The idea is that each row in the text output contains different types of data values seprated by a pipe dilmiter but have a value in common linking rows together. each row type can have different number of fields
XML provides a "gimmick" to do data concatenation across multiple rows, usually related rows.
That technique / "trick" is not needed in the second query because all the values are in a single row; XML is really only needed when combining multiple rows.