My Blog List

hotinit.com. Powered by Blogger.

Search This Blog

Importing XML File to Sql Server Table

Tuesday, 26 June 2012

 Here is the example on how to import an xml file in the sql server installed machine to sql server table. Steps to do this 
1. Save the xml file to a location on your drive eg.C:\xmlfiles\Students.xml
2. Create Table Students
3. Import the xml using Openrowset bulk option  and select the values in xml using xpath and xquery
    and Do a Insert into using select
4. Results
STEP 1
The contents of the saved file will be
<Students>
  <Student>
    <Id>1</Id>
    <Name>Madhan</Name>
  </Student>
  <Student>
    <Id>2</Id>
    <Name>Karthi</Name>
  </Student>
  <Student>
    <Id>3</Id>
    <Name>Bata</Name>
  </Student>
</Students>
STEP 2
The syntax to create a table
CREATE TABLE TblStudents
(
 Id INT PRIMARY KEY,
 Name VARCHAR(50)
);
STEP 3
Query to select the values from xml and to extract

INSERT INTO TblStudents (Id , Name )
SELECT students.student.query('Id').value('.', 'INT') as Id,
       students.student.query('Name').value('.', 'VARCHAR(30)') as Name
FROM (
SELECT CAST(b AS XML)
FROM OPENROWSET(
     BULK 'C:\xmlfiles\Students.xml',
     SINGLE_BLOB) AS a(b)
     ) AS a(b)
CROSS APPLY b.nodes('Students/Student') AS students(student);

STEP 4

SELECT Id, Name FROM TblStudents;

/*
Results:
Id        Name
----------- -------------
1           Madhan
2           Karthi
3           Bata

*/






xml to sql


Example1 on how to read xml in sql server

Read xml in sql server image

declare @xmlfullstring xml
set @xmlfullstring= '<root>
<name>madhan</name>
<name>kumar</name>
</root>'
select xfs.name.value('name[1]','varchar(50)') as namexml,
xfs.name.value('name[2]','varchar(50)') as namexml
from @XmlFullString.nodes('root')xfs(name)

Example2 on how to read xml in sql server




DECLARE @Details xml
    SET @Details = '<root>
    <SelectedCategory CategoryId="101">
        <ItemID>120</ItemID>
        <ItemID>256</ItemID>
      </SelectedCategory>
      <SelectedCategory CategoryId="125">
        <ItemID>158</ItemID>
        <ItemID>120</ItemID>
    </SelectedCategory>
    <SelectedCategory CategoryId="15">
            <ItemID>5986</ItemID>
            <ItemID>20</ItemID>
        <ItemID>268</ItemID>
      </SelectedCategory>
    </root>'

    SELECT  T.[CategoryID],
            T2.Loc.query('.').value('.', 'INT') Val
    FROM
        ( SELECT SelCat.CatDet.value('(@CategoryId)[1]', 'int') as CategoryID,
                 SelCat.CatDet.query('.') as ItemID
          FROM @Details.nodes('/root/SelectedCategory') as SelCat(CatDet)
         )CROSS APPLY ItemID.nodes('/SelectedCategory/ItemID') as T2(Loc)

Example3 on how to read xml in sql server




declare @studentxml xml ='
<Students>
<Student>
<Id>1</Id>
<Name>Madhan</Name>
</Student>
      <Student>
<Id>2</Id>
<Name>Karthi</Name>
</Student>
      <Student>
<Id>3</Id>
<Name>Bata</Name>
</Student>
</Students>'

SELECT X.Student.query('Id').value('.', 'INT') as studentid,
       X.Student.query('Name').value('.', 'VARCHAR(50)') as studentname
FROM @studentxml.nodes('/') as a(b)
CROSS APPLY b.nodes('Students/Student') AS X(Student);         


How to encrypt a procedure in sql server


Example Code for Stored Procedure Encryption - SQL SERVER

/*STEP1: create stored procedure*/
create procedure SSP_SelectNo
with encryption
as begin
select 1 as no
end
go
/*STEP2: Execute stored procedure*/
exec SSP_SelectNo
go
/*STEP3: Get Contents of stored procedure*/
sp_helptext 'SSP_SelectNo'

Code Explanation:

The keyword used to encrypt the stored procedure in sql server  is "with encryption". When you create a stored procedure with this keyword the stored procedure source code cannot be seen anymore.So please save a copy of your stored procedure before encrypting it. This will be useful when the sql server is installed at the client end and can avoid source code theft to some extent.

Stored Procedure Encryption in Sql Server Image


sql server procedure encryption image



Sql To Xml ( Create xml using sql select )

Sql To XML Image



Example code for you
select * from (
select 1 as id,'one' as name
union all
select 2 as id,'two' as name
union all
select 3 as id,'three' as name) as a
for xml path('units'),ROOT('root')

Result XML
<root>
  <units>
    <id>1</id>
    <name>one</name>
  </units>
  <units>
    <id>2</id>
    <name>two</name>
  </units>
  <units>
    <id>3</id>
    <name>three</name>
  </units>
</root>

Explanation : Convert Sql Select To XML

The Keyword used to convert an sql select into xml is "for xml path(),ROOT()" the parameter which goes inside the path() eg. path('units')is the tag which wraps the every single record. And the parameter which goes inside the ROOT() eg.,ROOT('root')is the one one which forms the root xml tag, i.e it wraps all the records as a whole.






What is Sql Server

Monday, 4 June 2012

Sql Server is a database software to keep the information organised. Imagine it to a Giant excel software which can handle a very large where that data can be 1. Inserting to it and 2. Retrieved from it.
Sql Server is also called as Relational Database Management Systems as the data are organised in it using relations called Public Key and Foreign Key. SQL Server can be installed only in Windows Operating System.

The Sql Server Runs as a service in Windows and it can be accessed by a GUI tool called Sql Server Management Studio or Command Prompt.

 

Blogger news

Blogroll

Most Reading

8.6/10