i am trying to use bcp to backup the tables data b...
# cfml-general
g
i am trying to use bcp to backup the tables data but the cfexecute is not writing anything in the files and not the output
Copy code
<cfsavecontent variable="_bcp">
		-- Run this command to the database you wish to export
		DECLARE @Commands TABLE(CommandText NVARCHAR(4000));
		DECLARE @SQL NVARCHAR(4000);

		INSERT INTO @Commands
		SELECT 'bcp '           --bcp
		+  QUOTENAME(DB_NAME())+ '.'               --database name
		+  QUOTENAME(SCHEMA_NAME(SCHEMA_ID))+ '.'  -- schema
		+  QUOTENAME(name)                         -- table
		+ ' out c:\temp\csvdump\'                  -- output directory
		+  REPLACE(SCHEMA_NAME(schema_id),' ','') + '_'
		+  REPLACE(name,' ','')                    -- file name
		+ '.xls -Usa -Pqwerty -C 65001 -t "|" -k -c -S localhost'   -- extension, security
		FROM sys.tables

		-- Copy-paste results to DOS or Powershell prompt or create a BAT file from these.
		SELECT 'mkdir #application.args.bcpbackupsFolder#\#dateFormat(now(),'MMDDYYYY')#
		UNION ALL
		SELECT * FROM @Commands
	</cfsavecontent>	
	<cffile action="write" file="#application.temp#/bcp.bat" output="#_bcp#">
	<cfexecute name="C:\Windows\System32\cmd.exe" arguments="#application.temp#/bcp.bat" timeout="60" outputfile="#application.logs#/log_output_bcp.txt"/>
i also tried changing the arguments value to put in name but same issue
m
Looking at your code all this will do is write the text that is contained in _bcp to a file named .bat the text that is written is not normal windows/dos commands but rather sql scripts. It would appear you lifted these strings from a script that does this in TSQL with things like DB_NAME() and SCHEMA_NAME(SCEHMA_ID))
g
Why don’t you do:
Copy code
<cfexecute name="#application.temp#/bcp.bat" timeout="60" outputfile="#application.logs#/log_output_bcp.txt"/>
g
correct, i did modified the query adn moved the contents of cfsavecontent inside the cfquery now, my bat file looks like the bcp [project1].[dbo].[feedback] out c:\temp\csvdump\feedback.xls -Usa -Pqwerty -C 65001 -t "|" -k -c -S localhost
and it has as many lines as the table but the cfexecute does not work and even i tried copying the file and run directly in cmd, directly it works but noty with bat
SQLState = S1000, NativeError = 0 Error = [Microsoft][ODBC Driver 17 for SQL Server]Unable to open BCP host data-file
i fixed it, it was an issue with my paths