Friday, December 4, 2015

How to select the data from table with multiple values to single parameter using XQuery



How to select the data from table with multiple values to single parameter using XQuery 

By using this below query we can get the selected data from the table with comma separated text
From this input text we split the text using xml conversion and nodes accessing 

DECLARE @Tmep AS TABLE (Sno int IDENTITY,Employee varchar(50))
INSERT INTO @Tmep VALUES('Krishna')
INSERT INTO @Tmep VALUES('Ganga')
INSERT INTO @Tmep VALUES('Ramesh')
INSERT INTO @Tmep VALUES('Suresh')

SELECT * FROM @Tmep t
DECLARE @Employee AS nvarchar(100)='Krishna,Ramesh'
DECLARE @EmployeeXML AS XML
SET @EmployeeXML = cast(('<a>'+replace(@Employee,',' ,'</a><a>')+'</a>') AS XML)


SELECT * FROM @Tmep t
WHERE t.Employee IN
(
       SELECT
    A.value('.', 'varchar(max)')
    FROM @EmployeeXML.nodes('a') AS FN(A)
)


To execute and analysis the query copy the entire code and execute in the SQL server


No comments:

Post a Comment