Hello people,
Im new, please be nice
I have a bit of an issue with trying to write my first scalar function. So let me start at the beginning..
I am using a view to link a table back to itself on 3 fields. The depot number, an item code and the date of a record (only the date not the time hence the conversion in the code). I am linking each record back to the similar record from the day before so I can do a sum on the item quantities that subtracts the quantity from today, to the quantity from the previous day. This is an update that will run every night to populate a field within the table.
Unfortunatly this only works when there is a record everyday, for some reason there are days that are missed. Whether the job didnt complete correctly or there is another reason the data hasnt been populated I'm unsure, but I have to deal with this.
I have been told to create the function to check if the link exists to yesterday and if so use the data it finds, otherwise I need it to check the previous day to see if there is a matching record. What I have so far is...
CREATE FUNCTION [dbo].[prev_qty ]
(
-- Add the parameters for the function here
@date datetime,
@code int,
@depot int
)
RETURNS int
AS
BEGIN
DECLARE @prev_qty int
IF EXISTS (SELECT qty from table WHERE @code = code AND @depot = depot AND @date=(CONVERT(CHAR(10), DATEADD(d, - 1, table1.tDate), 103)))
SET @prev_qty = (SELECT qty from table WHERE @code = code AND @depot = depot AND @date=(CONVERT(CHAR(10), DATEADD(d, - 1, table1.Date), 103)))
ELSE
IF EXISTS (SELECT qty from table WHERE @code = code AND @depot=depot and @date=(CONVERT(CHAR(10), DATEADD(d, - 2, table1.date), 103)))
SET @prev_qty = (SELECT qty from table WHERE @code = code AND @depot=depot and @date=(CONVERT(CHAR(10), DATEADD(d, - 2, table1.date), 103)))
END
-- Return the result of the function
RETURN @prev_qty
END
Im getting syntax errors for a start, which probably means I have done somthing silly and simple to correct. But also, is this code correct? I was told to use a recursive call but as im still learning I a, have never used recursion before and b, have never written a function like this before!
Any help appreciated, and I hope I have explained myself well enough that you arent all confused just trying to work out what I am doing!
Im new, please be nice
I have a bit of an issue with trying to write my first scalar function. So let me start at the beginning..
I am using a view to link a table back to itself on 3 fields. The depot number, an item code and the date of a record (only the date not the time hence the conversion in the code). I am linking each record back to the similar record from the day before so I can do a sum on the item quantities that subtracts the quantity from today, to the quantity from the previous day. This is an update that will run every night to populate a field within the table.
Unfortunatly this only works when there is a record everyday, for some reason there are days that are missed. Whether the job didnt complete correctly or there is another reason the data hasnt been populated I'm unsure, but I have to deal with this.
I have been told to create the function to check if the link exists to yesterday and if so use the data it finds, otherwise I need it to check the previous day to see if there is a matching record. What I have so far is...
CREATE FUNCTION [dbo].[prev_qty ]
(
-- Add the parameters for the function here
@date datetime,
@code int,
@depot int
)
RETURNS int
AS
BEGIN
DECLARE @prev_qty int
IF EXISTS (SELECT qty from table WHERE @code = code AND @depot = depot AND @date=(CONVERT(CHAR(10), DATEADD(d, - 1, table1.tDate), 103)))
SET @prev_qty = (SELECT qty from table WHERE @code = code AND @depot = depot AND @date=(CONVERT(CHAR(10), DATEADD(d, - 1, table1.Date), 103)))
ELSE
IF EXISTS (SELECT qty from table WHERE @code = code AND @depot=depot and @date=(CONVERT(CHAR(10), DATEADD(d, - 2, table1.date), 103)))
SET @prev_qty = (SELECT qty from table WHERE @code = code AND @depot=depot and @date=(CONVERT(CHAR(10), DATEADD(d, - 2, table1.date), 103)))
END
-- Return the result of the function
RETURN @prev_qty
END
Im getting syntax errors for a start, which probably means I have done somthing silly and simple to correct. But also, is this code correct? I was told to use a recursive call but as im still learning I a, have never used recursion before and b, have never written a function like this before!
Any help appreciated, and I hope I have explained myself well enough that you arent all confused just trying to work out what I am doing!