我不认为您可以使用SELECt来做到这一点。最好的机会是编写一个用户定义的函数,该函数将返回包含所有单词的表,然后对其执行SELECT DISTINCT。
免责声明: 函数 dbo.Split
来自http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648
CREATE TABLE test( id int identity(1, 1) not null, description varchar(50) not null)INSERT INTO test VALUES('The dog jumped over the fence')INSERT INTO test VALUES('The giant tripped on the fence')CREATE FUNCTION dbo.Split( @RowData nvarchar(2000), @SplitOn nvarchar(5)) RETURNS @RtnValue table ( Id int identity(1,1), Data nvarchar(100)) AS BEGIN Declare @Cnt int Set @Cnt = 1 While (Charindex(@SplitOn,@RowData)>0) Begin Insert Into @RtnValue (data) Select Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1))) Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData)) Set @Cnt = @Cnt + 1 End Insert Into @RtnValue (data) Select Data = ltrim(rtrim(@RowData)) ReturnENDCREATE FUNCTION dbo.SplitAll(@SplitOn nvarchar(5))RETURNS @RtnValue table( Id int identity(1,1), Data nvarchar(100))ASBEGINDECLARE My_Cursor CURSOR FOR SELECT Description FROM dbo.testDECLARE @description varchar(50)OPEN My_CursorFETCH NEXT FROM My_Cursor INTO @descriptionWHILE @@FETCH_STATUS = 0BEGIN INSERT INTO @RtnValue SELECt Data FROM dbo.Split(@description, @SplitOn) FETCH NEXT FROM My_Cursor INTO @descriptionENDCLOSE My_CursorDEALLOCATE My_CursorRETURNENDSELECt DISTINCT Data FROM dbo.SplitAll(N' ')


