2011-06-22 46 views
0

所以我有以下功能。我的問題是我如何得到以下查詢來使用函數來創建一個視圖。我試圖讓p_c_id成爲@ValueID,並注意到@List。TSQL dbo.split包括一個密鑰

select p_c_id, notes from dbo.product

create FUNCTION [dbo].[Split2Value] 
     ( @Delimiter varchar(5), 
      @List  varchar(8000), 
      @ValueID bigint 
     ) 
     RETURNS @TableOfValues table 
      ( RowID smallint IDENTITY(1,1), 
      [Value] varchar(500), 
       ValueID bigint 
     ) 
    AS 
     BEGIN 

      DECLARE @LenString int 

      WHILE len(@List) > 0 
      BEGIN 

       SELECT @LenString = 
        (CASE charindex(@Delimiter, @List) 
         WHEN 0 THEN len(@List) 
         ELSE (charindex(@Delimiter, @List) -1) 
        END 
        ) 

       INSERT INTO @TableOfValues 
        SELECT substring(@List, 1, @LenString), @ValueID 

       SELECT @List = 
        (CASE (len(@List) - @LenString) 
         WHEN 0 THEN '' 
         ELSE right(@List, len(@List) - @LenString - 1) 
        END 
        ) 
      END 

      RETURN 

     END 

回答

2
select 
    SV.RowID, 
    SV.[Value], 
    ValueID 
from dbo.product as P 
    cross apply dbo.Split2Value('DELI?', P.notes, P.p_c_id) as SV