I am trying to insert some information in Arabic from a XML variable into a SQL Server table.
I did something like the below. However it does not work. Could you please tell what is missing?
Create Table EmployeeTbl
(
firstname nvarchar(100),
familyName nvarchar(100)
)
Insert into EmployeeTbl (firstName, FamilyName)
select
t.x.value('(./firstname)[1]', 'nvarchar(100)') as firstname,
t.x.value('(./familyname)[1]', 'nvarchar(100)') as familyname
from
#XMLVariable.nodes('//employeexml) t(x)
Yes, You missed a single-quotes(') at last line:
Create Table EmployeeTbl
(
firstname nvarchar(100),
familyName nvarchar(100)
)
Insert into EmployeeTbl (firstName, FamilyName)
select
t.x.value('(./firstname)[1]', 'nvarchar(100)') as firstname,
t.x.value('(./familyname)[1]', 'nvarchar(100)') as familyname
from
#XMLVariable.nodes('//employeexml') t(x)
Your XML file should be like that :
<employeexml Id="1">
<firstname>بشیر</firstname>
<familyname>المنادی</familyname>
</employeexml>
<employeexml Id="2">
<firstname>یعغوب</firstname>
<familyname>سمیر</familyname>
</employeexml>
Related
This question already has answers here:
get a comma delimited string from rows [duplicate]
(2 answers)
Closed 4 years ago.
I have a table like this.
Create table #temp
(
id int,
firstname varchar(50),
lastname varchar(50)
)
insert into #temp (id, firstname, lastname)
select 1,'mit','jain'
insert into #temp (id, firstname, lastname)
select 1,'mit','jain1'
insert into #temp (id, firstname, lastname)
select 1,'mit','jain2'
insert into #temp (id, firstname, lastname)
select 2,'mit','jain3'
insert into #temp (id, firstname, lastname)
select 2,'mit','jain4'
insert into #temp (id, firstname, lastname)
select 1,'mit','jain5'
insert into #temp (id, firstname, lastname)
select 1,'mit','jain6'
I want the table to be shown as below
id firstname lastname
----------------------------------------------
1 mit jain,jain1,jain2,jain5,jain6
2 mit jain2,jain4
I have tried the query as below
select
id, firstname,
substring((Select ', '+tc1.lastname AS [text()]
From #temp tc1
Inner Join #temp c1 On c1.id = tc1.id
Where tc1.firstname = c1.firstname
Order BY tc1.lastname
For Xml Path('')), 2, 1000) 'LastName1'
from #temp
group by id, firstname
But it's not working. Please help me out
You're part of the way there. The tradition method is using STUFF:
SELECT t.id, t.firstname,
STUFF((SELECT ', ' + sq.lastname
FROM #temp sq
WHERE sq.id = t.id
AND sq.firstname = t.firstname
ORDER BY sq.lastname
FOR XML PATH('')),1,1,'') AS lastname
FROM #temp t
GROUP BY t.id, t.firstname;
There are lots of answers on SO already on how to do this though, but you have shown effort. :)
The code below is wrong on so many levels I can't list them all.
I imagine anyone trying to answer this would only make use of the test data table. Ha!
I've cobbled together several posts, bouncing between EXPLICIT, RAW and PATH and it's getting a little much.
I think someone who uses SQL to generate XML frequently would know this off the top of their head.
I don't have a preference for Ex, Raw or Path - I just need the best tool for the job. At the end of the day there will be 65 columns making up various levels of the final document.
I need code to query that table and generate this:
<batchContactList>
<contact contactID="123" action="AddOrModify">
<contactField name="FirstName">Johnny</contactField>
<contactField name="LastName">Testguy</contactField>
<contactPointList>
<contactPoint type="Email">
<contactPointField name="Label">Email</contactPointField>
<contactPointField name="Address">example#sendwordnow.com</contactPointField>
</contactPoint>
</contactPointList>
</contact>
</batchContactList>
Test Code
--Test Data
DECLARE #tvTest TABLE (contactID INT, FirstName VARCHAR(25), LastName VARCHAR(25), [type] VARCHAR(25), [address] VARCHAR(25))
INSERT INTO #tvTest (contactID, FirstName, LastName, [type], [address])
SELECT 123, 'Johnny', 'Testguy', 'email', 'example#sendwordnow.com'
UNION
SELECT 321, 'Sally', 'Testgirl', 'email', '1example#sendwordnow.com';
--Outer
SELECT
A.contactID AS "#contactID"
, 'AddOrModify' AS "#action"
FROM
#tvTest A
FOR XML PATH('contact'), ROOT('batchContactList')
--Level 1
DECLARE #xmldata XML;
SELECT #xmldata = (SELECT contactID, FirstName, LastName FROM #tvTest FOR XML PATH (''));
SELECT
ColumnName AS "#name"
, ColumnValue AS "text()"
FROM
(SELECT
i.value('local-name(.)','varchar(100)') ColumnName
, i.value('.','varchar(100)') ColumnValue
FROM
#xmldata.nodes('//*[text()]') x(i)) tmp
FOR XML PATH ('contactField'), ROOT('contact')
SELECT #xmldata = (SELECT contactID, [type], [address] FROM #tvTest FOR XML PATH (''));
--Level 2, not complete
SELECT
ColumnName AS "#name"
, ColumnValue AS "text()"
FROM
(SELECT
i.value('local-name(.)','varchar(100)') ColumnName
, i.value('.','varchar(100)') ColumnValue
FROM
#xmldata.nodes('//*[text()]') x(i)) tmp
FOR XML PATH ('contactPoint'), ROOT('contactPointList')
I use PATH, always.
Something like this should give you what you want.
select T.contactID as [#contactID],
'AddOrModify' as [#Action],
'FirstName' as [contactField/#name],
T.FirstName as [contactField],
null,
'LastName' as [contactField/#name],
T.LastName as [contactField],
(
select 'Email' as [contactPoint/#type],
'Label' as [contactPointField/#name],
T.type as [contactPointField],
null,
'Address' as [contactPointField/#name],
T.address as [contactPointField]
for xml path('contactPointList'), type
)
from #tvTest as T
for xml path('contact'), root('catchContactList')
Result:
<catchContactList>
<contact contactID="123" Action="AddOrModify">
<contactField name="FirstName">Johnny</contactField>
<contactField name="LastName">Testguy</contactField>
<contactPointList>
<contactPoint type="Email" />
<contactPointField name="Label">email</contactPointField>
<contactPointField name="Address">example#sendwordnow.com</contactPointField>
</contactPointList>
</contact>
<contact contactID="321" Action="AddOrModify">
<contactField name="FirstName">Sally</contactField>
<contactField name="LastName">Testgirl</contactField>
<contactPointList>
<contactPoint type="Email" />
<contactPointField name="Label">email</contactPointField>
<contactPointField name="Address">1example#sendwordnow.com</contactPointField>
</contactPointList>
</contact>
</catchContactList>
I would like to ask how to throw an error in SQL Server when the XML is parsed using OpenXML. Here is an example from Microsoft:
Insert Into Employee
SELECT EmployeeId, FirstName, LastName
FROM OPENXML (#hdoc, '/NewDataSet/Employee',1)
WITH (EmployeeId Integer, FirstName varchar(100), LastName varchar(100)) XMLEmployee
Where XMLEmployee.EmployeeId Not IN (Select EmployeeID from Employee)
What if the root node exists but there is no Employee in it?
Thanks
Try this one -
DECLARE #XML XML
SELECT #XML = '<NewDataSet></NewDataSet>'
IF NOT EXISTS(
SELECT 1
WHERE #XML.exist('/NewDataSet/Employee') = 1
) RAISERROR('Employee not exists', 16, 1)
Full answer (maybe wrong answer, because I need to see XML structure) -
DECLARE #XML XML
SELECT #XML = '<NewDataSet></NewDataSet>'
IF NOT EXISTS(
SELECT 1
WHERE #XML.exist('/NewDataSet/Employee') = 1
) RAISERROR('Employee not exists', 16, 1)
INSERT INTO dbo.Employee (EmployeeId, FirstName, LastName)
SELECT
t.c.value('#EmployeeId', 'INT')
, t.c.value('#FirstName', 'VARCHAR(100)')
, t.c.value('#LastName', 'VARCHAR(100)')
FROM #XML.nodes('/NewDataSet/Employee') t(c)
WHERE t.c.value('#EmployeeId', 'INT') NOT IN (
SELECT EmployeeID
FROM dbo.Employee
)
In the below sql code, what does T(C) mean? What is T and what is C?
declare #employeeData xml --this would be your XML input parameter
set #employeeData = '<employeeData>
<employee LastName="Smith" FirstName="Randolph" EmployeeID="1234567"/>
</employeeData>'
declare #xmlTable table (LastName nvarchar(255), FirstName nvarchar(255), EmployeeID int)
insert into #xmlTable (LastName, FirstName, EmployeeID)
select
C.value('#LastName','nvarchar(255)') as LastName,
C.value('#FirstName','nvarchar(255)') as FirstName,
C.value('#EmployeeID','int') as EmployeeID
from
#employeeData.nodes('/employeeData/employee') T(C)
select * from #xmlTable
Check MSDN: http://msdn.microsoft.com/en-us/library/ms188282.aspx
T - Table
C - Column
I can't get the value for the XML attribute 'Country' into my table.
What am I doing wrong?
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<CustomerDetails>
<PersonalInfo Country="USA">
<CustID>1001</CustID>
<CustLastName>Smith</CustLastName>
<DOB>2011-05-05T09:25:48.253</DOB>
<Address>
<Addr1>100 Smith St.</Addr1>
<City>New York</City>
</Address>
</PersonalInfo>
</CustomerDetails>
Here is my SQL:
Drop table #Cust
CREATE TABLE #Cust
(CustID INT, CustLastName VARCHAR(10)
, DOB DATETIME, Addr1 VARCHAR(100), City VARCHAR(10), Country VARCHAR(20))
insert into #Cust
select
c3.value('CustID[1]','int'),
c3.value('CustLastName[1]','varchar(10)'),
c3.value('DOB[1]','DATETIME'),
c3.value('(Address/Addr1)[1]','VARCHAR(100)'),
c3.value('(Address/City)[1]','VARCHAR(10)'),
c3.value('Country[1]','VARCHAR(20)')
from
(
select
cast(c1 as xml)
from
OPENROWSET (BULK 'C:\Users\wattronts\Documents\XMLImportTest.xml',SINGLE_BLOB) as T1(c1)
)as T2(c2)
cross apply c2.nodes('/CustomerDetails/PersonalInfo') T3(c3)
Select * from #Cust
Thanks for your help.
Use # to specify that you want an attribute.
T3.c3.value('#Country', 'varchar(50)')