Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need help with function

Author  Topic 

sn34
Starting Member

36 Posts

Posted - 2010-08-04 : 09:26:56
Hello
I have following function and now i need to get all data from it.
function:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER FUNCTION [dbo].[GetItemsAvailableQtyWithDate]
(
-- Add the parameters for the function here
@ItemNo VARCHAR(20),
@Location VARCHAR(10),
@Date DATETIME
)
RETURNS INT
AS
BEGIN
-- Declare the return variable here
DECLARE @Qty INT,
@ILEQty INT,
@Ret_Qty INT
IF(@Location = 'NULL')
SET @Location = NULL

IF(@Date IS NULL)
BEGIN
IF(@Location IS NULL)
BEGIN
SELECT @ILEQty = ISNULL(SUM(Quantity),0)
FROM dbo.[Main-db$Item Ledger Entry] WITH (NOLOCK)
WHERE [Item No_] = @ItemNo AND [Entry Type] = 0

SELECT @Qty = ISNULL(SUM(tse.Quantity),0)
FROM [dbo].[Main-db$Trans_ Sales Entry] tse WITH (NOLOCK)
LEFT OUTER JOIN dbo.[Main-db$Trans_ Sales Entry Status] tses WITH (NOLOCK) ON
tses.[Store No_] = tse.[Store No_] AND
tses.[POS Terminal No_] = tse.[POS Terminal No_] AND
tses.[Transaction No_] = tse.[Transaction No_] AND
tses.[Line No_] = tse.[Line No_]
WHERE tse.[Item No_] = @ItemNo AND
(tses.[Item No_] IS NULL OR tses.Status = 0)
SET @Ret_Qty = @ILEQty - @Qty
END
ELSE
BEGIN
SELECT @ILEQty = ISNULL(SUM(Quantity),0)
FROM dbo.[Main-db$Item Ledger Entry] WITH (NOLOCK)
WHERE [Item No_] = @ItemNo AND [Location Code] = @Location
AND [Entry Type] = 0

SELECT @Qty = ISNULL(SUM(tse.Quantity),0)
FROM [dbo].[Main-db$Trans_ Sales Entry] tse WITH (NOLOCK)
LEFT OUTER JOIN dbo.[Main-db$Trans_ Sales Entry Status] tses WITH (NOLOCK) ON
tses.[Store No_] = tse.[Store No_] AND
tses.[POS Terminal No_] = tse.[POS Terminal No_] AND
tses.[Transaction No_] = tse.[Transaction No_] AND
tses.[Line No_] = tse.[Line No_]
WHERE tse.[Item No_] = @ItemNo AND tse.[Location Code] = @Location
AND (tses.[Item No_] IS NULL OR tses.Status = 0)
SET @Ret_Qty = @ILEQty - @Qty
END
END
ELSE
BEGIN
IF(@Location IS NULL)
BEGIN
SELECT @ILEQty = ISNULL(SUM(Quantity),0)
FROM dbo.[Main-db$Item Ledger Entry] WITH (NOLOCK)
WHERE [Item No_] = @ItemNo AND [Posting Date] <= @Date
AND [Entry Type] = 0

SELECT @Qty = ISNULL(SUM(tse.Quantity),0)
FROM [dbo].[Main-db$Trans_ Sales Entry] tse WITH (NOLOCK)
LEFT OUTER JOIN dbo.[Main-db$Trans_ Sales Entry Status] tses WITH (NOLOCK) ON
tses.[Store No_] = tse.[Store No_] AND
tses.[POS Terminal No_] = tse.[POS Terminal No_] AND
tses.[Transaction No_] = tse.[Transaction No_] AND
tses.[Line No_] = tse.[Line No_]
WHERE tse.[Item No_] = @ItemNo AND tse.[Date] <= @Date AND
(tses.[Item No_] IS NULL OR tses.Status = 0)
SET @Ret_Qty = @ILEQty - @Qty
END
ELSE
BEGIN
SELECT @ILEQty = ISNULL(SUM(Quantity),0)
FROM dbo.[Main-db$Item Ledger Entry] WITH (NOLOCK)
WHERE [Item No_] = @ItemNo AND [Location Code] = @Location
AND [Posting Date] <= @Date AND [Entry Type] = 0

SELECT @Qty = ISNULL(SUM(tse.Quantity),0)
FROM [dbo].[Main-db$Trans_ Sales Entry] tse WITH (NOLOCK)
LEFT OUTER JOIN dbo.[Main-db$Trans_ Sales Entry Status] tses WITH (NOLOCK) ON
tses.[Store No_] = tse.[Store No_] AND
tses.[POS Terminal No_] = tse.[POS Terminal No_] AND
tses.[Transaction No_] = tse.[Transaction No_] AND
tses.[Line No_] = tse.[Line No_]
WHERE tse.[Item No_] = @ItemNo AND tse.[Date] <= @Date AND
tse.[Location Code] = @Location
AND (tses.[Item No_] IS NULL OR tses.Status = 0)
SET @Ret_Qty = @ILEQty - @Qty
END
END
-- Return the result of the function
RETURN @ILEQty
END



I need something like this:
select I.[Item No_], I.[Location Code],I.[Posting Date], F.TotalQty
from [dbo].[main-db$Item Ledger Entry] I
CROSS APPLY dbo.GetItemInventoryQuantitytestt(I.[Item No_], I.[Location Code], GetDate())F
order by I.[Posting Date] desc

is not it?
   

- Advertisement -