0
我有兩個colums ID和權利的表。 Rights列將保留250個固定字符,如'010111100000000 .... 250次'。現在,我必須拆分權限列字符串,並將結果存儲在臨時表中,其結構ID,權限(0或1),位置(1到250)。假設我最初有5行,然後在臨時表中,我將得到5 * 250 = 1250行。cte在與臨時表和拆分字符串的sql服務器
我已經拆分了單個字符串並使用了遊標,但現在我想避免遊標。我如何才能做到這一點。
declare @temp table(Chars int not null, RowID int not null)
--Split the rights string into 250 char rows with RowID as each position
;with cte as
(
select substring(@rights, 1, 1) as Chars,
stuff(@rights, 1, 1, '') as rights,
1 as RowID
union all
select substring(rights, 1, 1) as Chars,
stuff(rights, 1, 1, '') as rights,
RowID + 1 as RowID
from cte
where len(rights) > 0
)
--Get the values in a temporary table except 0
insert into @temp select Chars, RowID from cte option (MAXRECURSION 300);
非常感謝它爲我工作 – Anurag