infinitizon
MIS
Hi all,
I have a table-valued function that is supposed to return all records in a table thus:
I have a table-valued function that is supposed to return all records in a table thus:
Code:
CREATE FUNCTION TransItems (@interval INT)
RETURNS @testTable TABLE
(
No_ varchar (50) null,
[Document No_] varchar (50) null,
[Status]varchar (50) null,
[Person Responsible] varchar (50) null,
[H.O.D] varchar (50) null,
[Description] varchar (50) null,
[Job desc] varchar (50) null,
[Cust No] varchar (50) null,
[Job Category] varchar (50) null,
[Customer Name] varchar (50) null,
[Customer Addy] varchar (50) null,
[Creation Date] varchar (50) null,
[End Date] varchar (50) null,
[Start Date] varchar (50) null,
[Starting Date] varchar (50) null,
[Ending Date] varchar (50) null,
[Job Posting Group] varchar (50) null,
[Posting Group] varchar (50) null,
[Total Cost] varchar (50) null,
[Total Price] varchar (50) null,
[period1] varchar (50) null,
[period2] varchar (50) null,
[period3] varchar (50) null,
[period4] varchar (50) null,
[period5] varchar (50) null
)
AS
BEGIN
DECLARE @interval2 INT, @interval3 INT, @interval4 INT, @interval5 INT, @incr1 INT, @incr2 INT,@incr3 INT,@incr4 INT,@incr5 INT;
DECLARE @No VARCHAR(50),@DocNO VARCHAR(50),@stat VARCHAR(50),@PersonResponsible VARCHAR(50),
@HOD VARCHAR(50),@Description VARCHAR(50),@Jobdesc VARCHAR(50),@CustNo VARCHAR(50),@JobCategory VARCHAR(50)
,@CustomerName VARCHAR(50),@CustomerAddy VARCHAR(50),@CreationDate DATETIME, @EndDate DATETIME, @StartDate DATETIME
, @StartingDate DATETIME, @EndingDate DATETIME, @JobPostingGroup VARCHAR(50),@PostingGroup VARCHAR(50)
, @TotalCost VARCHAR(50), @TotalPrice VARCHAR(50) , @1 VARCHAR(50), @2 VARCHAR(50), @3 VARCHAR(50), @4 VARCHAR(50), @5 VARCHAR(50)
SELECT @No=a.[No_], @DocNO=b.[Document No_], @stat=a.[Status], @PersonResponsible=a.[User ID]
, @HOD=a.[Person Responsible], @Description=a.Description, @Jobdesc=b.[Description],@CustNo=a.[Bill-to Customer No_]
,@JobCategory=(CASE a.[Job Category]
WHEN 0 THEN ''
WHEN 1 THEN 'Field Service' WHEN 2 THEN 'Workshop'
WHEN 3 THEN 'Mining' WHEN 4 THEN 'Gas'
WHEN 5 THEN 'Project & Installation' WHEN 6 THEN 'Marine'
WHEN 7 THEN 'Contract' WHEN 8 THEN 'Premises'
WHEN 9 THEN 'Warranty' WHEN 10 THEN 'Rental'
WHEN 11 THEN 'Canopy' WHEN 12 THEN 'Part Sales'
ELSE '' END),
@CustomerName=a.[Bill-to Name],@CustomerAddy=(a.[Bill-to Address]+' '+a.[Bill-to Address 2]+' '+a.[Bill-to City]),
@CreationDate=a.[Creation Date], @EndDate=a.[Ending Date], @StartDate=a.[Starting Date],
@StartingDate=MIN(b.[Posting Date]), @EndingDate=MAX(b.[Posting Date]), @JobPostingGroup=a.[Global Dimension 1 Code],
@PostingGroup=a.[Job Posting Group], @TotalCost=SUM([Total Cost]), @TotalPrice=SUM([Total Price])
,@1=(CASE WHEN b.[Posting Date] < DATEADD(day, @interval, dbo.getMinPostingDate ( a.[No_])) THEN SUM([Total Cost]) END)
,@2=(CASE WHEN b.[Posting Date] between DATEADD(day, @interval, dbo.getMinPostingDate ( a.[No_])) and DATEADD(day, @interval*3, dbo.getMinPostingDate ( a.[No_])) THEN SUM([Total Cost]) END)
,@3=(CASE WHEN b.[Posting Date] between DATEADD(day, @interval*3, dbo.getMinPostingDate ( a.[No_])) and DATEADD(day, @interval*6, dbo.getMinPostingDate ( a.[No_])) THEN SUM([Total Cost]) END)
,@4=(CASE WHEN b.[Posting Date] between DATEADD(day, @interval*6, dbo.getMinPostingDate ( a.[No_])) and DATEADD(day, @interval*12, dbo.getMinPostingDate ( a.[No_])) THEN SUM([Total Cost]) END)
,@5=(CASE WHEN b.[Posting Date] > DATEADD(day, @interval*12, dbo.getMinPostingDate ( a.[No_])) THEN SUM([Total Cost]) END)
FROM [Job]a
RIGHT JOIN [Job Ledger Entry]b
ON b.[Job No_] = a.[No_]
GROUP BY a.[No_], b.[Document No_], a.[Status],a.[User ID], a.[Person Responsible], a.Description,b.[Description],
a.[Bill-to Customer No_],a.[Job Category], a.[Bill-to Name], a.[Bill-to Address], a.[Bill-to Address 2], a.[Bill-to City],
a.[Creation Date], a.[Ending Date], a.[Starting Date], b.[Posting Date], a.[Global Dimension 1 Code],a.[Job Posting Group]
INSERT INTO @testTable
VALUES
(@No,@DocNO,@stat,@PersonResponsible, @HOD,@Description,@Jobdesc,@CustNo,@JobCategory,@CustomerName,@CustomerAddy
,@CreationDate, @EndDate, @StartDate, @StartingDate, @EndingDate, @JobPostingGroup,
@PostingGroup, @TotalCost, @TotalPrice, @1, @2, @3, @4, @5)
RETURN
END
[code]
I was expecting that it will return all the records in the table join. However, it returned only the last record in it. Is my function wrong or that table-Valued functions can't do that?
Need help urgently.