本文发表在 rolia.net 枫下论坛不要以为我懒和工作轻松,昨晚可是工作到凌晨才睡觉的。
i have to create a inline table with help of a home brew function
select a.*
from fat_table a, (select * from dbo.[ConvertCommaSeparatedStringToIntTable]('399451, 403177, 386076, xxx,xxxx,xxx')) b
where a.currencycode='USD'
and a.dealtypecode='PE'
and a.id = b.item
order by a.companyName asc, a.InvestmentDate desc
/* function */
CREATE FUNCTION [dbo].[ConvertCommaSeparatedStringToIntTable]
(
@commaSeparatedString varchar(max)
)
RETURNS @TempList TABLE
(
item int
)
AS
BEGIN
DECLARE @item varchar(50), @Pos int
SET @commaSeparatedString = LTRIM(RTRIM(@commaSeparatedString))+ ','
SET @Pos = CHARINDEX(',', @commaSeparatedString, 1)
IF REPLACE(@commaSeparatedString, ',', '') <> ''
BEGIN
WHILE @Pos > 0
BEGIN
SET @item = LTRIM(RTRIM(LEFT(@commaSeparatedString, @Pos - 1)))
IF isnumeric(@item)=1
BEGIN
INSERT INTO @TempList (item) VALUES (cast(@item as int))
END
SET @commaSeparatedString = RIGHT(@commaSeparatedString, LEN(@commaSeparatedString) - @Pos)
SET @Pos = CHARINDEX(',', @commaSeparatedString, 1)
END
END
RETURN
END更多精彩文章及讨论,请光临枫下论坛 rolia.net
i have to create a inline table with help of a home brew function
select a.*
from fat_table a, (select * from dbo.[ConvertCommaSeparatedStringToIntTable]('399451, 403177, 386076, xxx,xxxx,xxx')) b
where a.currencycode='USD'
and a.dealtypecode='PE'
and a.id = b.item
order by a.companyName asc, a.InvestmentDate desc
/* function */
CREATE FUNCTION [dbo].[ConvertCommaSeparatedStringToIntTable]
(
@commaSeparatedString varchar(max)
)
RETURNS @TempList TABLE
(
item int
)
AS
BEGIN
DECLARE @item varchar(50), @Pos int
SET @commaSeparatedString = LTRIM(RTRIM(@commaSeparatedString))+ ','
SET @Pos = CHARINDEX(',', @commaSeparatedString, 1)
IF REPLACE(@commaSeparatedString, ',', '') <> ''
BEGIN
WHILE @Pos > 0
BEGIN
SET @item = LTRIM(RTRIM(LEFT(@commaSeparatedString, @Pos - 1)))
IF isnumeric(@item)=1
BEGIN
INSERT INTO @TempList (item) VALUES (cast(@item as int))
END
SET @commaSeparatedString = RIGHT(@commaSeparatedString, LEN(@commaSeparatedString) - @Pos)
SET @Pos = CHARINDEX(',', @commaSeparatedString, 1)
END
END
RETURN
END更多精彩文章及讨论,请光临枫下论坛 rolia.net