Load csv file into SQL Server table - bulkinsert

I am trying to bulk copy from my .csv file into a SQL Server table, but it's not working.
CSV file content:
empid,first_name,last_name
1,Jeni,G
2,Niki,S
3,Broke,D
SQL Server table structure:
CREATE TABLE [dbo].[emp]
(
[empid] [varchar](109) NOT NULL,
[first_name] [varchar](109) NULL,
[last_name] [varchar](19) NULL
) ON [PRIMARY]
Format file:
11.0
133
1 SQLCHAR 0 0 "\"" 1 empid SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 100 "\"\t\"" 2 first_name SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 100 "\"\t\"" 3 last_name SQL_Latin1_General_CP1_CI_AS
Batch script:
BULK INSERT [dbo].[emp] FROM 'C:\emp.csv'
WITH
(FORMATFILE='C:\emp.fmt');
Can anyone please let me know how to achieve bulk copy data from a .CSV file into a SQL Server table? Is there any other possibilities - if so, please let me know!
Thanks,

Related

How to retrieve German characters from a large CSV File into SQL Server 2017 script

I have a CSV file including a list of employees, where some of them includes German characters like 'ö' in their names. I need to create a temp table in my SQL Server 2017 script and fill it with the content of the CSV file. My script is:
CREATE TABLE #AllAdUsers(
[PhysicalDeliveryOfficeName] [NVARCHAR](255) NULL,
[Name] [NVARCHAR](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[DisplayName] [NVARCHAR](255) NULL,
[Company] [NVARCHAR](255) NULL,
[SAMAccountName] [NVARCHAR](255) NULL
)
--import AD users
BULK INSERT #AllAdUsers
FROM 'C:\Employees.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n', --Use to shift the control to next row
TABLOCK
)
However, even though I use "Nvarchar" variable type with the collation of "SQL_Latin1_General_CP1_CI", the German characters are not seem OK, for instance "Kösker" seems like:
"K├╢sker"
I've tried many other collations but couldn't find a fix for it. Any help would be very much appreciated.

insert rows to column wise in sql server [duplicate]

This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 7 years ago.
below is mine table name: sample1
ID NAME ADDRESS
1 DAN NO.10,CHANGJIANG XI STREET,JIANXI DISTRECT ,LUOYANG CITY,HENAN ,CHINA
2 SAM BINALBAGAN NEGROS OCCIDENTAL PHILIPPINES
3 JOSE B-36 L-40 PH-1 ST. JOSEPH VILLAGE 7, MARINIG CABUYAO LAGUNA, 4025
i need to enter the rows to column, but here the challenges is, in sample 2 i have only 4 columns source rows obtained 6 or more while using split function, how can i insert/ append balance data in ADRS4 after inserting datas in columns.
output should be as follows:
ID NAME ADRS1 ADRS2 ADRS3 ADRS4
1 DAN NO.10 NGJIANG XI STREET JIANXI DISTRECT LUOYANG CITY,HENAN ,CHINA
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
Where you want to skip a value simply input a null value. So you query should look something like this:
INSERT INTO table_name (name,adrs1,adres2,...)
VALUES (John,Null,Xi Street,...);
Your SQL design is not that great to begin with IMO but that should work with your current setup. I would create a table that has multiple columns then insert as needed, like this:
CREATE TABLE [dbo].[Table_1](
[id] [int] NULL,
[name] [varchar](50) NULL,
[ADRS1] [varchar](50) NULL,
[ADRS2] [varchar](50) NULL,
[ADRS3] [varchar](50) NULL,
[ADRS4] [varchar](50) NULL
) ON [PRIMARY]

Accents not getting inserted in SQL server

I am trying to add this name -> NumāTwó into a table in MS sql server along with the accents. But it is only getting inserted as -> NumaTwó (without ā). I tried many encodings but doesn't seem to work. I have given the DDL of the table below. Please help
CREATE TABLE [dbo].[test](
[testname] [nvarchar](40) COLLATE SQL_Latin1_General_CP1253_CI_AI NULL
) ON [PRIMARY]
----------- Insert-----------
insert into test values ('NumāTwó');
use N as Prefix for Unicode character
CREATE TABLE [dbo].[test](
[testname] [nvarchar](40) COLLATE SQL_Latin1_General_CP1253_CI_AI NULL
) ON [PRIMARY]
----------- Insert-----------
insert into test values (N'NumāTwó');
Try to use N before the string while inserting like this:
insert into test values (N'NumāTwó');

SQL - How to use the output from an insert to update a table

