Domanda

declare @sql varchar(8000)
select @sql = 'bcp "exec "'+@dbname+'"..myproc" queryout "'+@filepath+'text1.txt" -c -t "|" -r \n -T -S' + @@servername
exec master..xp_cmdshell @sql

I'm using above query to write myproc procedure output to pipeline delimited text file. It is working fine except output file doesn't have column header.

How to add column header without making any change inside procedure code.

È stato utile?

Soluzione

This works:

-- Create Temp Table 
CREATE TABLE #TMP_TABLE(COL1 varchar(50), COL2 varchar(50), COL3 varchar(100));

-- Insert Stored Proc Data in Temp Table
INSERT INTO #TMP_TABLE
EXEC dbo.myproc;

-- Select Column name + Date (Use ' not " for String delimiter)
SELECT 'COL1', 'COL2', 'COL3'
UNION ALL
SELECT * FROM #TMP_TABLE;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top