How to insert a pdf or image to SQL server - sql-server

I'm trying to add an img from my own computer for testing and I need to upload an img or pdf to a column in SQL Server as a varbinary data type. the table has 6 columns; description, img, date, etc.. how do I upload the img or pdf when I have more columns to fill?
create table testVarBinary (description nchar(10), img varbinary(max), etc..)
insert into testVarBinary
SELECT BulkColumn
FROM OPENROWSET (BULK 'FullFilePath\\FileName.pdf', SINGLE_BLOB) as img
I know this works for the img but how do I fill the other columns?
this is just an example, some data may not be correct like the syntax.

Related

How to parse string into multiple tables in SQL Server 2017

I have a text file that was created by dumping 8 SQL tables into it. Now I need to import this data back into SQL Server.
Using BULK insert I was able to load data into one table with single column 'FileData'.
DECLARE #FileTable TABLE (FileData NVARCHAR(MAX))
INSERT INTO #FileTable
SELECT BulkColumn
FROM OPENROWSET( BULK N'C:\My\Path\Name\FileName.txt', SINGLE_CLOB) AS Contents
SELECT * FROM #FileTable
So now I have this huge string that I need to organize into different tables.
For example this part of string corresponds to the below table :
FileData
00001 00000009716496000000000331001700000115200000000000
Table:
It also seems like all fields have a set length and I can get that length.
I can see doing something like this:
select SUBSTRING('00001 00000009716496000000000331001700000115200000000000 ', 1,5) as RecordKey
select SUBSTRING('00001 00000009716496000000000331001700000115200000000000 ', 6,17) as Filler
select SUBSTRING('00001 00000009716496000000000331001700000115200000000000 ', 23,16) as BundleAnnualPremium
But is any faster and better way to load this data into different tables?
You could just bulk insert with a format file right from the start. But since the data is already loaded into a big table, if you'd rather use pure TSQL, you can pull elements out of a string using left(), right(), and substring().

Insert Image with PostDeployment script with Visual Studio Database Project

I have a visual studio sql server database project, and an Images table. I need insert a default image in a PostDeployment script, because this image will be used in all entities with an empty image. How can I store this image in the project, and how can access it in the script?
Insert this image in the database project is a good practice? I have a WindowsService project, and a ASP.NET MVC project with entity framework. Should I insert this image in some kind of initial verification in each one of this other 2 projects?
There are a couple of ways to do this.
Given a table
create table testImage
(id int,
myImage varbinary( max));
You can insert an image from a file with something like:
INSERT INTO testImage (id, myImage)
SELECT 1, bulkcolumn
FROM openrowset(BULK 'D:\x\dZLx1.png', single_blob) as myImage
There are a couple of potential headaches here, you need to keep track of the path to the image in your project somehow, and I think there are a few security-related scenarios where OPENROWSET doesn't work anyway.
It might be more reliable to do this once on your desktop, then SELECT the value out again to use in an insert statement such as
IF NOT EXISTS (SELECT * FROM testIMAGE where ID = 2)
BEGIN
INSERT INTO testImage VALUES
(2,0x89504E470D0A1A0.....)
END
(full script here: https://gist.github.com/gavincampbell/a25431dffd3555563a052c297a32415e)
As you will realise when you try this, the resulting string will be long. I think it would be a good idea to keep this in a separate script and reference it from the main post-deploy script with :r (apologies if you knew this already!). Also remember that you don't need quotes around the binary "string".

Error in Insert to varbinary(max) String or binary data would be truncated. The statement has been terminated

I have table with definition:
I have image files near 80kb. When I am trying insert data to table Usluga like this:
INSERT [dbo].[Usluga] (Nazvanie, Cena_za_poseshenie, Image)
SELECT N'Персональный тренинг', 50, ThumbnailPhoto.*
FROM OPENROWSET
(BULK 'MyFilePathToImage.jpg', SINGLE_BLOB) ThumbnailPhoto
go
INSERT [dbo].[Usluga] (Nazvanie, Cena_za_poseshenie, Image)
SELECT N'Бокс', 90, ThumbnailPhoto.*
FROM OPENROWSET
(BULK 'MyFilePathToImage.jpg', SINGLE_BLOB) ThumbnailPhoto
go
I give error
String or binary data would be truncated.
The statement has been terminated.
But varbinary(max) allows save data from 0 through 2^31-1 (2,147,483,647) bytes.
How do I fix this?
Perhaps you're looking at the wrong column. Try resizing your nvarchar(20) column so it can accept more than 20 characters.
I was getting similar error when trying to insert a image file as a blob to
SQL Server database, it would throw "RIGHT TRUNCATION" error.
I solved my problem by changing the blob column data type from VARBINARY(MAX) to
IMAGE. The varbinary(max) datatype was supposed to replace the image datatype in newer SQL Server instances, my instance was SQL Server 2012. If using the Image data type works for you, then just keep in mind that the max size for this type is 2 GB.

Data shows incorrectly when imported from csv into SQL Server 2008 table

I am using SSIS to import a file into SQL Server 2008. This file is supplied in .csv format to me via someone else. I have no control over the export of the file.
When I import the file the one field shows e.g. 01.10111144000000000000000e+009 instead of 1101111440.
I then proceeded to open it up in Notepad and Excel and that is how it shows up as well. When I right-click on that column in Excel and select 'Format Cells' and set it to General it reflects correctly. Problem is I can't do this manually.
What can I do prior to doing a bulk insert from the file to make sure that the column will import correctly?
You can use below code for importing data from CSV to SQL Server database.
DECLARE #filePath VARCHAR (200)
,#basePath VARCHAR(200) = 'C:\Dumps\'
,#Bulk NVARCHAR(MAX);
-- Get full file path
SET #filePath = #basePath + 'reportsdump.csv';
-- Populate table [#ReportsDumpData] with data from csv.
SET #Bulk = 'BULK INSERT #ReportsDumpData FROM '''+#filePath+''' WITH (';
SET #bulk += 'FIRSTROW = 2, FIELDTERMINATOR = ''";"'' , rowterminator=''\n'')';
EXEC(#Bulk);
The joys of having to work with other people's extracts. I loaded it into a temp (staging) table. From there I first convert it to a float which I convert into a decimal(10, 0) and convert that to a varchar(10). This works.

How to insert binary file into a binary SQL column?

I have a SQL Server 2008 database with a table containing an image column.
Any idea how to write query to insert bin file to the image field?
Thank you in advance!
Totally agree with marc_s.
But your query will be something like this.
INSERT yourtable(ImageColumn)
SELECT image FROM
OPENROWSET(BULK, 'filename.png', SINGLE_BLOB) AS blobtable(image)

Resources