| Recently, we were working on XML having namespace and we need to read that XML to fetch the information from the XML. Here, is the Sample XML which we are trying to parse: DECLARE @x XML If we need to fetch the “info” element with T-SQL we can write query as follows: ;WITH XMLNAMESPACES(DEFAULT 'http://schemas.microsoft.com/search/local/ws/rest/v1') Here, we have to use “WITH XMLNAMESPACES”, so we can specify the namespace of the XML that we are reading, else it wont give us any result. |
Learn SQL and database management at SQLYoga for articles, tutorials, and tips to improve your skills and streamline data operations. Join our community!
July 8, 2013
SQL Yoga: Parse XML with namespace with SQL SERVER
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
May 6, 2013
SQL Yoga – Read XML with T-SQL statement in SQL SERVER
| In my previous post, “XML Result sets with SQL Server”, we review to generate result sets in XML from SQL server. Then I got a comment from the team, to also have post to read XML in SQL Server. To read XML in SQL server, is also simple. Lets read the XML which is created by XML PATH in previous post. Read XML Elements with T-SQL: DECLARE @SQLYoga TABLE( DECLARE @xml XML Now, please find query to read the query to read XML generated above: SELECT x.v.value('ID[1]', 'INT') AS ID, This query generates the output as follows: That’s it. It is much simple and you can get rid of the complex coding in application. Let me know your comments or issues you are facing while working on this. Reference: Tejas Shah (www.SQLYoga.com) |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
May 3, 2013
SQLYoga – XML Result sets with SQL Server
| Recently, found that people making much complex code to have an XML in application. I have found that they return result set in Table to the application and have applied code to convert that data table to XML by Asp. Net application. When I review the process, I have suggested that why don’t you use the XML feature of the application. They surprised the simple use of it and make their life easy. Let me have sample query to generate XML result sets in SQL Server database. There are two ways to have an XML from query: 1. FOR XML RAW 2. FOR XML PATH Please find following sample queries where both of the way are being used: Lets create sample data DECLARE @SQLYoga TABLE( Generate XML with XML ROW SELECT * XML RAW, returns the XML by each record with row element and all columns as attributes in the XML. Generate XML with XML PATH SELECT * XML PATH, returns the XML by each record with separate element and also columns as element within respected element of the record. That’s it. It is much simple and you can get rid of the complex coding in application. Let me know your comments or issues you are facing while working on this. Reference: Tejas Shah (www.SQLYoga.com) |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
August 12, 2009
SQL SERVER: Check if Node exists in XML or not
| Today, I have one requirement to check dynamically if a node exists in my xml or NOT. I have a stored procedure that receives XML and I need to check if the message information xml contains one Node or NOT. If that node exists then I need to execute that Stored Procedure by different logic and if not it should run with different logic. I figure it out by using EXISTS. This is my XML, that I got as parameter. Now I need to check if "BulkData" node exists in XML, then I need to write different logic to get the result.DECLARE @ExportData XMLSELECT @ExportData ='<Data Number="A123"><BulkData><EachData Parts="Test1" /><EachData Parts="Test2" /><EachData Parts="Test3" /></BulkData></Data>' So, I used this This will return "1" if node is exists else return "0".SELECT @ExportData.exist('(//BulkData)') That's it. I can write based on the return result by this statement. Let me know if it helps you. Reference : Tejas Shah(http://www.SQLYoga.com) |
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
May 4, 2009
SQL SERVER: Read values from Comma Separated variable
SET @xmlIDs = '' ' + REPLACE(@str, ',', ' ') + ' ' + '
1 6 7 8 20
SELECT x.v.value('.','INT')
FROM @xmlIDs.nodes('/IDs/ID') x(v)
CREATE PROC Test_ReadValuesFromCommaSeparatedVariable
@str VARCHAR(100)
AS
DECLARE @XmlIDs XML
SET @xmlIDs = '
' + REPLACE(@str, ',', ' ') + ' ' +
' '
UPDATE TableName
SET Flag = 1
WHERE ID IN(
SELECT x.v.value('.','INT')
FROM @xmlIDs.nodes('/IDs/ID') x(v)
)
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS
February 28, 2009
SQL SERVER: Generate Comma Separated List with SELECT statement
CREATE TABLE #test( field1 VARCHAR(5), field2 VARCHAR(5) )
INSERT INTO #test SELECT '001','AAA' UNION ALL SELECT '001','BBB' UNION ALL SELECT '002','CCC' UNION ALL SELECT '003','DDD' UNION ALL SELECT '004','EEE' UNION ALL SELECT '004','FFF' UNION ALL SELECT '004','GGG'
SELECT field1,
SUBSTRING(
(
SELECT ( ', ' + field2)
FROM #test t2
WHERE t1.Field1 = t2.Field1
ORDER BY t1.Field1, t2.Field1
FOR XML PATH('')
), 3, 1000)
FROM #test t1
GROUP BY field1
18+ years of Hands-on Experience
MICROSOFT CERTIFIED PROFESSIONAL (Microsoft SQL Server)
Proficient in .NET C#
Hands on working experience on MS SQL, DBA, Performance Tuning, Power BI, SSIS, and SSRS