Sunday, February 19, 2012

How to bring out column names with bcp?

Hello, everyone:

I used bcp to generate a txt file as:

EXEC master..xp_cmdshell 'bcp "SELECT * FROM Test..Orders" queryout "c:/test.txt " -U tester -P tester -c'

It works fine except without column names of table. Does any one have idea that can bring out column names from table?

Thanks

ZYTColumn names is not one of the arguments of bcp.

You could do a "select * from <table> where 0=1" and save the output to a txt file, or script out the table schema and carry that across to the new location.|||Or you could use a union query with the column headings as literal strings in the row.|||Or you could use a union query with the column headings as literal strings in the row.I was going to suggest that. The main problem is that you then need to convert all data to character... which then means you need to be a little clever if you need to order your export for whatever reason.

http://weblogs.sqlteam.com/brettk/archive/2005/04/13/4395.aspx

I thought Mladen on SQLTeam had blogged something more sophisticated but I can't find it.|||I was going to suggest that. The main problem is that you then need to convert all data to character... which then means you need to be a little clever if you need to order your export for whatever reason.

http://weblogs.sqlteam.com/brettk/archive/2005/04/13/4395.aspx

I thought Mladen on SQLTeam had blogged something more sophisticated but I can't find it.

I am honored|||Lol - no offense. I mean I think he had some complicated automation code - I've never tried it. My own method, as it happens, is virtually identical to yours.|||Never [played with it, but my guess is you could use com automation..good luck|||This the way I export my "headers" in specific situation but you can modify it so it works for you as well.

DECLARE @.tmp varchar(1000), @.fld_list varchar(8000), @.loop int, @.i int, @.select varchar(2000), @.val_list varchar(8000)

............... YOUR CODE HERE ................

SET @.i=(SELECT COUNT(*)
FROM information_schema.columns
WHERE TABLE_NAME='ut_RptQryProdTemp')

SET @.loop = 1
SET @.fld_list = ''
SET @.val_list = ''
WHILE (@.loop <= @.i)
BEGIN
SELECT @.select = 'SELECT ''['' + COLUMN_NAME + ' + '''],''' + ' AS F
INTO ##fld_list
FROM information_schema.columns
WHERE TABLE_NAME=''ut_RptQryProdTemp'' AND ORDINAL_POSITION=' + convert(varchar(3),@.loop)
EXEC (@.select)
SELECT @.tmp = (SELECT REPLACE(F, ',', '') + ' AS ' + REPLACE(F, 'PRODOUT],', '],') + '' FROM ##fld_list)
SELECT @.val_list = @.val_list + @.tmp
SELECT @.fld_list = @.fld_list + REPLACE(STUFF(@.tmp, 1, 0, 'CAST('), ' AS [', ' AS varchar) AS [')
DROP TABLE ##fld_list
SET @.loop = @.loop + 1
END
SET @.fld_list = LEFT(@.fld_list, LEN(@.fld_list) - 1)
SET @.val_list = LEFT(@.val_list, LEN(@.val_list) - 1)
SET @.val_list = REPLACE(@.val_list, ',[', ',''')
SET @.val_list = REPLACE(@.val_list, '] AS', ''' AS')
SET @.val_list = REPLACE(@.val_list, 'PRODOUT', '')
SET @.val_list = STUFF(@.val_list, 1, 1, '''')

EXEC ('SELECT ' + @.val_list + ' UNION ALL SELECT ' + @.fld_list + ' FROM ut_RptQryProdTemp ORDER BY _DEP, _FAM, _STYLE, _COLOR, [_SIZE]')

As I said this is for a very specific situation, but if you're good with sql syntax you should be able to modify it.

good luck!|||Never [played with it, but my guess is you could use com automation..good luck

Any docs on COM AUTOMATION please ?|||Hello, everyone:

Thanks for all replies.

UNION cannot be used in bcp statement as SELECT part. I tried and got error. Pootle got a great idea. I did it and worked. Creat a view with all varchar column. Insert column names as first row. Then insert data from table. bcp to txt from view. This query will run automatically. The txt file name is changed by date. No problem on file name in bcp.

ZYT

No comments:

Post a Comment