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

Time:03-03

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?

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.

CodePudding user response:

In MySQL you can do:

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

see: LOAD_FILE

  • Related