/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu

Saturday, September 28, 2019

Using SQL query Insert Data From CSV File in to the SQL server database

drop table #test1
CREATE TABLE #test1(
[GID] [nvarchar](MAX) NULL,
[Staff_Name] [nvarchar](MAX) NULL,
[E_mail_Address] [nvarchar](MAX) NULL,
[OrgLevel1] [nvarchar](MAX) NULL,
[OrgLevel2] [nvarchar](MAX) NULL,
[OrgLevel3] [nvarchar](MAX) NULL,
[OrgLevel4] [nvarchar](MAX) NULL,
[OrgLevel5] [nvarchar](MAX) NULL,
[OrgLevel6] [nvarchar](MAX) NULL,
[HeaOrDeputy] [nvarchar](MAX) NULL,
[RoleType] [nvarchar](MAX) NULL,
[OrgLevelId] [nvarchar] (MAX) NULL
)

--BULK INSERT #test1

--   FROM 'D:\Master.csv'

--BULK INSERT #test1
--FROM 'D:\Master.csv'
--WITH
--(
--    FIELDTERMINATOR = ' '           -- add this
--);


--BULK INSERT #test1
--FROM 'D:\Master.csv'
--WITH (
--FIELDTERMINATOR = ',',
--ROWTERMINATOR = '\n',
--ERRORFILE = 'D:\myRubbishData.log'
--);

--BULK
--INSERT #test1
-- FROM 'D:\tempApproverMst.txt'
--WITH
--(
-- FIRSTROW = 1,
-- FIELDTERMINATOR = ' ',
-- ROWTERMINATOR = '\n', --0x0a
-- Lastrow = 30000003
--)
--GO

INSERT INTO #test1 ([GID], [Staff_Name], [E_mail_Address], [PhoneN],OrgLevel1,OrgLevel2,OrgLevel3,OrgLevel4,OrgLevel5,OrgLevel6,HeaOrDeputy,OrgLevelId)
SELECT A.[GID], A.[Staff_Name], A.[E_mail_Address], A.[PhoneN],A.OrgLevel1,A.OrgLevel2,A.OrgLevel3,A.OrgLevel4,A.OrgLevel5,A.OrgLevel6,A.HeaOrDeputy,A.OrgLevelId
FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0; Database=D:\Master.csv', 'select * from [TableName]') AS A;

SELECT * FROM #test1

No comments:

Post a Comment

My Blog List