I have a table with a field for text files. These files have multiple lines that I need to copy to a temporary table. I tried using Bulk Import like this:
Bulk Import MyTable From (Select File From FilesTable where Key = 1)
but no success (Incorrect sintax near "(").
Some people have sugested that I use the path of the file but that's not an option because the files are in a table.
AFAICT this can be done with a simple INSERT INTO ... SELECT statement:
INSERT INTO MyTable (
File
)
SELECT
[File]
FROM
FilesTable
WHERE
[Key]=1;
Related
I have a script with which I am trying to load data stored in a .txt file using SQL Server. The text file is stored as UTF-8 encoding. The data read from the txt file showing bom character appended to the first line of data. I am using SQL Server 2012 version.
How can I avoid the bom character while reading?
Here's what I have tried:
CREATE TABLE #Temp1(record nvarchar(512))
BULK INSERT #Temp1
FROM 'C:\Test\test.txt'
WITH (ROWTERMINATOR = '\n')
SELECT * FROM #Temp1
Temp table result is shown as:
test.txt is as below
I am trying to upload 3,000 records to a Teradata table and I am getting the following error:
Error reading import file at record 1: Index and length must refer to
a location within the string
I am importing the data with a txt file and loading it with the following code:
-- Create Table
CT mytable
( col1 VARBYTE (35))
-- Insert data
INSERT INTO mytable VALUES(?)
The text file looks something like this
812619
816625
2B01112
...
I want to import several CSV files automatically using SQL-code (i.e. without using the GUI). Normally, I know the dimensions of my CSV file. So, in many cases I create an empty table with, let say, x columns with the corresponding data types. Then, I import the CSV file into this table using BULK INSERT. However, in this case I don't know much about my files, i.e. information about data types and dimensions are not given.
To summerize the problem:
I receive a file path, e.g. C:...\DATA.csv. Then, I want to use this path in SQL-code to import the file to a table without knowing anything about it.
Any ideas on how to solve this problem?
Use something like this:
BULK INSERT tbl
FROM 'csv_full_path'
WITH
(
FIRSTROW = 2, --Second row if header row in file
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n', --Use to shift the control to next row
ERRORFILE = 'error_file_path',
TABLOCK
)
If columns are not known, you could try with:
select * from OpenRowset
Or, do a bulk insert with only the first row as one big column, then parse it to create the dynamic main insert. Or bulk insert the whole file into a table with just one column, then parse that...
You can use OPENROWSET (documantation).
SELECT *
INTO dbo.MyTable
FROM
OPENROWSET(
BULK 'C:\...\mycsvfile.csv',
SINGLE_CLOB) AS DATA;
In addition, you can use dynamic SQL to parameterize table name and location of csv file.
I have this table in a database.
create table #temp
(
name nvarchar(max)
)
insert into #temp
(
name
)
values
('ปภวรินทร์ เฉื่à¸à¸¢à¹„ธสง')
select * from #temp
When I am seeing this data in the website. the data is displaying as
ชญา สวัสดิ์โยธ
But when I am exporting this data to csv it is displaying as
ปภวรินทร์ เฉื่à¸à¸¢à¹„ธสง
I want to export the data to csc from sqlserver in the same way that shows in WEB.
How can i do that ?
Thanks in advance.
Try to add N before inserting your values. You can insert UNICODE characters.
insert into #temp
(
name
)
values
(N'ชญา สวัสดิ์โยธ')
This is either (a) an issue with your UTF target (e.g. you are targeting UTF-32 on export rather than UTF-8 / 16) or (b) the db you are using requires a symbolic string for inserting these characters-- sort of like how "??!" is a trigraph for "|".
I have an H2 database (http://www.h2database.com) and I'd like to insert a file into a BLOB field via a plain simple sql script (to populate a test database for instance). I know how to do that via the code but I cannot find how to do the sql script itself.
I tried to pass the path, i.e.
INSERT INTO mytable (id,name,file) VALUES(1,'file.xml',/my/local/path/file.xml);
but this fails.
Within the code (java for instance), it's easy to create a File object and pass that in, but directly from a sql script, I'm stuck ...
Any idea?
For testing, you can insert literal hex bytes or use the RAWTOHEX(string) function, as shown below.
create table a(id integer, item blob);
insert into a values(1,'54455354');
insert into a values(2, RAWTOHEX('Test'));
select UTF8TOSTRING(item) from a;
TEST
Test
Addendum: For loading BLOB fields from a file, FILE_READ(fileNameString) may be a useful alternative.
insert into a values(3, FILE_READ('file.dat'));
Not h2database, but may help; https://blog.jerrynixon.com/2009/03/tsql-to-insert-imageblog.html
Example code from the linked blog article, should the link break again:
CREATE TABLE MyTable
(id int, image varbinary(max))
INSERT INTO MyTable
SELECT 1
,(SELECT * FROM OPENROWSET(
BULK 'C:\file.bmp', SINGLE_BLOB) as x )