我创建的这个函数用于返回表格“ContributorDeeds”中所有记录的股票编号,该表格仅包含股票的起始编号和股票的结束编号(例如,从数字到数字)我需要获得的在该范围内的所有数字。
该功能正常,但是,它会花几分钟时间返回结果:(。
是否有更好的SQL查询来获得相同的结果?
FUNCTION [dbo].[StockNumbers] ()
RETURNS @SequenceNumbersOfShares TABLE (StockNumber NVARCHAR(50))
AS
BEGIN
DECLARE @DeedDocumentID BIGINT
DECLARE @MIN NVARCHAR(50)
DECLARE @MAX NVARCHAR(50)
--CREATE TABLE #LocalTempTable(StockNumber NVARCHAR(50))
DECLARE db_cursor CURSOR FOR
SELECT ContributorDeedsID FROM dbo.ContributorDeeds WHERE DeedFromShare IS NOT NULL AND DeedToShare IS NOT NULL
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @DeedDocumentID
WHILE @@FETCH_STATUS = 0
BEGIN
SET @MIN = (SELECT DeedFromShare FROM ContributorDeeds WHERE ContributorDeedsID = @DeedDocumentID)
SET @MAX = (SELECT DeedToShare FROM ContributorDeeds WHERE ContributorDeedsID = @DeedDocumentID)
WHILE dbo.InsertSP(@MIN, 20) <= dbo.InsertSP(@MAX, 20)
BEGIN
INSERT INTO @SequenceNumbersOfShares(StockNumber) VALUES(@MIN)
SET @MIN = @MIN + 1
END
FETCH NEXT FROM db_cursor INTO @DeedDocumentID
END
CLOSE db_cursor
DEALLOCATE db_cursor
RETURN;
END;