简体   繁体   English

导入将数据从CSV文件导出到SQL Server DB?

[英]Import Export data from a CSV file to SQL Server DB?

I am working on a web application where i have to do a import export functionality. 我正在使用Web应用程序,在其中必须执行导入导出功能。 i am new in this so i want your suggestions and there are some issues that i am facing to do this functionality, 我是新手,因此我想向您提出建议,并且在执行此功能时遇到一些问题,

So first i have a JSON array from Frontend (AngularJs) 首先,我有一个来自Frontend(AngularJs)的JSON数组

 [ { "MyName": "Shubham", "UserType": "Premium", "DialCode": "India", "ContactNumber": "9876543210", "EmailAddress": "Contact@Shubh.com" "Country": "India", "Notes": "Notes-Notes-Notes-Notes" }, { "MyName": "Shubham 2", "UserType": "Free Trial", "DialCode": "India", "ContactNumber": "123456789", "EmailAddress": "Contact2@Shubh.com" "Country": "India", "Notes": "Notes-Notes-Notes-Notes" } ] 

Now i am converting this array to a XML in NodeJs Using XMLbuilder Like This 现在,我正在使用XMLbuilder将此数组转换为NodeJs中的XML

<UserXML>
    <MyName>Shubham</MyName>
    <UserType>Premium</UserType>
    <DialCode>India</DialCode>
    <ContactNumber>9876543210</ContactNumber>
    <EmailAddress>Contact@Shubh.com</EmailAddress>
    <Country>India</Country>
    <Notes>Notes-Notes-Notes-Notes</Notes>
</UserXML>
<UserXML>
    <MyName>Shubham 2</MyName>
    <UserType>Free Trial</UserType>
    <DialCode>India</DialCode>
    <ContactNumber>123456789</ContactNumber>
    <EmailAddress>Contact2@Shubh.com</EmailAddress>
    <Country>India</Country>
    <Notes>Notes2-Notes2-Notes2-Notes2</Notes>
</UserXML>

Now i am using this XML in SQL Server to insert these 2 records into my user table now the issue is i have another table where country code and country name is saved i am using Foreign Key ref in my user table i have made a sample code 现在,我在SQL Server中使用此XML将这2条记录插入到我的用户表中,现在的问题是我在另一个表中保存了国家/地区代码和国家/地区名称。我在我的用户表中使用了外键引用,我做了一个示例代码

DROP TABLE IF EXISTS #Country
DROP TABLE IF EXISTS #user
DROP TABLE IF EXISTS #UserType


 CREATE TABLE #Country
(
    Id INT PRIMARY KEY  identity(1 ,1),
    Name VARCHAR(50) NOT NULL,
    DialCode VARCHAR(50) NOT NULL
 )
 CREATE TABLE #UserType
(
    Id INT PRIMARY KEY  identity(1 ,1),
    Name VARCHAR(50) NOT NULL
 )
 CREATE TABLE #user
(
    Id                INT PRIMARY KEY  IDENTITY(1 ,1),
    Name              VARCHAR(50) NOT NULL,
    UserTypeId        INT NOT NULL,
    DialCodeId        INT NOT NULL,
    ContactNumber     VARCHAR(50) NOT NULL,
    EmailAddress      VARCHAR(50) NOT NULL,
    CountryId         INT NOT NULL,
    Notes      VARCHAR(50) NOT NULL
   FOREIGN KEY(CountryId) REFERENCES #Country(Id),
   FOREIGN KEY(UserTypeId) REFERENCES #UserType(Id)
);



INSERT INTO #Country (Name,DialCode)
VALUES ('India','+91'), 
       ('Dubai','+971'),
       ('U.S','+1') ;
INSERT INTO #UserType (Name)
VALUES ('Premium'), 
       ('Free Trial');

CASE 1 (Working Fine) if i have a single record then there is no issue by using this apporch 情况1(工作正常),如果我只有一条记录,那么使用此处理程序就没有问题

declare @xml xml = '<UserXML>
    <Name>Shubham CASE-1</Name>
    <UserType>Premium</UserType>
    <DialCode>India</DialCode>
    <ContactNumber>9876543210</ContactNumber>
    <EmailAddress>Contact@Shubh.com</EmailAddress>
    <Country>India</Country>
    <Notes>Notes-Notes-Notes-Notes CASE-1</Notes>
