SQL query image column to file - sql-server

I want to run a query over a table in SQL Server to save out the data as files.
The table has one column with a filename in it and one column that is an image column with binary file contents data in it.
I'm sure I saw some syntax that would let me do this, but I cannot for the life of me find it anymore.
Is this possible?

You can do this with the bcp.exe from the command line which you could call through xp_cmdshell.
bcp "select MyBlobField from myTable WHERE a=b " queryout "c:\MyImage.jpg" -T -n
You can probably do it through OLE automation natively in SQL Server; but its not something I have tried.
An easy alternative is (if you have 2005/8) a CLR into the DB to do the job. THere are lots of code examples on the web how to do that.

Related

How to export all tables within an Sybase 12.5 database to multiple flat files?

My customers runs an very old (seems to me) Sybase 12.5.2 database. I want/need to export all tables from a database to multiple (for each table) flat (text) files. I have access to ISQL command line prompt with the admin user. I havent worked ever with an Sybase database before.
Sybase Adaptive Server Enterprise (ASE) allows multiple databases to be hosted. You don't specify whether only one of the databases in the database server needs to be exported or if all of them do.
For each database, the following query will list the names of the tables
select name from sysobjects where type = 'U'
Sybase ASE also comes with a tool called "bcp" which stands for "Bulk Copy". It is an easy way of creating a flat file of a table's contents.
bcp database.schema.table out file_name -c -U username -S server_name
It has more options that may be of interest, especially around field and row terminators. Documentation for the most relevant version (12.5.1) can be found here:
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.dc30191_1251/html/utility/BABGCCIC.htm
i have been using BCP commands to export data from sybase environments.bcp is a command line utility which you can use it to export data from multiple types of databases
below is a very example and you can try it for
bcp Table Name out OUTPUT FILE PATH\FILENAME.dat -S SERVER NAME -U USERNAME -P PASSWORD -F Format -r row_terminator -e error output file path and name
You can create a batch file with such commands and do multiple exports on one hit.
If you have access to any ETL tool you can exporting the data using the same as well.

SSIS - How to insert into OLE DB destination using SQL command while the source is flat file?

I want to know how to insert value in SQL Server database with the flat file source in SSIS using SQL command. I've done inserting it using table view, now i have to insert it using SQL command
Well you need a good query to set into a Execute SQL Task in SSIS
you can get help for queries in the site below
----here is the link ----
well you can parametrize the query in Execute SQl Task of SSIS
BCP
This is one of the options that is mostly widely used. One reason for this is that it has been around for awhile, so DBAs have come quite familiar with this command. This command allows you to both import and export data, but is primarily used for text data formats. In addition, this command is generally run from a Windows command prompt, but could also be called from a stored procedure by using xp_cmdshell or called from a SSIS package.
Here is a simple command for importing data from file C:\ImportData.txt into table dbo.ImportTest.
bcp dbo.ImportTest in 'C:\ImportData.txt' -T -SserverName\instanceName
BULK INSERT
This command is a T-SQL command that allows you to import data directly from within SQL Server by using T-SQL. This command imports data from file C:\ImportData.txt into table dbo.ImportTest.
BULK INSERT dbo.ImportTest
FROM 'C:\ImportData.txt'
WITH ( FIELDTERMINATOR =',', FIRSTROW = 2 )
Forgot to say that u can write a select query too with the samples in a OLEDB Source Using Sql Command

How to save the generated XML file in a directory passing him in sql server?

