set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go create FUNCTION [dbo].[FunBarcodeQuantitySeperator] ( ) RETURNS @T TABLE ( Syscode int ) AS begin declare @Qty int declare @SysCode int declare CurCursor Cursor for SELECT GoodCode AS SysCode,Quantity AS Qty FROM BarcodeTbl open CurCursor -- Perform the first fetch. FETCH NEXT FROM CurCursor into @SysCode,@Qty -- Check @@FETCH_STATUS to see if there are any more rows to fetch. WHILE @@FETCH_STATUS = 0 BEGIN if not @SysCode is null while (@qty>0) begin insert into @t (syscode) values(@syscode) set @qty=@qty-1 end FETCH NEXT FROM CurCursor into @SysCode,@Qty if @qty is null set @qty=0 END ReturnLbl: return END