</UserXML>'

Now i have to check the country name/User Type and match it with the country table to get the id 现在,我必须检查国家/地区名称/用户类型,并将其与国家/地区表匹配以获取ID

DECLARE @CountryId INT 
       ,@UserType INT
SELECT @CountryId = id FROM #Country WHERE Name LIKE ''+(select U.Items.value('./DialCode[1]','NVARCHAR(200)') as DialCode FROM @xml.nodes('/UserXML') U(Items))+'%'
SELECT @UserType = id FROM #UserType WHERE Name LIKE ''+(select U.Items.value('./UserType[1]','NVARCHAR(200)') as UserType FROM @xml.nodes('/UserXML') U(Items))+'%'


INSERT INTO #user 
                SELECT 
                U.Item.query('./Name').value('.','VARCHAR(100)') Name,
                @UserType,
                @CountryId,
                U.Item.query('./ContactNumber').value('.','VARCHAR(100)') ContactNumber,
                U.Item.query('./EmailAddress').value('.','VARCHAR(100)') EmailAddress,
                @CountryId,
                U.Item.query('./Notes').value('.','VARCHAR(100)') Notes
                FROM @Xml.nodes('/UserXML') AS U(Item)

CASE-2 (Well the Isssue is here) if i have multiple records then how can i check every node and then make a join or something like that to make my insert query work fine 案例2(问题在这里)如果我有多个记录,那么我如何检查每个节点,然后进行联接或类似的操作,以使我的插入查询工作正常

declare @xml2 xml = '<UserXML>
    <Name>Shubham CASE-2</Name>
    <UserType>Premium</UserType>
    <DialCode>India</DialCode>
    <ContactNumber>9876543210</ContactNumber>
    <EmailAddress>Contact@Shubh.com</EmailAddress>
    <Country>India</Country>
    <Notes>Notes-Notes-Notes-Notes CASE-2</Notes>
</UserXML>
<UserXML>
    <Name>Shubham 2 CASE-2</Name>
    <UserType>Free Trial</UserType>
    <DialCode>Dubai</DialCode>
    <ContactNumber>123456789</ContactNumber>
    <EmailAddress>Contact2@Shubh.com</EmailAddress>
    <Country>Dubai</Country>
    <Notes>Notes2-Notes2-Notes2-Notes2 CASE-2</Notes>
</UserXML>'

DECLARE @CountryId2 INT 
       ,@UserType2 INT
SELECT @CountryId2 = id FROM #Country WHERE Name LIKE ''+(select U.Items.value('./DialCode[1]','NVARCHAR(200)') as DialCode FROM @xml2.nodes('/UserXML') U(Items))+'%'
SELECT @UserType2 = id FROM #UserType WHERE Name LIKE ''+(select U.Items.value('./UserType[1]','NVARCHAR(200)') as UserType FROM @xml2.nodes('/UserXML') U(Items))+'%'


INSERT INTO #user 
                SELECT 
                U.Item.query('./Name').value('.','VARCHAR(100)') Name,
                @UserType,
                @CountryId,
                U.Item.query('./ContactNumber').value('.','VARCHAR(100)') ContactNumber,
--              U.Item.query('./EmailAddress').value('.','VARCHAR(100)') EmailAddress,
                @CountryId,
                U.Item.query('./Notes').value('.','VARCHAR(100)') Notes
                FROM @xml2.nodes('/UserXML') AS U(Item)

Please If you have any suggestions Or any other better approach for doing this task then help me out i am new to this so i don't know about the best approach for doing this task 请如果您有任何建议或其他更好的方法来执行此任务,然后帮帮我,我是新来的,所以我不知道执行此任务的最佳方法

You can read JSON directly and perform the JOIN 您可以直接读取JSON并执行JOIN

SELECT entity.*
 FROM OPENROWSET (BULK N'd:\temp\file.json', SINGLE_CLOB) as j
 CROSS APPLY OPENJSON(BulkColumn)
 WITH(
    MyName nvarchar(100)
   ,UserType nvarchar(100)
   ,DialCode nvarchar(100)
   ,ContactNumber nvarchar(100)
   ,EmailAddress nvarchar(100)
   ,Country nvarchar(100)
   ,Notes nvarchar(500)
 ) AS entity

More infos about import JSON documents and the OPENJSON 有关导入JSON文档OPENJSON的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM