Processing xml files and push data to sql table - sql

may anyone please help me with this.
How can i parse the below xml to table.
i want data under note to be concatenate and seperated by (;)
for eg:-
App action="A" id="9951" there are two note
Reflector
Packaging Type: Box
so in table under column note it should be (Reflector;Packaging Type: Box)
similarly for qual
all text under need to be semicolon seperated.
xml data
DECLARE #DociD INT,
#XML NVARCHAR(MAX) =
'<root><App action="A" id="1">
<BaseVehicle id="95989"/>
<EngineBase id="2572"/>
<Qty>2</Qty>
<Note>Power</Note>
<Note>Textured Finish</Note>
<Note>w/Heat</Note>
<Note>wo/Turn Signal</Note>
<Note>w/Puddle Lamps</Note>
<Note>wo/Dimming</Note>
<PartType id="11618"/>
<MfrLabel>Professional Grade</MfrLabel>
<Position id="23"/>
<Part>816-8130</Part>
</App>
<App action="A" id="2">
<BaseVehicle id="8198"/>
<Qty>2</Qty>
<PartType id="11618"/>
<MfrLabel>Professional Grade</MfrLabel>
<Position id="23"/>
<Part>816-8130</Part>
</App>
<App action="A" id="3">
<BaseVehicle id="8197"/>
<Qty>2</Qty>
<PartType id="11618"/>
<MfrLabel>Professional Grade</MfrLabel>
<Position id="23"/>
<Part>816-8130</Part>
</App>
<App action="A" id="11840">
<BaseVehicle id="3723" />
<Note>Power</Note>
<Note>Textured Finish</Note>
<Note>w/Heat</Note>
<Note>wo/Turn Signal</Note>
<Note>w/Puddle Lamps</Note>
<Note>wo/Dimming</Note>
<Qty>1</Qty>
<PartType id="13117" />
<Position id="2" />
<Part>955-1147</Part>
</App>
</root>';
EXEC sys.sp_xml_preparedocument
#DociD OUTPUT,
#XML;
SELECT *
FROM OPENXML(#DociD,'/root/App',3)
WITH
(appaction CHAR(1) '#action',id INT '#id',
BaseVehicleID INT './BaseVehicle/#id',
Note varchar(20) './Note/#id'
);
Table structure
DECLARE #TABLE TABLE
(
[App action] VARCHAR (50),
[APPID] VARCHAR (50),
[BaseVehicleid] VARCHAR (50),
[Qual] VARCHAR (50),
[Qty] VARCHAR (50),
[PartTypeID] VARCHAR (50),
MfrLabel VARCHAR (50),
PositionID VARCHAR (50),
Part VARCHAR (50),
[param value] VARCHAR (50),
SubModelID VARCHAR (50),
EngineBaseID VARCHAR (50),
EngineVINID VARCHAR (50),
RecordCount VARCHAR (50),
Note VARCHAR (50)
)
but note column is coming as NULL. Can anyone please suggest.

Related

SQL- missing comma in insert statement line

CREATE TABLE suppliers
(
supplier_ID NUMBER not null,
supplier_Name varchar2 (50) not null,
supplier_Addr varchar2 (100),
supplier_Town varchar2 (30),
supplier_State varchar2 (30),
supplier_Pcode varchar2 (15) not null,
supplier_Phone1 varchar2 (15),
supplier_Phone2 varchar2 (15),
supplier_Contact varchar2 (50),
supplier_Fax varchar2 (15),
supplier_Email varchar2 (40),
supplier_Renew DATE,
supplier_Creditlimit NUMBER,
supplier_Comments varchar2 (500),
PRIMARY KEY (supplier_ID)
);
INSERT INTO suppliers (supplier_ID, supplier_Name, supplier_Addr, supplier_Town, supplier_State, supplier_Pcode, supplier_Phone1, supplier_Phone2, supplier_Contact, supplier_Fax, supplier_Email, supplier_Renew, supplier_Creditlimit,supplier_Comments)
VALUES (010203, 'DSHK COMPANY', 'G 7 JLN TUNKU HASSAN ', 'Seremban', 'Negeri Sembilan', 70000, 604-42449268, 09-4265050, 'DSHK CO', 04-2224568, 'DSHKCOMPANY#GMAIL.COM', 2024-08-23, 1000, 'COMPANY WILL CALL BEFORE SENDING THE GOODS');
I keep getting missing comma error for insert statement. Please guide me, tq
You need to make the phone numbers into strings as you have defined them as varchar.
70000,604-42449268,09-4265050
->
'70000', '604-42449268', '09-4265050'
I think you need to do tha same for date... that is 2024-08-23 -> '2024-08-23'
Assuming that you are using an Oracle database since you are using VARCHAR2 data types then use string and date literals for VARCHAR2 and DATE data types respectively:
insert into suppliers (
supplier_ID,
supplier_Name,
supplier_Addr,
supplier_Town,
supplier_State,
supplier_Pcode,
supplier_Phone1,
supplier_Phone2,
supplier_Contact,
supplier_Fax,
supplier_Email,
supplier_Renew,
supplier_Creditlimit,
supplier_Comments
) values (
010203,
'DSHK COMPANY',
'G 7 JLN TUNKU HASSAN ',
'Seremban',
'Negeri Sembilan',
'70000',
'604-42449268',
'09-4265050',
'DSHK CO',
'04-2224568',
'DSHKCOMPANY#GMAIL.COM',
DATE '2024-08-23',
1000,
'COMPANY WILL CALL BEFORE SENDING THE GOODS'
);
If you don't then, for example, 604-42449268 will be parsed as 604 subtract 42449268 which gives the result −42448664.
Do not use a string literal such as '2024-08-23' for the date as Oracle will implicitly try to convert the string literal to a date using the NLS_DATE_FORMAT session paramter. The default Oracle DATE format depends on the territory you are in and for most territories that implicit conversion will fail. Either use a DATE literal or explicitly call TO_DATE('2024-08-23', 'YYYY-MM-DD') with a format model.
db<>fiddle here
Regarding the create statement: NUMBER is not a valid type. You can use e.g. int(11) unsigned.
Also, instead of varchara(500) you can use text. So the create statement should be:
CREATE TABLE suppliers
(
supplier_ID int(11) not null,
supplier_Name varchar (50) not null,
supplier_Addr varchar (100),
supplier_Town varchar (30),
supplier_State varchar (30),
supplier_Pcode varchar (15) not null,
supplier_Phone1 varchar (15),
supplier_Phone2 varchar (15),
supplier_Contact varchar (50),
supplier_Fax varchar (15),
supplier_Email varchar (40),
supplier_Renew DATE,
supplier_Creditlimit int(11),
supplier_Comments text,
PRIMARY KEY (supplier_ID)
);
For your insert statement you must enclose all non-numerical values in quotes, like this:
INSERT INTO suppliers (supplier_ID, supplier_Name, supplier_Addr, supplier_Town, supplier_State, supplier_Pcode, supplier_Phone1, supplier_Phone2, supplier_Contact, supplier_Fax, supplier_Email, supplier_Renew, supplier_Creditlimit,supplier_Comments)
VALUES (010203, 'DSHK COMPANY', 'G 7 JLN TUNKU HASSAN ', 'Seremban', 'Negeri Sembilan', 70000, '604-42449268', '09-4265050', 'DSHK CO', '04-2224568', 'DSHKCOMPANY#GMAIL.COM', '2024-08-23', 1000, 'COMPANY WILL CALL BEFORE SENDING THE GOODS');

Inserting XML data into SQL Table with multiple nodes

So I have the following XML:
<NIACList>
<NIAC>
<Number></Number>
<SubmissionDate></SubmissionDate>
<ExpirationDate />
<IssuerIDNO></IssuerIDNO>
<IssuerName></IssuerName>
<SuspensionPeriod/>
<Cessation>
<Basis />
<Date />
</Cessation>
<Merchant>
<IDNx></IDNx>
<Name></Name>
<Address>
<Region></Region>
<Locality></Locality>
<Street></Street>
<House></House>
<Block />
<Flat />
<Phone />
<Fax />
<Email />
</Address>
</Merchant>
<CommercialUnit>
<IDNx />
<Name />
<Type></Type>
<Area></Area>
<Location></Location>
<Address>
<Region></Region>
<Locality></Locality>
<Street></Street>
<House></House>
<Block />
<Flat />
</Address>
<Activities>
<Activity>
<Code></Code>
<Name></Name>
</Activity>
</Activities>
<Goods>
<Good>
<Name></Name>
</Good>
</Goods>
<WorkProgram />
<PublicSupplyUnit>
<Capacity />
<TerraceCapacity />
</PublicSupplyUnit>
<TradingAlcohol />
<TradingBeer />
<TradingTobaccoProducts />
<AmbulatoryTrading />
<MobileUnitTrading></MobileUnitTrading>
<MobileUnit>
<Type />
<Length />
<Width />
<Height />
</MobileUnit>
<CommercialApparatusTrading></CommercialApparatusTrading>
<CommercialApparatus>
<Count />
<Length />
<Width />
<Height />
</CommercialApparatus>
</CommercialUnit>
<Modifications />
</NIAC>
</NIACList>
This is the script for the tables I created:
create table Merchant (
IdMerchant int identity primary key,
IDNX nvarchar(max) null,
Name nvarchar(max) null,
WorkProgram datetime2 null,
IdAddress int
);
create table Address (
IdAddress int identity primary key,
Region nvarchar(60) null,
Locality nvarchar(50) null,
Street nvarchar (60) null,
House nvarchar (10) null,
Block nvarchar (10) null,
Flat nvarchar(10) null,
Phone nvarchar(30) null,
Fax nvarchar(60) null,
Email nvarchar(60) null
);
create table CommercialUnit (
IDCommercialUt int identity primary key,
IDNx nvarchar(90) null,
Name nvarchar(90) null,
Type nvarchar(90) null,
Area int null,
Location nvarchar(50) null,
TerraceCapacity float null,
TradingAlcohol bit null,
TradingBeer bit null,
TradingTobaccoProducts bit null,
AmbulatoryTrading bit null,
MobileUnitTrading bit null,
CommercialApparatusTrading bit null,
IDActivities int ,
IDGoods int ,
IDMobileUnit int ,
IDCommercial int ,
IDPSU int
);
I'm not very good at XML, but here is the question:
I have tables Merchant and Address.The problem is, that the node Address is repeated 2 times(both in Merchant and CommercialUnit nodes), and has different data.My task is to specify somehow the insert, so the data that I want to insert will be divided into 2 categories, one for the Merchant node, and another for the CommercialUnit.After inserting into Address,the records must be linked with the Foreign Key from Merchant and CommercialUnit(IdAddress), so the data will be inserted here also.
I've tried to insert the data, but it inserted from the CommercialUnit node.
Below is the code for inserting:
INSERT INTO Address(Region,Locality,Street,House,Block,Flat,Phone,Fax,Email)
SELECT
Region=c.value('Region[1],','nvarchar(60)'),
Locality=c.value('Locality[1],','nvarchar(50)') ,
Street=c.value('Street[1],','nvarchar(60)') ,
House=c.value('House[1],','nvarchar(10)') ,
Block=c.value('Block[1],','nvarchar(10)') ,
Flat=c.value('Flat[1],','nvarchar(10)') ,
Phone=c.value('Phone[1],','nvarchar(30)') ,
Fax=c.value('Fax[1],','nvarchar(60)') ,
Email=c.value('Email[1],','nvarchar(60)')
FROM #xml.nodes('/NIACList/NIAC/Merchant/Address') Address(c)
You need to do four INSERT statements sequentially:
INSERT INTO Address ... (for Merchant), and capture its newly generated identity value into a variable.
INSERT INTO Merchant ... and use the variable from above.
INSERT INTO Address ... (for CommercialUnit), and capture its newly
generated identity value into a variable.
INSERT INTO CommercialUnit ... and use the variable from above.
Conceptual SQL
DECLARE #IdAddress INT;
-- Merchant
INSERT INTO Address ...
-- get last IDENTITY value for the Address table
SET #IdAddress = SCOPE_IDENTITY();
INSERT INTO Merchant ...
-- CommercialUnit
INSERT INTO Address ...
-- get last IDENTITY value for the Address table
SET #IdAddress = SCOPE_IDENTITY();
INSERT INTO CommercialUnit ...
One method is using cross apply. Provided you need distinct addresses only,
SELECT distinct t.*
FROM #xml.nodes('/NIACList/NIAC') niac(n)
cross apply (
select Region=c.value('Region[1],','nvarchar(60)'),
Locality=c.value('Locality[1],','nvarchar(50)') ,
Street=c.value('Street[1],','nvarchar(60)') ,
House=c.value('House[1],','nvarchar(10)') ,
Block=c.value('Block[1],','nvarchar(10)') ,
Flat=c.value('Flat[1],','nvarchar(10)') ,
Phone=c.value('Phone[1],','nvarchar(30)') ,
Fax=c.value('Fax[1],','nvarchar(60)') ,
Email=c.value('Email[1],','nvarchar(60)')
from niac.n.nodes('Merchant/Address') ma(c)
union
select Region=c.value('Region[1],','nvarchar(60)'),
Locality=c.value('Locality[1],','nvarchar(50)') ,
Street=c.value('Street[1],','nvarchar(60)') ,
House=c.value('House[1],','nvarchar(10)') ,
Block=c.value('Block[1],','nvarchar(10)') ,
Flat=c.value('Flat[1],','nvarchar(10)') ,
Phone=c.value('Phone[1],','nvarchar(30)') ,
Fax=c.value('Fax[1],','nvarchar(60)') ,
Email=c.value('Email[1],','nvarchar(60)')
from niac.n.nodes('CommercialUnit/Address') ua(c)
) t

Work-built custom vending machine SQL assistance

At work, we have build a vending machine and i have been asked as a side-project to attempt to create a SQL database for it. It will not be used with the vending machine, but will be used for me to practise as i'm very new to SQL (1 month in).
The vending machine will use a keyfob to touch on. Once that's done, you open up the door and grab what you like. The machine has weight sensors, so whatever you take will be added to your fob amount.
Below is what i have produced. I am currently only creating the tables. Please send me your constructive critisism and corrections as it will help me greatly!
CREATE TABLE Vender (
Name varchar (50),
Description varchar (50)
);
CREATE TABLE Bay (
StockItem uniqueidentifier,
Channel integer
);
CREATE TABLE Users (
NameFirst varchar (50),
NameLast varchar (50),
Title varchar (50),
FobID uniqueidentifier,
Credential_TakeStock integer,
Credential_Addstock integer,
Credential_Admin integer
);
CREATE TABLE Reasons (
Name uniqueidentifier
);
CREATE TABLE Machine (
Name uniqueidentifier
);
CREATE TABLE StockItems (
Code integer,
Description varchar (50),
Vendor varchar (50),
LeadTime time,
QtyCurrent integer,
QtyMax integer,
QtyMin integer,
QtyCritical integer,
WeightInGrams integer,
Bay ???
);
CREATE TABLE Purchase (
DateTime datetime,
UserName varchar,
StockItem uniqueidentifier,
Reason varchar,
Machine uniqueidentifier,
Qty integer,
DepletedItemReturned bit
);
One thing to also mention is i'm not sure of the data value for Bay in StockItems.
Thank you in advance.

Argument data type varchar is invalid for argument 3 of EncryptByKey function encryption

I'm currently doing an assessment and having quite some trouble with SQL encryption, I need to encrypt three of these columns on registration which are varchar but then they will need to be converted to varbinary for AES_256 encryption. I already have the keys and certificates set up it's just the T SQL that I need help with at the moment.
CREATE PROCEDURE dbo.Register
#Email nvarchar (60),
#Password varbinary (256),
#Fname varchar (24),
#Lname varchar (24),
#DoB date,
#HomePhoneNo nvarchar (20),
#MobileNo nvarchar (20),
#SecretQuestion1 varbinary (256),
#SecretQuestion2 varbinary (256)
AS
BEGIN
IF EXISTS (SELECT #Email FROM Customers.Customers)
DROP PROCEDURE dbo.Register
ROLLBACK
OPEN SYMMETRIC KEY Password_Key_DB
DECRYPTION BY CERTIFICATE PasswordEncryption
SET NOCOUNT ON
INSERT INTO Customers.Customers
VALUES
(
#Email,
ENCRYPTBYKEY(Key_GUID('Password_Key_DB'), #Password, CONVERT (varchar, #Password)),
#Fname,
#Lname,
#DoB,
#HomePhoneNo,
#MobileNo,
ENCRYPTBYKEY(Key_GUID('Password_Key_DB'), #SecretQuestion1, CONVERT (varchar, #SecretQuestion1)),
ENCRYPTBYKEY(Key_GUID('Password_Key_DB'), #SecretQuestion2, CONVERT (varchar, #SecretQuestion2))
)
END
GO

importing into mysql mangled

I am trying to import a CSV file into MySQL 5.0 with the following line:
LOAD DATA LOCAL INFILE 'file' INTO TABLE MYTABLE FIELDS TERMINATED BY ';' ENCLOSED BY '"' ESCAPED BY '\\'
My table schema is as follows
CREATE TABLE AUCTIONS (
ARTICLE_NO VARCHAR(20),
ARTICLE_NAME VARCHAR(100),
SUBTITLE VARCHAR(20),
CURRENT_BID DECIMAL(5,2),
START_PRICE DECIMAL(5,2),
BID_COUNT VARCHAR(20),
QUANT_TOTAL VARCHAR(20),
QUANT_SOLD VARCHAR(20),
ACCESSSTARTS VARCHAR(20),
ACCESSENDS VARCHAR(20),
ACCESSORIGIN_END VARCHAR(20),
USERNAME VARCHAR(20),
BEST_BIDDER_ID VARCHAR(20),
FINISHED TINYINT,
WATCH TINYINT,
BUYITNOW_PRICE DECIMAL(5,2),
PIC_URL VARCHAR(120),
PRIVATE_AUCTION TINYINT,
AUCTION_TYPE VARCHAR(20),
ACCESSINSERT_DATE VARCHAR(20),
ACCESSUPDATE_DATE VARCHAR(20),
CAT_DESC VARCHAR(20),
CAT_PATH VARCHAR(20),
ARTICLE_DESC TEXT,
COUNTRYCODE VARCHAR(20),
LOCATION VARCHAR(20),
CONDITIONS VARCHAR(20),
REVISED TINYINT,
PAYPAL_ACCEPT TINYINT,
PRE_TERMINATED TINYINT,
SHIPPING_TO VARCHAR(20),
FEE_INSERTION DECIMAL(5,2),
FEE_FINAL DECIMAL(5,2),
FEE_LISTING DECIMAL(5,2),
PIC_XXL TINYINT,
PIC_DIASHOW TINYINT,
PIC_COUNT VARCHAR(20),
ITEM_SITE_ID VARCHAR(20),
STARTS DATETIME,
ENDS DATETIME,
ORIGIN_END DATETIME,
PRIMARY KEY ( `ARTICLE_NO` ));
An example of the data I am trying to import, which contains intentionally snipped HTML for the ARTICLE_NAME field:
"160330609276","Ed Hardy White Tiger Panther Schuhe Gr.40 18ER103W Neu","",£40.50,£1.00,5,1,0,24/04/2009 14:41:16,27/04/2009 14:41:16,27/04/2009 14:41:16,"brand-leader-europe",0,0,0,£0.00,"http://www.modestern.de/ebay/18ER103W/Galeriebild.jpg",0,1,27/04/2009 13:43:30,27/04/2009 13:43:46,"Damenschuhe","Kleidung & Accessoires","
<!DOCTYPE html PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN""
""http://www.w3.org/TR/html4/loose.dtd"">
<html>
<head>
<title>Brand-Leader-Europe - Because Fashion is our Business</title>
<meta http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1"">
</head>
<style type=""text/css"">
body {
background-color: #E2E3E0;
}
</style>
<script language=""Javascript"">
function showArtikel() {
if(document.getElementById)
document.getElementById(""artikelbeschreibung"").style.display = ""block"";
document.getElementById(""bezahlung"").style.display = ""none"";
document.getElementById(""widerrufsrecht"").style.display = ""none"";
document.getElementById(""ueberuns"").style.display = ""none"";
document.getElementById(""kontakt"").style.display = ""none"";
document.getElementById(""agb"").style.display = ""none"";
document.getElementById(""impressum"").style.display = ""none"";
}
<!-- AfterbuyListing -->",77,"Berlin",1,0,1,0,77,£0.25,£0.00,£0.25,0,0,1,77
I am not responsible for the data being imported.
This data is exported to a CSV file on a machine with no network connection, so importing into the db directly is not an option. I have tried with the ESCAPED BY clause, which does not help much.
I would like to know how I can either import the data correctly, or export the data to a CSV file in a way that can be imported directly. The CSV file is exported from a Microsoft Access 2003 database, using UTF-8, commas for delimiters, and fields enclosed by '"'.
Your LOAD is saying FIELDS TERMINATED BY ';' while everything else suggests the terminator is ',' instead, could that be the problem?