开发者

could any one explain me the below code.

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
    开发者_StackOverflow中文版     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 Expressions to then form a SELECT statement using an INNER JOIN.

The first Common Table Expression (CTE) is using a UNION to 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 SELECT statement 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:

string               Term   Location
sjfhasjdg III do     III    10
sjfhasjdg III do     DO     14

See more on CTE's:

http://msdn.microsoft.com/en-us/library/ms190766.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