sql 数据库根据数量拆分为与数量相等的行数(函数)

来源:互联网 发布:linux中tcpdump 用法 编辑:程序博客网 时间:2024/06/07 04:59
 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fn_StrToTable]') and xtype in (N'FN', N'IF', N'TF'))  
drop function [dbo].[fn_StrToTable]  
GO  
Create Function fn_StrToTable(@djbh int)  
    Returns @tableName Table
    (
        billno int,
        billsn int,
        num int
    )
As  
Begin  
declare @billno int
declare @billsn int
declare @num int
declare @index int
declare pcurr cursor for select billno,billsn,num from purindt where billno = @djbh
open pcurr  
fetch next from pcurr into @billno,@billsn,@num  
while (@@fetch_status = 0)  
begin
set @index = 0
begin
while (@index < @num)
begin
Insert @tableName Values(@billno,@billsn,1)
set @index = @index + 1;
end
end
fetch next from pcurr into @billno,@billsn,@num  
end  
close pcurr
deallocate pcurr 
    
    Return  
End
0 0
原创粉丝点击