Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to load a text file as a complete string for an Sql query?

I have a Sql query as:

SET @title1 = 'text_title';
SET @content1 = 'Text_contents';

INSERT INTO `tablename` (`ID`, `content`, `title`)
     VALUES
     (101, @content1, @title1)
ON DUPLICATE KEY UPDATE
    content = VALUES(content),
    title = VALUES(title);

I want to laod a text file and use its contents for the content‘s column, instead of declaring it with SET, and if possible, load the text file’s name as the title.

How can I do so?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If my question needs some corrections, please let me know. I looked around the internet and SO, and I believe my syntaxes weren’t right with what I tried.

To be exact, I tried the following:

UPDATE table_names
SET name = Content
FROM (
    SELECT * FROM OPENROWSET(BULK 'D:\ttt.txt', SINGLE_CLOB)) AS Content
WHERE ID = 3;

.

LOAD DATA 'D:\\ttt.txt' 
INTO TABLE table_names 
WHERE ID = 3;

And some variations that I tested all through.

Any help is appreciated.

>Solution :

In MySQL you can do:

UPDATE table_names
SET name = LOAD_FILE('D:\\ttt.txt')
WHERE ID=3;

see: LOAD_FILE

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading