SQL 有个函数要转成视图,求助
发布网友
发布时间:2024-05-06 17:29
我来回答
共2个回答
热心网友
时间:2024-08-28 00:09
Create View V_Prd_No_Chg
As
With T
As
(
Select Bom_No,Prd_No,Convert(Varchar(100),Prd_No_Chg) As Prd_No_Chg ,1 As ID From Tf_Bom
Union All
Select Bom_No,Prd_No,Convert(Varchar(100),Stuff(Prd_No_Chg,1,Charindex(';',Prd_No_Chg),'')) As Prd_No_Chg,ID+1 As Cnt
From T Where Charindex(';',Prd_No_Chg)>0
)
Select ID,Bom_No,Prd_No,Substring(Prd_No_Chg,1,Charindex(';',Prd_No_Chg+';')-1) As Prd_No_Chg From T
--测试
Select * from V_Prd_No_Chg Where Bom_No='011->01' And prd_no in('010','002')
Order By Bom_No,prd_no,ID
--你的不会是天心ERP吧
热心网友
时间:2024-08-28 00:10
--批量拆分字符串,以下代码适用于SQL Server 2005及以上
--步骤一:构造一张辅助的整数表Nums
--该表中保存从1开始的整数。最大值应超过需拆分的字段的最大长度
--这里Nums中保存了1到100万
--drop table Nums;
GO
create table Nums(n int primary key);
GO
declare @m int;
insert into Nums select 1;
set @m=1;
while (2*@m<1000000)
begin
insert into Nums
select n+@m from Nums;
set @m=2*@m;
end
insert into Nums
select n+@m from Nums
where n+@m<=1000000;
--步骤二:批量拆分字符串
--测试数据:
with tmp(bom_no,prd_no,prd_no_chg) as (
select '1010100006','1020600002','' union all
select '1010100006','1020400002','00001;00003')
--批量拆分
select bom_no,prd_no,prd_no_chg from (
SELECT bom_no,prd_no,
SUBSTRING(prd_no_chg, n, CHARINDEX(';', prd_no_chg + ';', n) - n) AS prd_no_chg
FROM tmp
JOIN Nums
ON n <= LEN(prd_no_chg)
AND SUBSTRING(';' + prd_no_chg, n, 1) = ';'
) as t where prd_no_chg<>'';
结果: