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.

有帮助吗?

解决方案

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top