문제

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