QUESTION INFO
Detailed Question
The best way I can explain my question is to explain my desired outcome. I'm trying to take a certain set of offices, insert its data into the dbo.DeliveryLocation table, then take the output inserted.DeliveryLocationId and update the corresponding office's DeliveryLocationId field with that id.
Desired Outcome Example
Office Data Before
OfficeId | DeliveryLocationId
-----------------------------
1 | null
2 | null
3 | null
Run the SQL statement
Office Data After
OfficeId | DeliveryLocationId
-----------------------------
1 | 5
2 | 6
3 | 7
Delivery Location with the DeliveryLocationId of 5 was created with
the data of the Office with OfficeId of 1
Delivery Location with the DeliveryLocationId of 6 was created with
the data of the Office with OfficeId of 2
Delivery Location with the DeliveryLocationId of 7 was created with
the data of the Office with OfficeId of 3
The problem
Per my current SQL script below, you can see that I have the first part (inserting the Office data into the Delivery Location table) complete. The second part (updating the Office with the corresponding DeliveryLocationId of the created Delivery Location) is not complete, and I am unsure how to go about doing that.
My initial thoughts/ solutions
If there would be a way to store the correlated OfficeId and DeliveryLocationId, perhaps we could loop through them and update the offices in a second SQL statement rather than try to create one SQL statement that does everything.
REFERENCES
dbo.DeliveryLocation
[DeliveryLocationId] [int] IDENTITY(1,1) NOT NULL,
[LocationName] [nvarchar](max) NULL,
[ShortName] [nvarchar](max) NULL,
[ValidatedAddressId] [int] NOT NULL,
[DropoffInstruction] [nvarchar](max) NULL,
[PickupInstruction] [nvarchar](max) NULL,
[TaxRate] [decimal](18, 2) NOT NULL,
[Active] [bit] NOT NULL,
[DisableOffices] [bit] NOT NULL
dbo.Office
[OfficeId] [int] IDENTITY(1,1) NOT NULL,
[OfficeName] [nvarchar](max) NULL,
[ValidatedAddressId] [int] NOT NULL,
[ReferralSource] [nvarchar](max) NOT NULL,
[NumberOfEmployees] [int] NOT NULL,
[DeliveryLocationId] [int] NULL
Current SQL
insert into
dbo.DeliveryLocation
(LocationName, ShortName, ValidatedAddressId, Active, DisableOffices)
output
inserted.DeliveryLocationId
select
OfficeName, OfficeName, ValidatedAddressId, 0, 0
from
dbo.Office as o
where
OfficeId in
(
select distinct
OfficeId
from
dbo.[User] as u
where
u.DeliveryLocationId is null
and
u.OfficeId is not null
)
I'm not sure about doing this in an INSERT statement, but if you use a MERGE statement using Office (or a query based on Office) as the source, you'll be able to refer to source.OfficeId as well as inserted.DeliveryLocationId in the OUTPUT clause. You can simply skip the update and delete usage of the MERGE, and only use the ON NOT MATCHED clause.
When I'm doing things like this I put the output into a temp table, then carry out any further updates or inserts I need to do from there.
In case you've not used the MERGE statement before (or even for anyone who just hasn't used all of their capabilities), this is a really fantastic resource on how to use them, and how to use them well: http://www.made2mentor.com/2012/07/got-the-urge-to-merge/
You could do an update join after you insert into delivery location
update dbo.Office
set o.DeliveryLocationID = dl.DeliveryocationID
from Office o
JOIN DeliveryLocation dl on dl.LocationName = o.OfficeName
Assuming that the Office Names are unique. If they are not you may want to add OfficeID to the DeliveryLocations table, at least temporarily, and join on that.

Ignore certain columns when using BULK INSERT

I have a comma delimited text file with the structure
field1 field2 field3 field4
1 2 3 4
I wrote the following script to bulk insert the text file, but I wanted to leave out column 3
create table test (field1 varchar(50),field2 varchar(50),field4 varchar(50))
go
bulk insert test
from 'c:\myFilePath'
with
(fieldterminator=',',
rowterminator='\n'
)
The insert worked fine, but the results of the insert made field4 look like
field3,field4, so the field 3 was actually just concatenated onto field4. The flat files I'm working with are several gigs and can't be easily modified. Is there a way to use bulk insert but have it ignore the columns that aren't declared in the create table statement?
The easiest way is to create a view that has just the columns you require.
Then bulk insert into that view.
Example:
create table people (name varchar(20) not null, dob date null, sex char(1) null)
--If you are importing only name from list of names in names.txt
create view vwNames as
select name from people
bulk insert 'names.txt'
You can use a format file to do this:
http://msdn.microsoft.com/en-gb/library/ms178129.aspx
http://msdn.microsoft.com/en-gb/library/ms179250.aspx
Or if you want a slightly cheekier way, just import it all and drop a column afterwards. ;)
you cant ignore a field while doing bulk insert , insted of doing that .. Load all 4 column and drop the colum which you dont want
create table test (field1 varchar(50),field2 varchar(50), field3 varchar(50),field4 varchar(50))
go
bulk insert test
from 'c:\myFilePath'
with
(fieldterminator=',',
rowterminator='\n'
)
ALTER TABLE test DROP column [field3]
You can create a temporary table and insert any data there. After that, you can do whatever you want with it.
CREATE TABLE #TmpTable(
[CategoryId] [int] IDENTITY(1,1) NOT NULL,
[ParentCategoryId] [int] NULL,
[CategoryTypeId] [int] NOT NULL,
[Name] [varchar](255) NOT NULL,
[Code] [varchar](255) NOT NULL,
[AlternateName] [varchar](900) NOT NULL
)
BULK INSERT [dbo].[#TmpTable]
FROM 'C:\tmp\Categories.csv'
WITH
(
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '0x0a' --Use to shift the control to next row
)
-- original table insert
INSERT INTO [dbo].[Categories]
(
CategoryId,
ParentCategoryId,
CategoryTypeId,
Name,
Code,
AlternateName,
IsDeleted,
SystemCreated
)
SELECT
CategoryId,
ParentCategoryId,
CategoryTypeId,
Name,
Code,
AlternateName,
0, -- custom value missed by file
GETDATE() -- custom value missed by file
FROM #TmpTable
-- remove tmp table
DROP TABLE #TmpEventCategories