I'm trying to export the column names of all my DB tables to a csv file using bcp utility to be later merged with the data from the tables merged into one file with the retrieved column names as headers in the file.
I've copied the code in the answer from this question: export table to file with column headers (column names) using the bcp utility and SQL Server 2008. However I am getting this error:
Msg 214, Level 16, State 201, Procedure xp_cmdshell, Line 1 Procedure expects parameter 'no_output' of type 'varchar'.
Here's my code:
DECLARE @var NVARCHAR(MAX)
DECLARE curRunning
CURSOR LOCAL FAST_FORWARD FOR
SELECT name FROM sysobjects WHERE type = 'U'
OPEN curRunning
FETCH NEXT FROM curRunning INTO @var
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @Exec2 NVARCHAR(MAX),@sql1 VARCHAR(1000)
SET @sql1 ='"DECLARE @colnames VARCHAR(max);SELECT @colnames= COALESCE(@colnames + '+''','''+' , '''') + column_name from dbname.INFORMATION_SCHEMA.columns where TABLE_NAME='+@var+'; select @colnames;"'
SET @Exec2 = 'exec master.dbo.xp_cmdshell ''bcp '+@sql1+' queryout "C:\migration\HeadersOnly'+@var+'.csv" -c -k -T -t,'''
PRINT @Exec2
EXECUTE sp_executesql @Exec2
FETCH NEXT FROM curRunning INTO @var
END
CLOSE curRunning
DEALLOCATE curRunning