Call User defined function as System Function in SQL server - sql

I have created an user defined functions in SQL server. Is it possible to call these function in select statement, just like any other built-in function?
Create function [dbo].[TIME_TO_SEC](#inputTime time)
returns INT
As
Begin
DECLARE #secDiff INT = datediff(second, '00:00:00', #inputTime)
return #secDiff;
end
Currently calling this as below:
DECLARE #ret int;
EXEC TIME_TO_SEC #inputTime='01:00:00'
select #ret;
I am attempting something as simple as:
select TIME_TO_SEC('01:00:00') from TimeTable
Is it feasible in SQL-server?

It's possible, but you need to reference the function by the user's schema
select dbo.TIME_TO_SEC('01:00:00') from TimeTable

if you get an issue with your function, try to add "WITH EXECUTE AS CALLER"
Create function [dbo].[TIME_TO_SEC](#inputTime time)
Returns int
WITH EXECUTE AS CALLER
As
Begin
DECLARE #secDiff INT = datediff(second, '00:00:00', #inputTime)
return #secDiff;
end
then call the function with select
select dbo.[TIME_TO_SEC](getdate())

Yes, it's certainly feasible, and it looks like you've got everything you need already. Have you tried it?

Related

Convert Query to Stored Procedure

In db2, can someone show me how to convert a query into a stored procedure, so that when the stored procedure is called it simply returns the same result set as the query would return.
For example, let say I have query like this:
select * from tablename
How would you turn this into a stored procedure that returns the same record set as is returned by just simply running the query directly?
Just create a procedure
Format is as follows
CREATE PROCEDURE <<SPmyProcedure>>
AS
BEGIN
<---procedure definition here--->
select * from tablename
END
Then you can just call the procedure name and it will perform your query.
via
exec SPmyProcedure
Why you dont use table valued function like this?
ALTER FUNCTION [db].[fn_tablename]
(
)
RETURNS TABLE
AS
RETURN
(
SELECT * from [db].tablename
)

How to execute function in SQL Server 2008

I build a function and I am trying to execute it...but some errors are occurring
CREATE FUNCTION dbo.Afisho_rankimin(#emri_rest int)
RETURNS int
AS
BEGIN
Declare #rankimi int
Select #rankimi=dbo.RESTORANTET.Rankimi
From RESTORANTET
Where dbo.RESTORANTET.ID_Rest=#emri_rest
RETURN #rankimi
END
GO
SELECT dbo.Afisho_rankimin(5)AS Rankimi
GO
The errors when I execute it are:
Msg 2714, Level 16, State 3, Procedure Afisho_rankimin, Line 11
There is already an object named 'Afisho_rankimin' in the database.
and also it is said that:
Can not find column "dbo", or the user defined function, or aggregate "dbo.Afisho_rankimin", or the name is ambiguous
It looks like there's something else called Afisho_rankimin in your DB so the function is not being created. Try calling your function something else. E.g.
CREATE FUNCTION dbo.Afisho_rankimin1(#emri_rest int)
RETURNS int
AS
BEGIN
Declare #rankimi int
Select #rankimi=dbo.RESTORANTET.Rankimi
From RESTORANTET
Where dbo.RESTORANTET.ID_Rest=#emri_rest
RETURN #rankimi
END
GO
Note that you need to call this only once, not every time you call the function. After that try calling
SELECT dbo.Afisho_rankimin1(5) AS Rankimi
I have come to this question and the one below several times.
how to call scalar function in sql server 2008
Each time, I try entering the Function using the syntax shown here in SQL Server Management Studio, or SSMS, to see the results, and each time I get the errors.
For me, that is because my result set is in tabular data format. Therefore, to see the results in SSMS, I have to call it like this:
SELECT * FROM dbo.Afisho_rankimin_TABLE(5);
I understand that the author's question involved a scalar function, so this answer is only to help others who come to StackOverflow often when they have a problem with a query (like me).
I hope this helps others.
you may be create function before so,
update your function again using.
Alter FUNCTION dbo.Afisho_rankimin(#emri_rest int)
RETURNS int
AS
BEGIN
Declare #rankimi int
Select #rankimi=dbo.RESTORANTET.Rankimi
From RESTORANTET
Where dbo.RESTORANTET.ID_Rest=#emri_rest
RETURN #rankimi
END
GO
SELECT dbo.Afisho_rankimin(5) AS Rankimi
GO

Overwrite the return value of a Stored Procedure

Usually a Stored Procedure return 0 on success execution without errors, and -1 or etc when the stored procedure encounter errors.
Is there anyway I can overwrite the int return value in the Stored Procedure?
Here is function description that will give you any return what you want.
Or if you need write access, you can add output parameters in you storedprocedure
Something like this
CREATE PROCEDURE dbo.MyStoredProcedure (
#myInput NVARCHAR(50),
#myOutput INT OUTPUT )
AS
SELECT #myOutput = COUNT(*)
FROM MyTable
WHERE MyColumn = #myInput
SP can return what ever you want to return by using select statement but ideally you should use functions when you need to return a single value.
Answer to your problem is yes!!
use the return keyword:
return 42

solution for generating a running sequence number by using stored procedure in sql server 2008

how to generate a running sequence number from 0001 to 9999 by through using a function in sql server, if u call that function in sql server, it should to be provide a running sequence number when ever if it reach 9999 again it should to be reset 0001
can any help on this please to perform this above task
There are plenty of good articles about that. Try Google.
For instance see here and there
A function can't update, insert or delete, so you can't store the variable and check it's value next time you call the function. So there is no way of doing this using a function.
You can simply get the sequence number using following procedure:
CREATE PROCEDURE proc_GetSeqNumber
AS
BEGIN
DECLARE #idt INT
SET #idt = 0
WHILE (#idt < 9999)
BEGIN
SELECT #idt = #idt + 1
PRINT #idt
END
END
The only way a database can remember a value is if it is stored in a table (in Oracle, you could use a sequence, but SQL Server doesn't use those.) So I would create a single table with a single value and the function would read the value, increment it, and if need be reset it.
if #ItemsCount=0
begin
set #generatedSeNumber=1
end
else
begin
SET #sql= 'insert into Senumber values(('+#SeNumber+'))'
Exec (#sql)

SQL Looping through results

I do a select and it brings back a list of IDs. I then want to call another procedure for each ID that calculates a result, so I end up with a list of results.
How can I do this? I need some kind of loop but I am not very good at SQL.
Edit: Microsoft SQL 2008, and purely in SQL
Write a user defined function that takes in the ID and returns the calculated result you can then get that result for each ID with a query like this:
SELECT id, DatabaseYouUsed.dbo.functionYouWrote(id)
FROM DatabaseYouUsed.dbo.TableWithIDs
You can have a stored procedure that calls the select to get the IDs, use a cursor on the result list and call the other procedure for each ID. All inside a stored procedure.
If one row generates one result:
CREATE FUNCTION f(x int) RETURNS int AS BEGIN
RETURN x * 2
END
GO
SELECT id, x, dbo.f(x) FROM my_table
If one row might generate more than one result:
CREATE FUNCTION f(x int) RETURNS #r TABLE(result int) AS BEGIN
INSERT INTO #r VALUES(x)
INSERT INTO #r VALUES(x * 2)
RETURN
END
GO
SELECT t.id, t.x, r.result FROM my_table t CROSS APPLY dbo.f(t.x) r