Could any one help me understand the below code. Please help me how it works as i am new lerning sql server.
WITH SearchTerms(Term)
AS (SELECT ' I '
UNION ALL
SELECT ' II '
UNION ALL
SELECT ' III '
UNION All
SELECT ' MD '
UNION All
SELECT ' M.D '
UNION All
SELECT ' M.D. '
UNION All
SELECT ' D.O '
UNION All
SELECT ' D.O. '
UNION All
SELECT ' DO '
),
ToBeSearched(string)
AS (SELECT 'sjfhasjdg III do ')
SELECT string,
Term,
Charindex(Term, string) AS Location
FROM ToBeSearched
JOIN SearchTerms
ON Charindex(Term, string) > 0
This query is using two
Common Table Expressionsto then form aSELECTstatement using anINNER JOIN.The first Common Table Expression (CTE) is using a
UNIONto create a set of results for the table.The second CTE contains just one record, with one column, with the value
'sjfhasjdg III do 'The
SELECTstatement at the end of the query is filtering out the results from the first CTE dependant on the record in the second CTE.The result set will be:
See more on CTE’s:
http://msdn.microsoft.com/en-us/library/ms190766.aspx