Problems with bcp utility - sql

I want to extract a SELCET query with the bcp utility. But it doesn't work. MS SQL underlines "bcp" marks it.
DECLARE #DBName NVARCHAR(MAX);
SET #DBName = (SELECT name FROM master.dbo.sysdatabases where name LIKE '%NAV%');
EXECUTE ('USE [' + #DBName+']');
PRINT (' DATABASE SELECTED: '+ #DBName)
bcp "SELECT [Role ID],[Name] FROM [dbo].[Permission Set]" out "C:\Users\Public\Documents\test.txt" -c -T
The error message is:
Msg 102, Level 15, State 1, Line 19
Incorrect syntax near 'bcp'.
Does anybody know the correct syntax? If I compare it with the examples on the docs.microsoft website (https://learn.microsoft.com/en-us/sql/tools/bcp-utility#o) I do not see my mistake.

You need to use xp_cmdshell procedure to execute the bcp command in T-SQL script
Declare #sql varchar(8000)
Select #sql = 'bcp "SELECT [Role ID],[Name] FROM [dbo].[Permission Set]" out "C:\Users\Public\Documents\test.txt" -c -T'
Exec master..xp_cmdshell #sql

bcp is a command line utility. It runs from a shell script (typically), not from T-SQL.
Within SQL code, you would normally use bulk insert. See the documentation here.

Related

BCP with WHERE clause Fails

When using BCP to export data from a table to an XML file this works perfectly:
declare #cmd nvarchar(255);
select #cmd = '
bcp "select AGENT,TERMCD ,MYAMOUNT,CAMPNAME,LCDATE,CAMPAIGNID from [MYDATABASE].[dbo].[CC1] row for xml auto, root(''rows''), elements" '
+ 'queryout "d:\temp\sample.xml" -S DESKTOP-2C2OAE9 -T -w -r -t';
exec xp_cmdshell #cmd;
go
However if I add a simple WHERE clause like below it fails.
declare #cmd nvarchar(255);
select #cmd = '
bcp "select AGENT,TERMCD ,MYAMOUNT,CAMPNAME,LCDATE,CAMPAIGNID from [MYDATABASE].[dbo].[CC1] WHERE AGENT=''Kelly Martens'' row for xml auto, root(''rows''), elements" '
+ 'queryout "d:\temp\sample.xml" -S DESKTOP-2C2OAE9 -T -w -r -t';
exec xp_cmdshell #cmd;
go
There are the errors:
NULL
Starting copy...
SQLState = 37000, NativeError = 102
Error = [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'row'.
SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]Unable to resolve column level collations
NULL
BCP copy out failed
NULL
I am actually going to have to use LCDATE which is varchar to compare to the current date but I wanted to do something simple initially since I am having problems but if you have thoughts on that too it would be welcome.
Your 'row' table alias moved to after the where, it should be after the table name
declare #cmd nvarchar(255);
select #cmd = '
bcp "select AGENT,TERMCD ,MYAMOUNT,CAMPNAME,LCDATE,CAMPAIGNID from [MYDATABASE].[dbo].[CC1] row WHERE AGENT=''Kelly Martens'' for xml auto, root(''rows''), elements" '
+ 'queryout "d:\temp\sample.xml" -S DESKTOP-2C2OAE9 -T -w -r -t';
exec xp_cmdshell #cmd;
go

How to populate csv file from SQL Server stored procedure?

How to populate a .csv file from a SQL Server stored procedure?
We don't have Office on the Server. The .CSV file has to be populated from a stored procedure result set.
How to export to a .CSV without using SSIS package?
End result, I will have to generate email alert by attaching this CSV file as report.
I will have to use bulk copy program utility (BCP), I am looking on samples for BCP to generate csv file
There are two methods to achieve that:
(1) Using OPENROWSET
Try implementing a similar logic:
INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=D:\;HDR=YES;FMT=Delimited','SELECT * FROM [FileName.csv]')
EXEC Sp_TEST
(2) Using bcp
From the third link in References section:
The queryout method allows you to BCP from the result of a stored procedure, which opens up a lot of possibilities and offers a lot of control over the file format. For anything other than a simple table extract I would tend to use this method rather than a view. I would also format each line within the stored procedure. This means that the formatting can be tested independently from the file creation
declare #sql varchar(8000)
select #sql = 'bcp "exec sp_Test"
queryout c:\bcp\sysobjects.csv -c -t, -T -S'
+ ##servername
exec master..xp_cmdshell #sql
References
How To Export Data To the .csv file using Sql server Stored Procedure.
How to produce an csv output file from stored procedure in SQL Server
Creating CSV Files Using BCP and Stored Procedures
Exporting a csv file via stored procedure
Writing select result to a csv file
Look at the code below. A stored procedure is created and then used as the source in the BCP command. Then a text file is generated. Delimiters are discussed in the code sample in the link.
Code sample taken from Creating CSV Files Using BCP and Stored Procedures
use tempdb
go
create proc s_bcpMasterSysobjects
as
select '"' + name + '"'
+ ',' + '"' + convert(varchar(8), crdate, 112) + '"'
+ ',' + '"' + convert(varchar(8), crdate, 108) + '"'
from master..sysobjects
order by crdate desc
go
declare #sql varchar(8000)
select #sql = 'bcp "exec tempdb..s_bcpMasterSysobjects"
queryout c:\bcp\sysobjects.txt -c -t, -T -S'
+ ##servername
exec master..xp_cmdshell #sql
I would use BCP
see example
declare #sql varchar(2000)
select #sql = 'bcp "SQL QUERY HERE" queryout '+#fileLocation+#fileName+'.csv -c -t, -T -S' + ##servername
exec master..xp_cmdshell #sql, NO_OUTPUT
if you need to see any errors or whats it's doing, just remove the ", NO_OUTPUT"
Create a little script, the csv is a simple format : csv = comma separate value. Separate your values width comma.

Using bcp utility to export SQL queries to a text file

I debug a stored procedure (SQL Server 2005) and I need to find out some values in a datatable.
The procedure is run by an event of the application and I watch just the debugging output.
I do the following my stored procedure (SQL Server 2005), I took a system table (master.dbo.spt_values) as example:
set #logtext = 'select name, type from master.dbo.spt_values where number=6'
--set #logtext = 'master.dbo.spt_values'
SET #cmd = 'bcp ' + #logtext + ' out "c:\spt_values.dat" -U uId -P uPass -c'
EXEC master..XP_CMDSHELL #cmd
So, when I uncomment the second like everything works, a file apprears on the C:\ drive... but if I coment it back leaving only the first line, any output is generated.
How to fix this problem?
bcp out exports tables.
To export a query use queryout instead - you'll need to wrap your query in "double quotes"
set #logtext = '"select name, type from master.dbo.spt_values where number=6"'
--set #logtext = 'master.dbo.spt_values'
SET #cmd = 'bcp ' + #logtext + ' queryout "c:\spt_values.dat" -U uId -P uPass -c'
EXEC master..XP_CMDSHELL #cmd
http://msdn.microsoft.com/en-us/library/ms162802.aspx

TSQL QUERY to dump table into a file?

I want to dump a tale into a text file I am using following command -
bcp tablename out myTable.dat -T -C
But got error it says incorrect symbol near bcp.
I am not sure why I am getting this.
From #Code Monkey's Link : http://www.simple-talk.com/sql/database-administration/creating-csv-files-using-bcp-and-stored-procedures/
try this
declare #sql varchar(8000)
select #sql = 'bcp [dbname].[dbo].[tablename] out myTable.dat -c -t, -T -S'+ ##servernameexec
master..xp_cmdshell #sql
where dbname is the name of your database, and replace dbo with the name of the schema if not dbo

Is it possible to execute a text file from SQL query?

I have a number of generated .sql files that I want to run in succession. I'd like to run them from a SQL statement in a query (i.e. Query Analyzer/Server Management Studio).
Is it possible to do something like this and if so what is the syntax for doing this?
I'm hoping for something like:
exec 'c:\temp\file01.sql'
exec 'c:\temp\file02.sql'
I am using SQL Server 2005 and running queries in management studio.
use xp_cmdshell and sqlcmd
EXEC xp_cmdshell 'sqlcmd -S ' + #DBServerName + ' -d ' + #DBName + ' -i ' + #FilePathName
Very helpful thanks, see also this link:
Execute SQL Server scripts
for a similar example.
To turn xp_cmdshell on and off see below:
On
SET NOCOUNT ON
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE
Off
EXEC master.dbo.sp_configure 'xp_cmdshell', 0
RECONFIGURE
EXEC master.dbo.sp_configure 'show advanced options', 0
RECONFIGURE
SET NOCOUNT OFF
Or just use openrowset to read your script into a variable and execute it (sorry for reviving an 8 years old topic):
DECLARE #SQL varchar(MAX)
SELECT #SQL = BulkColumn
FROM OPENROWSET
( BULK 'MeinPfad\MeinSkript.sql'
, SINGLE_BLOB ) AS MYTABLE
--PRINT #sql
EXEC (#sql)
This is what I use. Works well and is simple to reuse. It can be changed to read all files in the directory, but this way I get to control which ones to execute.
/*
execute a list of .sql files against the server and DB specified
*/
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRAN
DECLARE #DBServerName VARCHAR(100) = 'servername'
DECLARE #DBName VARCHAR(100) = 'db name'
DECLARE #FilePath VARCHAR(200) = 'path to scrips\'
/*
create a holder for all filenames to be executed
*/
DECLARE #FileList TABLE (Files NVARCHAR(MAX))
INSERT INTO #FileList VALUES ('script 1.sql')
INSERT INTO #FileList VALUES ('script 2.sql')
INSERT INTO #FileList VALUES ('script X.sql')
WHILE (SELECT COUNT(Files) FROM #FileList) > 0
BEGIN
/*
execute each file one at a time
*/
DECLARE #FileName NVARCHAR(MAX) = (SELECT TOP(1) Files FROM #FileList)
DECLARE #command VARCHAR(500) = 'sqlcmd -S ' + #DBServerName + ' -d ' + #DBName + ' -i "' + #FilePath + #Filename +'"'
EXEC xp_cmdshell #command
PRINT 'EXECUTED: ' + #FileName
DELETE FROM #FileList WHERE Files = #FileName
END
COMMIT TRAN
I wouldn't recommended doing this, but if you really have to then the extended stored procedure xp_cmdshell is what you want. You will have to first read the contents of the file into a variable and then use something like this:
DECLARE #cmd sysname, #var sysname
SET #var = 'Hello world'
SET #cmd = 'echo ' + #var + ' > var_out.txt'
EXEC master..xp_cmdshell #cmd
Note: xp_cmdshell runs commands in the background, because of this, it must not be used to run programs that require user input.
Take a look at OSQL. This utility lets you run SQL from the command prompt. It's easy to get installed on a system, I think it comes with the free SQL Server Express.
Using the osql Utility
A qick search of "OSQL" on stack overflow shows a lot of stuff is available.
The main thing to handle properly is the user and password account parameters that get passed in on the command line. I have seen batch files that use NT file access permissions to control the file with the password and then using this file's contents to get the script started. You could also write a quick C# or VB program to run it using the Process class.
For Windows Authentication, if you are running as another user:
Open Command Prompt as your Windows user (Right click on it, Open File Location, Shift + Right Click, Run as a different user)
sqlcmd -S localhost\SQLEXPRESS -d DatabaseName-i "c:\temp\script.sql"
Or if you are using Sql Server user:
sqlcmd -S localhost\SQLEXPRESS -d DatabaseName-i "c:\temp\script.sql" -U UserName -P Password
Replace localhost\SQLEXPRESS with you server name if not local server.
Open windows command line (CMD)
sqlcmd -S localhost -d NorthWind -i "C:\MyScript.sql"
For anybody stumbling onto this question like I did and might find this useful, I liked Bruce Thompson's answer (which ran SQL from files in a loop), but I preferred Pesche Helfer's approach to file execution (as it avoided using xp_cmdshell).
So I combined the two (and tweaked it slightly so it runs everything from a folder instead of a manually created list):
DECLARE #Dir NVARCHAR(512) = 'd:\SQLScriptsDirectory'
DECLARE #FileList TABLE (
subdirectory NVARCHAR(512),
depth int,
[file] bit
)
INSERT #FileList
EXEC Master.dbo.xp_DirTree #Dir,1,1
WHILE (SELECT COUNT(*) FROM #FileList) > 0
BEGIN
DECLARE #FileName NVARCHAR(MAX) = (SELECT TOP(1) subdirectory FROM #FileList)
DECLARE #FullPath NVARCHAR(MAX) = #Dir + '\' + #FileName
DECLARE #SQL NVARCHAR(MAX)
DECLARE #SQL_TO_EXEC NVARCHAR(MAX)
SELECT #SQL_TO_EXEC = 'select #SQL = BulkColumn
FROM OPENROWSET
( BULK ''' + #FullPath + '''
, SINGLE_BLOB ) AS MYTABLE'
DECLARE #parmsdeclare NVARCHAR(4000) = '#SQL varchar(max) OUTPUT'
EXEC sp_executesql #stmt = #SQL_TO_EXEC
, #params = #parmsdeclare
, #SQL = #SQL OUTPUT
EXEC (#sql)
DELETE FROM #FileList WHERE subdirectory = #FileName
PRINT 'EXECUTED: ' + #FileName
END