Personally I'm with a doubt saving my XML in sql server. I am using FOR XML PATH to generate the xml from a table in my database. The sql server shows me on the screen the XML generated but not saved to file it (well, I guess not lol). How do I save it by passing a directory?
I have the following query to generate XML:
select TableName,
operation,
UserName,
DataAcesso,
CamposTabela,
ValoresCampos,
CamposPKs,
ValoresCamposPKs
FROM TabelaLog
FOR XML PATH ('Log')
anyone know how to save to file?
thank you!
You can try the BCP utiity
bcp "select TableName, operation, UserName, DataAcesso, CamposTabela, ValoresCampos, CamposPKs, ValoresCamposPKs FROM TabelaLog FOR XML PATH ('Log')" queryout "D:\MyTable.csv" -c -t , -S SERVERNAME -T
However, the headers must be passed explicitly, if you want them. You can use a UNION ALL for that purpose.
Check out this post. Has some good instructions on exporting info from a query\table to file
How do I send a database query to a text file?
Alternatively you could use SQL Server Integration services (SSIS) and setup a scheduled job to export the information periodically. That would be my option. Another alternative would be to pull it into your application and then save\export it from server side code rather than letting the database do it for you.

How Can I Export a Record Set to Excel from SQL Server 2008?

I have a department table filled with records. I would like to run a query or use the
management studio interface to export department records to an excel file. Format is not an issue, i just want to dump the records into excel. I know i can copy and paste, but this is not what i'm looking for.
Using SQL Server Management Studio, consider these two options:
1. Ensure you have your results format set to "Results to Grid". Run your query as per normal. Right-click the result set, and choose "Save Results As..." The default file-type should be .csv, and that'll open in Excel without problem.
2. Ensure your results format is set to "Results to File". Run your query, and SSMS will prompt you for a file name, and dump to a plaintext file. Excel will open this, but the formatting won't be optimal.
Also consider this answer from Sijin regarding setting the output separator when using Results to File. SSMS allows you to specify a separator. In this case, you'd want a comma for .csv files.
Using sqlcmd from the command line, consider this answer by scottm for another similar question.
sqlcmd -S myServer -d myDB -E -Q "select col1, col2, col3 from SomeTable"
-o "MyData.csv" -h-1 -s"," -w 700
What version of Excel are you using?
Excel 2007 has the ability to read data from pretty much any data source - you can find this on the "Data" tab under "From other sources" -> "From SQL Server".
I'm sure that this functionality is also available in earlier editions of Excel (at least 2003), but I wouldn't be able to tell you where.

How to import data from a view in another database (in another server) into a table in SQL Server 2000?

I was thinking about using bcp command to solve the user authentication, but does a bcp command capable to import to a table in my database? By the way, I am using SQL Server 2000 environment.
Here's the code I have got so far:
SET #Command = 'bcp "SELECT vwTest.* from [myserver\sql].test.dbo.vwTest" queryout dbo.Test -C ACP -c -r \n -t ";" -S myserver\sql -Umyuser -Puser1'
EXEC master.dbo.xp_cmdshell #Command
Based on the comparison of BCP, BULK INSERT, OPENROWSET (infer Linked Server) here:
...the bcp utility runs out-of-process. To move data across process memory spaces, bcp must use inter-process data marshaling. Inter-process data marshaling is the process of converting parameters of a method call into a stream of bytes. This can add significant load to the processor. However, because bcp [both] parses the data and [converts the] data into [the] native storage format in the client process, they can offload parsing and data conversion from the SQL Server process.
...bcp possibly isn't the most efficient means of transferring data. You might be better off to:
Create a linked server instance to the other database
Use INSERT statements, so that the tables are populated based on records from the database exposed in the linked server instance.
Besides potentially being more efficient, you only need to setup the linked server instance once versus running BCP to create output scripts every time you want to move data.
Mind that the linked server instance is based on a user on the other database, so permissions to the other database are based on that users' permissions.
SURE !!
Use this command (adopt it for your needs) on your source machine:
bcp database.dbo.viewname out c:\temp\viewname.bcp
and then import the data back into your destination system using:
bcp newdatabase.dbo.importtable in c:\temp\viewname.bcp
-c -S(servername) -U(username) -P(password)
That should grab the contents of your "viewname" from the source server, put it in a temporary file, and insert that file back into the new database on the new server.
Typically, you would load those data rows into a new, temporary staging table, and form there, use T-SQL or other means to insert that data into your actual tables.
Check out the MSDN documentation on bcp in SQL Server 2000 for details on all those switches and their meanings.

Resources