larrydavid
Programmer
Hello,
I have a table-valued function in which I'm trying to return multiple rows per item code:
ALTER FUNCTION fn_listTestTiers
(
@dealItemCode varchar(12)
)
RETURNS @TestTiers TABLE
(
-- Columns returned by the function:
tierId int NOT NULL,
tierItem varchar(12) NOT NULL,
tierQty int NOT NULL,
tierAmt money NOT NULL
)
AS
-- Returns the tier qtys and amts for the specified item:
BEGIN
DECLARE
@tierId int,
@tierItem varchar(12),
@tierQty int,
@tierAmt money
-- Get item/deal/tier info:
SELECT
@tierId = qdaTierId,
@tierItem = @dealItemCode,
@tierQty = tierQty,
@tierAmt = tierAmt
FROM
TestTiers
WHILE (@dealItemCode IS NOT NULL)
BEGIN
INSERT @TestTiers
SELECT @tierId, @tierItem, @tierQty, @tierAmt
END
RETURN
END
The output I am expecting is three rows, here is the test query:
SELECT tierId, tierItem, tierQty, tierAmt
FROM dbo.fn_listTestTiers(012313213213)
GO
Here is the expected output:
17 031311600012 6 5.5500
18 031311600012 12 8.4500
19 031311600012 24 11.4500
The table has three rows of information I am trying to return in a select statement for a stored procedure, so I have dynamic columns for each tier in one row returned instead of hard-coding the columns.
The problem I'm having is if I test the function without the WHILE loop it returns one row, no good. If I test it with the WHILE loop it seems to be an endless loop and I have to break execution.
Any help would be greatly appreciated.
Thanks,
Larry
I have a table-valued function in which I'm trying to return multiple rows per item code:
ALTER FUNCTION fn_listTestTiers
(
@dealItemCode varchar(12)
)
RETURNS @TestTiers TABLE
(
-- Columns returned by the function:
tierId int NOT NULL,
tierItem varchar(12) NOT NULL,
tierQty int NOT NULL,
tierAmt money NOT NULL
)
AS
-- Returns the tier qtys and amts for the specified item:
BEGIN
DECLARE
@tierId int,
@tierItem varchar(12),
@tierQty int,
@tierAmt money
-- Get item/deal/tier info:
SELECT
@tierId = qdaTierId,
@tierItem = @dealItemCode,
@tierQty = tierQty,
@tierAmt = tierAmt
FROM
TestTiers
WHILE (@dealItemCode IS NOT NULL)
BEGIN
INSERT @TestTiers
SELECT @tierId, @tierItem, @tierQty, @tierAmt
END
RETURN
END
The output I am expecting is three rows, here is the test query:
SELECT tierId, tierItem, tierQty, tierAmt
FROM dbo.fn_listTestTiers(012313213213)
GO
Here is the expected output:
17 031311600012 6 5.5500
18 031311600012 12 8.4500
19 031311600012 24 11.4500
The table has three rows of information I am trying to return in a select statement for a stored procedure, so I have dynamic columns for each tier in one row returned instead of hard-coding the columns.
The problem I'm having is if I test the function without the WHILE loop it returns one row, no good. If I test it with the WHILE loop it seems to be an endless loop and I have to break execution.
Any help would be greatly appreciated.
Thanks,
Larry