将字符串分解为SQL Server中的多行

时间:2011-09-15 09:17:00

标签: sql-server-2008

我在SQL Server中存储了一个包含多行的字符串。

如何从具有多行的SQL Server中进行选择?

ForEx:数据存储为Line1 Line2 Line3

我如何展示

Line1 <nextLine>
Line2 <nextline>
Line3

1 个答案:

答案 0 :(得分:0)

创建功能dbo.fStringToTable(@P_KeyWordList AS Varchar(max),@ Delimeter as varchar(1))
退货
@Result
TABLE(VarcharValue Varchar(255))
AS
BEGIN
 DECLARE @V_Keyword VARCHAR(255),
 @CIndex Int
 IF(@P_KeyWordList不为空)
 BEGIN
  SET @CIndex = CHARINDEX(@Delimeter,@ P_KeyWordList)
  WHILE(@CIndex&gt; 0)
  BEGIN
   SET @V_Keyword = SUBSTRING(@ P_KeyWordList,1,@ CIndex-1)
   SET @P_KeyWordList = SUBSTRING(@ P_KeyWordList,@ CIndex + 1,LEN(@P_KeyWordList) - @ CIndex)
   INSERT INTO @Result值(@V_Keyword)
   SET @CIndex = CHARINDEX(@Delimeter,@ P_KeyWordList)
  END
  SET @V_Keyword = @P_KeyWordList
  INSERT INTO @Result值(@V_Keyword)
 END
 RETURN
END
 走 从dbo.fStringToTable中选择*('l1 l2 l3','')