infinitizon
MIS
Hi all,
I was working with an interesting subject that took me two days to complete. However, I'm not certain of the cost of the query so I thought to post it as a question. Here it is.
First, I have a function that gets the alphabeths in a string
Then another one that gets the numbers in the same string
I used the two functions in another function that gets a string from a table, splits it, increments the number part (by a default incrementer specified), and returns the string (now incremented)
Now, I create a procedure thus
Everything works fine. However, When I look at the procedure and the number of side-effecting queries in it, I begin to wonder if this is the most efficient way to go about this?
Any Ideas please?
____________________
Men put up a strong face just to cover their weaknesses...good!
But a smile makes them live longer....
Which would you choose?
Think about it.
I was working with an interesting subject that took me two days to complete. However, I'm not certain of the cost of the query so I thought to post it as a question. Here it is.
First, I have a function that gets the alphabeths in a string
Code:
CREATE FUNCTION [dbo].[GetAlphabeths](@DATA VARCHAR(8000))
RETURNS VARCHAR(8000)
AS
BEGIN
RETURN Left(
SubString(@DATA, PatIndex('%[a-zA-Z><!=]%', @DATA), 8000),PatIndex('%[^a-zA-Z><!=]%', SubString(@DATA, PatIndex('%[a-zA-Z><!=]%', @DATA), 8000) + '1')-1)
END
Then another one that gets the numbers in the same string
Code:
ALTER FUNCTION [dbo].[GetNumbers](@DATA VARCHAR(8000))
RETURNS VARCHAR(8000)
AS
BEGIN
RETURN LEFT(
SUBSTRING(@DATA, PATINDEX('%[0-9.-]%', @DATA), 8000), PATINDEX('%[^0-9.-]%', SUBSTRING(@DATA, PATINDEX('%[0-9.-]%', @DATA), 8000) + 'X')-1)
END
I used the two functions in another function that gets a string from a table, splits it, increments the number part (by a default incrementer specified), and returns the string (now incremented)
Code:
ALTER FUNCTION [dbo].[getLstOvtUsed]()
RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @lastNoUsed int, @prepender varchar(3), @incNo int, @returnVal varchar(10)
SELECT @lastNoUsed = dbo.GetNumbers([Last No. Used]), @prepender=dbo.GetAlphabeths([Last No. Used])
, @incNo = [Increment-by No]
FROM [No_ Series Line]
SET @returnVal = @lastNoUsed+@incNo
RETURN @prepender + RIGHT('0000000' + @returnVal, 7)
END
Now, I create a procedure thus
Code:
CREATE PROCEDURE sp_insertOvtAppl
@staff_id VARCHAR(10), @dept_id VARCHAR(10), @designation VARCHAR(10)
, @1st_sup VARCHAR(50), @2nd_sup VARCHAR(50)
, @apply_date DATETIME, @start_time DATETIME, @end_time DATETIME
AS
BEGIN
DECLARE @over_id VARCHAR(10)
UPDATE [No_ Series Line] SET [Last No. Used]=dbo.getLstOvtUsed()
SELECT @over_id = [Last No. Used] FROM [No_ Series Line]
INSERT INTO OvertimeApplication
([over_id], [staff_id],[dept_id],[designation],[1st_sup],[2nd_sup],[apply_date],[start_time],[end_time])
VALUES
(@over_id, @staff_id, @dept_id, @designation, @1st_sup, @2nd_sup, @apply_date, @start_time, @end_time)
SELECT [Last No. Used] FROM [No_ Series Line]
END
Everything works fine. However, When I look at the procedure and the number of side-effecting queries in it, I begin to wonder if this is the most efficient way to go about this?
Any Ideas please?
____________________
Men put up a strong face just to cover their weaknesses...good!
But a smile makes them live longer....
Which would you choose?
Think about it.