Passing username and password to sqlcmd, - passwords

Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'abc.pqr'..
Begin
Declare #trcustomerid int = 16
Declare #cmd varchar(8000)
set #bcp = 'sqlcmd -S servername,portname -U abc.pqr -P Password, -W -Q "set nocount on; SELECT * FROM [dbname].[schemaname].[tablename] WHERE id= '+ cast(#trcustomerid as nvarchar(10))+' "'
EXEC master..xp_cmdshell #cmd
END
OR set #bcp = 'sqlcmd -S servername,portname -U abc.pqr -P Password, -W'
is also giving Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'abc.pqr'..
basically, sqlcmd is not able to parse the passed password
How to resolve it?Please help

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

Error in exec master xp_cmdshell BCP queryout

I need to create some XML from a SQL query. I need to increment a name file but with my code the system return the error
Messaggio 102, livello 15, stato 1, riga 89
Incorrect syntax near '+'.
This error refers to the code part in
exec master.. `'+#num'+`
Below the code; the variable #num is of type nvarchar
SET #num = (SELECT CAST([incr_num] AS NVARCHAR(10)) FROM [DB01].[dbo].[NUM_XML])
EXEC master..xp_cmdshell 'bcp "SELECT #header,#inland FOR XML RAW(''''),ROOT(''root''), ELEMENTS, TYPE" queryout "\\server01\TEMP_SW\XML_TMS\test_'+#num+'.xml" -U xx -P xxxxx -c -C ANSI -t;'
I try to insert all path in another variable and attach this new variable in exec function, but the result not change
Can you help me?
Create command text first
declare #cmd varchar(2000) = 'bcp "SELECT #header,#inland FOR XML RAW(''''),ROOT(''root''), ELEMENTS, TYPE" queryout "\\server01\TEMP_SW\XML_TMS\test_'+#num+'.xml" -U xx -P xxxxx -c -C ANSI -t;' ;
exec master..xp_cmdshell #cmd;
Now if I execute the single query
SELECT #header,#inland FOR XML RAW(''),ROOT('root'), ELEMENTS, TYPE
I receive the correct XML data.
But if i insert the query into exec master..xp_cmdshell
declare #cmd varchar(2000) = 'bcp "SELECT #header,#inland FOR XML RAW(''''),ROOT(''root''), ELEMENTS, TYPE" queryout "\\server01\TEMP_SW\XML_TMS\test_'+#num+'.xml" -U xx -P xxxxx -c -C ANSI -t;' ;
exec master..xp_cmdshell #cmd;
The system return the error
SQLState = 37000, NativeError = 137
Error = [Microsoft][SQL Native Client][SQL Server]Must declare the scalar variable "#header".
SQLState = 37000, NativeError = 8180
Error = [Microsoft][SQL Native Client][SQL Server]Statement(s) could not be prepared.
NULL
#header is declared as XML
Let me know thanks and regards

gpg encryption in xp_cmdshell

I am trying to encrypt a .csv file using gpg in xp_cmdshell command in sql server 2012.
When I use gpg through xp_cmdshell, it says gpg is not recognized as an internal or external command.
But this works perfectly on windows cmd.
How should I configure my sql server to accept this command. Please advice.
select #encrypt = 'gpg -e -r ' + #sEncryptionKey+ ' '+ #sPath + #tempdataFolder+'\'+ #sFileName
exec master..xp_cmdshell #encrypt
To remedy the error you are indicating above, simply add the full path to gpg.exe in your code.
select #encrypt = 'c:\gpg\gpg.exe -e -r ' + #sEncryptionKey+ ' '+ #sPath + #tempdataFolder+'\'+ #sFileName
exec master..xp_cmdshell #encrypt
For comparison, this is how I have done this in the past via SSIS; be sure you provide the exact full path to your executable... we are using gpg4win (gpg2.exe) in this example.
You may also want to wrap each parameter in " " to handle file names/paths with spaces better.
DECLARE #sKeyEmail VARCHAR(100), #sInFile VARCHAR(100), #sOutFile VARCHAR(100), #SQL VARCHAR(1000)
SET #sInFile = 'c:\temp\somefile.xls'
SET #sOutFile = 'c:\temp\somefile.xls.gpg'
SET #sKeyEmail = 'user#domain.com'
SET #SQL = 'EXEC master.dbo.xp_cmdshell ''C:\Progra~1\GNU\GnuPG\gpg2 -o ' +#sOutFile+ ' -e -r ' +#sKeyEmail + ' ' +#sInFile+ ''''
PRINT #SQL
-- EXEC sp_execute #SQL

How can I BCP queryout with filename from table?

I'm extracting many BLOBs to files using BCP and wonder if there's a way to give each file a unique name using an ID field from the SQL table.
This works:
DECLARE #Command NVARCHAR(4000)
SET #Command = 'bcp "SELECT FileData FROM MyDB.dbo.Attachments
WHERE Hist_ID = ''00004F13''"
queryout "C:\Wha.pdf" -T -c -C RAW -S SERVERNAME -U SA -P pa$$word'
EXEC xp_cmdshell #Command
But to get the filename, I've tried:
DECLARE #FileName varchar(50),
#Command NVARCHAR(4000)
SET #FileName = Hist_ID+'wha.PDF'
SET #Command = 'bcp "SELECT FileData FROM MyDB.dbo.Attachments
WHERE Hist_ID = ''00004F13''" queryout'
SET #Command = #Command + 'C:\' + #FileName + '-T -c -C RAW
-S SERVERNAME -U SA -P pa$$word'
EXEC xp_cmdshell #Command
I get an error: Invalid column name 'Hist_ID'.
Is there some way to do this? Thanks for any help!
Try this. This is the only way I know have to execute one row at a time.
This code just prints the command. Uncomment the xp_cmdshell to make it actually run. You might want to start with a small sample first (by limiting the select)
DECLARE #Hist_ID VARCHAR(100)
DECLARE #Ext VARCHAR(10)
DECLARE #Command NVARCHAR(4000)
DECLARE cAttachments CURSOR FOR
SELECT DISTINCT Hist_ID,Extension
FROM MyDB.dbo.Attachments
ORDER BY Hist_ID
OPEN cAttachments
FETCH NEXT FROM cAttachments
INTO #Hist_ID, #Ext
WHILE ##FETCH_STATUS = 0
BEGIN
SET #Command = 'bcp "SELECT FileData FROM MyDB.dbo.Attachments
WHERE Hist_ID = ''' + #Hist_ID + '''"
queryout "C:\' + #Hist_ID + '.' + #Ext + '" -T -c -C RAW -S SERVERNAME -U SA -P pa$$word'
PRINT #Command
-- EXEC xp_cmdshell #Command
FETCH NEXT FROM cAttachments
INTO #Hist_ID, #Ext
END
CLOSE cAttachments
DEALLOCATE cAttachments

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