I'm trying to export data to txt file having arabic filename using xp_cmdshell
It is working fine. the problem is when I tried to name the file in arabic i got this error message
SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]Unable to open BCP host data-file
Find below the query:
EXEC xp_cmdshell N'bcp "SELECT FirstName,LastName,Salary FROM MyDB.dbo.Employee" queryout "\\myserver\FDS\سسيسي.txt" -SFDSLAP\SQLR2 -UmyUser -PmyPass -T -w -t "'
This error happens with me when the file is opened by another program and sometimes when the path specified will create a new directory.
Related
So I'm using bcp to import a csv file to my SQL database.
This is the command:
bcp table in D:\temp\some_file.csv -T -S serverName -d dbName -F 2 -c -t "," -r "\n"
I'm getting the following error when I try to import the file:
Starting copy...
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]String data, right truncation
SQLState = 22001, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]String data, right truncation
24 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total : 31 Average : (774.19 rows per sec.)
The last 2 columns of the csv file contains User full name ("surname, name"), Customer full name ("surname, name")
SYS Hostname,SYS Domain,User Name,Customer Name
server1,test.com,"surname, name","surname2, name2"
Is it possible to ignore the , between the ""?
Thanks
EDIT:
As #AlwaysLearning said:
You have a Quoted CSV file which is something the bcp.exe process
cannot handle. As per RFC4180 your data row has four fields server1
test.com surname, name and surname2, name2 but bcp.exe only sees the
commas so thinks it is six fields. Consider alternative methods - such
as using a PowerShell script to load the data file into a DataTable
using Import-Csv and then insert the DataTable using an SqlBulkCopy
object over an SQL connection
There is any other option to import a CSV file by command line o SQL script?
Thanks
See this response here. BCP can work so long as the quoted fields are always quoted, on every line of the file. Regardless of whether the name value contains a comma or not. The question below is regarding export, but it works the same on import. You essentially tell BCP that the delimeter is "," and not just ,
SQL Server BCP Export where comma in SQL field
I have a scenario, in which I am using -q option to change the collation of SQL Server Instance. The command is as follows:
sqlservr.exe -m -T4022 -T3659 -s"SQLexpress" -q"SQL_Latin1_General_CP1_CI_AI"
The above command works perfectly, where I have an instance name as ".\SQLExpress". But if I want to use the instance name using TCP port number, such as ".,52407", then how can I execute it above command, because I get the following error:
SQL Server
Your SQL Server installation is either corrupt or has been tampered with (Error: Instance name exceeds maximum length). Please uninstall then re-run setup to correct this problem
I was thinking to get the instance name from this port number to solve this issue. Is it possible to get the instance name from TCP port number through any query? Or is there any other way to execute the above command through TCP port number?
Following piece of code can handle the above situation:
If($ServerInstanceName -like "*,*")
{
Write-Host "SQL Server Instance Name containts TCP Port Number"
$SQLQuery=#"
SET NOCOUNT ON
Declare #key Varchar(100), #PortNumber varchar(100)
if charindex('\',CONVERT(varchar(200),
SERVERPROPERTY('servername')),0<>0
begin
set #key = 'SOFTWARE\MICROSOFT\Microsoft SQL Server\'+##servicename+'\MSSQLServer\Supersocketnetlib\TCP'
end
else
begin
set #key = 'SOFTWARE\MICROSOFT\MSSQLServer\MSSQLServer\Supersocketnetlib\TCP'
end
EXEC master..xp_regread #rootkey='HKEY_LOCAL_MACHINE', #key=#key,#value_name='Tcpport',#value=#PortNumber OUTPUT
SELECT CONVERT(varchar(200), SERVERPROPERTY('servername')) AS ServerName
"#
$ServerInstanceName = (Invoke-Sqlcmd -ServerInstance
$ServerInstanceName -Database 'master' -Query $SQLQuery).ServerName
Write-Host "Compatible ServerName (without TCP Port) to change its Collation:" $ServerInstanceName
I have looked thoroughly for an answer regarding BCP and extracting a BLOB from my SQL server database. I have followed the various steps found in other threads and I keep coming up with the same file corrupted error.
My database has a column with data type IMAGE in BLOB format. On the users end they can enter photos, pdfs, anything to this field and the server converts them to extremely long BLOBs. My task is to extract these so we can replace them with file name extensions, putting less stress on the database.
My current command is:
bcp "select notes_activex from myDatabase where NCR_NO = '1361'" queryout "C:\BCPtest\testOutput" -S -U -P -f C:\BCPtest\testOutput.fmt
My format file is correct for an image file as per some of the other files posted. I have tried casting the image file first to a varbinary(max) and that still doesnt solve my solution. No matter what I try I can get the BLOB to export but it is a corrupted file.
Added my format file:
11.0
1
1 SQLIMAGE 0 0 "" 1 notes_activex ""
As far as binary data, BCP is intended for extracting data so that it can be later inserted into another SQL Server. It's not saved in a format that's compatible with a binary data file. You'll need to extract the data with a program or script, basically, that's capable of converting the data to bytes.
I've done this in the past with a PowerShell script. I'd use something like the script below. I strongly recommend determining the file from the SQL query if at all possible if you're fetching more than one record at a time.
# Define the connection parameters
$SqlServer = 'MyServerName';
$SqlDatabase = 'MyDatabase';
$SqlConnectionString = 'Data Source={0};Initial Catalog={1};Integrated Security=SSPI' -f $SqlServer, $SqlDatabase;
# Define the query. Note that one field is the file name and the other is the data.
# Modify the WHERE clause to pull the records you need. I am assuming that NCR_NO is not a unique identifier.
$SqlQuery = "select SomeUniqueID AS FileName, notes_activex AS FileData from myDatabase where NCR_NO = '1361'";
# Or use a GUID for the filename
# $SqlQuery = "select NEWID() AS FileName, notes_activex AS FileData from myDatabase";
# Define the path pattern for the output files. {0} will get filled in with the filename.
$OutputFileFullNamePattern = 'C:\Path\To\Output\{0}';
# Create the Connection object and the Command object
$SqlConnection = New-Object -TypeName System.Data.SqlClient.SqlConnection -ArgumentList $SqlConnectionString;
$SqlCommand = $SqlConnection.CreateCommand();
$SqlCommand.CommandText = $SqlQuery;
# Open the connection
$SqlConnection.Open();
# Create the Sql Data Reader
$SqlDataReader = $SqlCommand.ExecuteReader();
while ($SqlDataReader.Read()) {
# Set in the file name
$OutputFileFullName = $OutputFileFullNamePattern -f $SqlDataReader['FileName'];
# Save the data to the file
[System.IO.File]::WriteAllBytes($OutputFileFullName,$SqlDataReader['FileData']);
}
# Close and dispose of the SQL connection
$SqlConnection.Close();
$SqlConnection.Dispose();
This uses an SqlDataReader, which loads records one at a time. This means your system won't need to load the entire table into memory, but it does mean that you'll have a shared lock on the table until it's done if you're dumping the whole table. If possible, run this query during downtime.
I have an application for managing pharmacies (just an example ) this application is developed so that it relates to a single pharmacy . This application map on a Micrsoft DBMS MSSDE
each pharmacy generates database file ( pharmacy.mdf , pharmacy_log.mdf )
So after getting these files from different pharmacies, I wrote a script to automate data extraction from these all files
As the application is running, the script do the following tasks:
- Stops the server MSSQL server
- copy the two files from folder files recover and paste them in the path of the application
- Restart the server
- exctract the desired data
the connection with the database in question is using the python pyodbc Module
Once the extraction is run at a certain time pyodbc crash
i gotte this error:
cursor.execute ('select * from Pha.dbo.table_test ')
pyodbc.Error : ( '01000 ',' [ 01000 ] [ Microsoft] [ODBC SQL Server Driver] [ Shared Memory] ConnectionWrite ( ( send () ( 10054 ) ( SQLExecDirectW ) ')
i wrote this code for connection:
log = os.path.join(path,c,"SIC_log.ldf")
mdf = os.path.join(path,c,"SIC.mdf")
print(log)
print(mdf)
subprocess.call('net stop mssqlserver')
time.sleep(2)
os.system('copy "' + log + '" "' + MSSQL_DIR+'"')
os.system('copy "' + mdf + '" "' + MSSQL_DIR+'"')
time.sleep(2)
subprocess.call('net start mssqlserver')
time.sleep(2)
# Open a connection with the database
cnxn = pyodbc.connect('DSN=SIC_ODBC')
time.sleep(2)
extract_clo(cnxn, wb, ws)
cnxn.close()
I would like to retreive some binary data from a varbinary(max) column in a SQL Server database for debugging purposes.
What is the easiest way to get this data into a local binary file, preferably without having to write a throw-away console application?
I have tried using SQL Server Management Studio (with the "results to file" option) but this outputs a hex encoded binary string to the file, rather than the raw binary data.
I can't think of any easier way to do this than a throw away bit of C#...
static void Main(string[] args)
{
GetBinaryDataToFile("Server=localhost;Initial Catalog=ReportServer;Integrated Security=true", "D:\\temp.dat");
}
public static void GetBinaryDataToFile(string connectionString, string path)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT Sid FROM Users WHERE UserID = '62066184-8403-494E-A571-438ABF938A4F'";
command.CommandType = CommandType.Text;
using (SqlDataReader dataReader = command.ExecuteReader())
{
if (dataReader.Read())
{
SqlBinary sqlBinary = dataReader.GetSqlBinary(0);
File.WriteAllBytes(path, sqlBinary.Value);
}
dataReader.Close();
}
}
connection.Close();
}
}
This code has been tested using the Users.Sid column (which is of varbinary type) in a default installation of SQL Server wih Reporting Services.
I loved the LinqPad suggestion. I tried it and had a query that spit out the binary to a file within 10 minutes. No VS Project, no build - and now the script is saved and I can pull it up anytime. So cool!
LinqPad script:
var g = from pd in Archives
where pd.ArchiveId == 123
select pd;
var q = from p in printDocs
where p.DocumentId == g.SingleOrDefault().DocumentId
select p;
File.WriteAllBytes("C:\\temp.pdf", q.SingleOrDefault().Pdf.ToArray());
I've found this solution with bcp command (run from command prompt):
c:\temp>bcp "select MYVARBINARYCOL from MYTABLE where id = 1234" queryout "c:\filename.pdf" -S MYSQLSERVER\MYINSTANCE -T
Enter the file storage type of field filedata [varbinary(max)]:
Enter prefix-length of field filedata [8]: 0
Enter length of field filedata [0]:
Enter field terminator [none]:
Do you want to save this format information in a file? [Y/n] n
Starting copy...
1 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.) Total : 15 Average : (66.67 rows per sec.)
I used the -T option to use windows authentication to connect to the DB. If you use password auth, you'll need to use the -U and -P switches to specify a username and password.
But I also like LinqPad suggestion in Robb Sadler's answer and somehow prefer it.