BULK INSERT import text file - database

When I import a CSV or text file and bulk insert it into my database, the process successfully adds all record to the table.
My problem is that the inserted string is in Arabic, which appears as symbols in my database table. How can i solve this problem?

Insert using query
You need to choose an Arabic collation for your varchar/char columns or use Unicode (nchar/nvarchar).
CREATE TABLE MyTable
(
MyArabicColumn VARCHAR(100) COLLATE Arabic_CI_AI_KS_WS,
MyNVarCharColumn NVARCHAR(100)
)
Both columns should work.
Bulk Insert from file
This article explains how to bulk insert unicode characters.
Test Table
USE AdventureWorks2012;
GO
CREATE TABLE myTestUniCharData (
Col1 smallint,
Col2 nvarchar(50),
Col3 nvarchar(50)
);
Bulk Insert
DATAFILETYPE='widechar' allows the use of Unicode character format when bulk importing data.
USE AdventureWorks2012;
GO
BULK INSERT myTestUniCharData
FROM 'C:\myTestUniCharData-w.Dat'
WITH (
DATAFILETYPE='widechar',
FIELDTERMINATOR=','
);
GO
SELECT Col1,Col2,Col3 FROM myTestUniCharData;
GO

Related

T-SQL OUTPUT clause to update a temp table

I have a utility script that is used to insert data into tables in my database. The script has a number of temp table in it that stores the new data to be inserted and a lot of it is related.
So, for example I have tables like so
DECLARE #Table1 TABLE
(
Table1ID INT
Table1Description VARCHAR(50)
Table1Code VARCHAR(5)
)
DECLARE #Table2 TABLE
(
Table2ID INT
Table2Description VARCHAR(50)
Table2Code VARCHAR(5)
)
DECLARE #Relationships TABLE
(
Table1Code VARCHAR(5)
Table2Code VARCHAR(5)
)
So the script populates the data in #Table1 and #Table2, but doesn't populate the ID fields. Once the data has been MERGEd into the database tables, I update the Table1ID and Table2ID fields in a separate statement as they are auto incrementing fields. Then when I use the #Relationships table to populate the database table, I can join to #Table1 and #Table2 to get the actual ID values.
I'm updating the script and I'm wondering if I can MERGE the data from #Table1/#Table2 into the database and update the ID fields in the temp table as part of the MERGE statement using the OUTPUT clause all in one statement?
I think the answer is no as I can't find anything mentioning updating an existing table with the OUTPUT clause, only inserting into a table.
I am still able to do what I need to do, so I'm not after alternatives. I just wondering if it is possible using the OUTPUT Clause
Thanks in advance

How can we completely replace the contents of a table with another table in a SQL Server database?

How can we completely replace the contents of a table with another table in a SQL Server database?
Like Truncate. We want to take all the data in one and put the data in the other one into it. How can we do? How do we do it with a script? By doing this automatically.
It happens in Oracle, does it happen in SQL Server? Thanks.
Just make simple insert into statement like this :
Also you can create procedure that will makes like just my simple example
DECLARE #employee1 TABLE(
Emp VARCHAR(100),
DOB datetime
)
INSERT INTO #employee1 SELECT 'ABC','1991-03-01'
INSERT INTO #employee1 SELECT 'XYZ','1992-12-01'
INSERT INTO #employee1 SELECT 'AJM','1992-08-20'
INSERT INTO #employee1 SELECT 'RNM','1991-07-10'
DECLARE #employee2 TABLE(
Emp VARCHAR(100),
DOB datetime
)
INSERT INTO #employee2
select * from #employee1
select '#employee1',* from #employee1
select '#employee2',* from #employee2
SQLFiddleDemo
If i am correct schema is same for both tables. In this case there are many way you can achieve this automatically,
Option 1: Call Stored Procedure from any web page or window service or any api.
Option 2: Create triggers.
In both way you can use following script to copy data from one table to other:
Truncate table <table1>
GO
INSERT <Table1> (column1, column2)
SELECT Column1, column2 FROM <Table2>
GO

Importing a CSV file which contains French Characters

I have a CSV file which has French Characters in some of the fields.
But when I import this data into a DB. I do not see French characters instead it shows some other special characters.
Query I am using to import the .csv file is as follows:
--Create Table
Create Table A_test (A_1 VARCHAR(100))
--Bulk Import .csv file with ANSI encoding
BULK INSERT A_Test
FROM 'C:\A_Test.csv'
WITH
( DataFileType = 'widechar',
ROWTERMINATOR ='\n'
);
--Sample Data in C:\A_Test.csv file
Le vieux château
Une fête
Le dîner
L'hôtel
Could anyone help me on this?
You can alter the collation of the affected columns by running the following code (I just made up the column name and datatype):
ALTER TABLE dbo.a_test
ALTER COLUMN somecolumn varchar(100) COLLATE French_CI_AS NOT NULL;
Also you could create the original table with the relevant columns pre-collated:
CREATE TABLE dbo.a_test
(
[somecolumn] varchar(100) COLLATE French_CI_AS NOT NULL
)
BULK INSERT like this:
BULK INSERT a_test from 'C:\etc.txt' WITH (DATAFILETYPE = 'widechar')

Does Bulk Insert have an opportunity with accent grave characters?

If I do this, it works correctly with plain old varchar, so I don't think it's a nVarchar problem:
declare #RAM TABLE(
Descr Varchar(128)
)
INSERT INTO #RAM(Descr) VALUES('De La Crème')
SELECT * FROM #RAM
But I'm having trouble importing the same data with a Bulk Insert.
Q: Is there a setting that I'm missing to allow accent grave?
Your question connected not with Bulk Insert (actually, there is no Bulk Insert in your code) or INSERT, but with data types. You need to use nvarchar and N in front of the string constant which define it as a UNICODE value.
declare #RAM TABLE(
Descr nvarchar(128)
)
INSERT INTO #RAM(Descr) VALUES(N'De La Crème')
SELECT * FROM #RAM

BULK INSERT into table and auto-increment

I have a .txt file in the format:
123456
111111
122222
123344
121212
I wish to insert these into a temporary table along with an integer recording the order in which they are in the .txt file, such as:
Index Number
---------------
1 123456
2 111111
3 122222
4 123344
5 121212
Currently I'm doing this by having an IDENTITY column in my temporary table and doing a BULK INSERT using a FORMATFILE like so:
CREATE TABLE #tbl
(
idx int NOT NULL IDENTITY,
ItemNumber nchar(6)
)
BULK INSERT #tbl
FROM 'd:\MyNumberList.txt'
WITH
(
FORMATFILE='d:\MyFormatFile.xml'
)
However, I'm hoping theres a way of achieving this without the need for a FORMATFILE file.
Is there a way of doing this?
You can do this with a view. However, because you are using a temporary table here, and it's not possible to create a view on a temporary table, you'd addtionally need to make use of a synonym.
If you were importing into a regular table, you wouldn't need the synonym:
create synonym tbl for #tbl
GO
create view vtbl
as
select ItemNumber from tbl
GO
bulk insert vtbl from 'd:\MyNumberList.txt'
GO
select * from #tbl
GO

Resources