SQL - insert image from relative path? - sql

I'm trying to write a SQL script, that can be transferred to another computer and someone else will get a whole database with no problem.
What I'm fighting with, is how to make a relative path for an image?
Let's say, that someone will have a script in c:\Documents\script.sql
But I don't know that if someone will keep it there.
I have a folder with images, that I want to load to my database
So here's a fragment of my script, how to make a relative path? Let's say that images/ and script.sql are in the same folder
INSERT dbMagazynier.dbo.Produkty (Nazwa, Cena, Opis, Zdjecie, ID_Producenct)
SELECT 'jeansy', 199.00, 'jenasy jak na zdjeciu', Zdjecie.*, 5
FROM OPENROWSET (BULK '\images\jeansy.jpg', SINGLE_BLOB) Zdjecie
BUT SQL Server 2012 says that it can't find my jeans image

I don't believe you can work with relative paths in SQL Server. You can check more in this question: Relative path in t sql?
The best you can do is to declare a variable at the top of the script, specify the file location there and use your variable later in the code of the script. You can also put some comments about the variable with instructions for people using the script.

Related

How to extract sql code from Knime Nodes?

Is there a way to automatically extract code from nodes and save it in .sql or .txt files?
I'm using mostly Database SQL Executor (legacy) nodes where I have sql queries.
I've found that there is settings.xml file for every node in which I can see code as a value for key="statement", maybe I could use XML Reader and XPath nodes somehow?
I would like to have .sql or .txt file for every node, that file should contain sql code that is pasted in that particular node. It would be great if I could choose a name of that file as name of a node.
The Vernalis community contribution has a DB to Variable node. If you change the input port type to DB Data Port, then one of the output variables will be the SQL query. If you are using the Legacy nodes, then the corresponding Database To Variable (Legacy) node will to the same thing.
Once you have the SQL in the variable, you can use a Variable to Table Row node, and then e.g. the Vernalis Save File Locally node, or if you require further options, String to Binary Objects and Binary Objects to Files nodes will allow that
I've decided to share the idea of my solution to that problem, maybe someone else would like to do something like this in simillar way.
I had to work with RStudio and decided to write a script in Rcpp language (weird version of cpp that allows you to anchor R script in it).
Script has a path to the Knime workflow and iterates through every Node folder in search of "Databse SQL Executor" and "Database Reader" nodes.
Then extracts sql code and name of the node from settings.xml file.
After saving it to the variables it clears node name from signs not allowed in windows file names (like ? : | \ / etc) or stuff that xml added.
Same goes for sql code but instead of clearing xml stuff it changes it to the normal version of a sign (for example it changes %%000010 to \n or &lt to <)
When sql code is cleared and formated it saves the code in a .sql file with the name beeing name of the node.
It works pretty well and quite fast. One annying problem is that rcpp doesn't read UTF-8 signs so I had to clear them out from node names manually so the names are readable and not full of some nonesense.

Query for finding all occurrences of a string in a database

I'm trying to find a specific string on my database. I'm currently using FlameRobin to open the FDB file, but this software doesn't seems to have a properly feature for this task.
I tried the following SQL query but i didn't work:
SELECT
*
FROM
*
WHERE
* LIKE '126278'
After all, what is the best solution to do that? Thanks in advance.
You can't do such thing. But you can convert your FDB file to a text file like CSV so you can search for your string in all the tables/files at the same time.
1. Download a database converter
First step you need a software to convert you databse file. I recommend using Full Convert to do it. Just get the free trial and download it. It is really easy to use and it will export each table in a different CSV file.
2. Find your string in multiple files at the same time
For that task you can use the Find in files feature of Notepad++ to search the string in all CSV files located at the same folder.
3. Open the desired table on FlameRobin
When Notepad++ highlight the string, it shows in what file it is located and the number of the line. Full Convert saves each CSV with the same name as the original table, so you can find it easily whatever database manager software you are using.
Here is Firebird documentation: https://www.firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25.html
You need to read about
Stored Procedures of "selectable" kind,
execute statement command, including for execute statement variant
system tables, having "relation" in names.
Then in your SP you do enumerate all the tables, then you do enumerate all the columns in those tables, then for every of them you run a usual
select 'tablename', 'columnname', columnname
from tablename
where columnname containing '12345'
over every field of every table.
But practically speaking, it most probably would be better to avoid SQL commands and just to extract ALL the database into a long SQL script and open that script in Notepad (or any other text editor) and there search for the string you need.

Pulling Image directly from URL directory and stored it in sql server db

Hi I have a Table Called Temp
with two columns Name (varchar) and Image (VarBinary(max)) datatype respectively
Insert into Temp
(
Name,
Image
)
Select
'Bob',
(
select BulkColumn from openrowset (Bulk 'http://pngimg.com/upload/apple_PNG2579.png',Single_Blob) as Apple
)
This query errors out saying 'Cannot bulk load because the file "http://pngimg.com/upload/apple_PNG2579.png" could not be opened. Operating system error code 123(error not found). However when i click on the url it is perfectly valid.
Anyone up for suggestions?
Please note there is a way to store the image on the local server and then access it. However i am trying to find a way to directly do it within sql
Thanks in Advance
http://pngimg.com/upload/apple_PNG2579.png seems like an URL to me.
BULK INSERT is expecting a file name like c:\image\image.jpg
Using URL as the source is not supported with BULK INSERT.
Try to download the files locally first, then insert into the table.
SSIS, for example, has the functionality to download HTTP content.

Bulk insert from a txt file which has its own rules

I got a txt file which includes 350.000 lines and I have to download and insert it to my sql server database. I write the part that connects to FTP gets the related file and download it. What I want is insert it to my table.
Here's a line:
9996281000L0000000000000000
As you can see also I need to seperate the specific parts like
999 628 1000 L 0000000000000000
I need an effective solution which cuts the lines and inserts the data to related columns.
Anyone any ideas how I can achieve this?
Look into the BCP utility and its format files. It's a detailed and somewhat complex process, but it will do the job quickly and efficiently once set up.
You can get similar functionality (with a much better GUI) with SQL Server Integration Services (SSIS). While completely different, it does much the same thing as bcp.

In a Sql Bulk Insert statement, can we use relative path(files\a.txt) instead of absolute path(c:\abc\a.txt) or networked universal path (\\abc\a.txt)

I wanted to insert data in a table from a text file where it is stored in csv format to a sql server table. For that, I am using bulk-insert statement. Now I need to specify the file name in "From" clause. I don't want to use networked locations or local locations over there. I want to upload my text file in the same directory as my executable file and give a relative path to it. Is it possible???
I don't think so. I just tried
SELECT BulkColumn
FROM OPENROWSET(BULK'files\doesnotexist.txt',SINGLE_BLOB)x;
And looked at it in Process Monitor
And it was looking for C:\WINDOWS\system32\files\doesnotexist.txt so I think you would need to put in the full path.