SQL Function within Function - sql

I have a function that gets the recipeid. I am creating another function that shows the average price with recipename. How do I use the first function within the second function?
This is what I have so far. As can be seen, I am using a join for RecipeID, but I want it to use the 1st function I have (let's call it function1) that gets the ID instead. How do I go about this?
CREATE FUNCTION AveragePriceforRecipe() RETURNS #PriceTable TABLE (
[Amount] smallmoney,
[RecipeName] nvarchar(75)) AS
BEGIN
INSERT INTO #PriceTable
SELECT AVG(Amount)
FROM Prices JOIN
RecipePrices ON Prices.PriceID = RecipePrices.PriceID JOIN
Recipes ON RecipePrices.RecipeID = Recipes.RecipeID
END

are you using ms sql server (t-sql)? if so, please try the code below:
given that function1 is:
CREATE FUNCTION function1(#RecipeName)
RETURNS INT
AS
BEGIN
DECLARE #RecipeID INT
SELECT #RecipeID = ID
FROM Recipes
WHERE RecipeName = #RecipeName
RETURN #RecipeID
END
getting the recipes price table using function1:
CREATE FUNCTION AveragePriceforRecipe (#Amount SMALLMONEY, #RecipeName NVARCHAR(75))
RETURNS #PriceTable TABLE
AS
BEGIN
INSERT INTO #PriceTable
SELECT AVG(Amount)
FROM Prices
JOIN RecipePrices ON Prices.PriceID = RecipePrices.PriceID
AND RecipePrices.RecipeID = function1(#RecipeName)
END

Related

Pass Table as parameter to function

I have a temp table which contains data needed in selection, but I can't join in to main select statement because it contains too many rows, and grouping is not good enough. So I decided to use values directly from my temp table in the selection, and it works fine. But since I need to add selection from temp table 50 times as sub queries in that main selection, I was considering to move it to function, as is nicer to call a function 50 times than sub queries.
My question is how to pass values from that table to function? Function will be also supplied with other parameters needed for extraction exact value from that table. I know I can't pass temp table as a parameter, but what about table variable? I don't care if I use temp table or a table variable..
Firstly I wrote a function containing the select statement same as for temp table, and it works but too slow. So if I could pass table results to a function, it will speed up the process..
My function now look like this:
ALTER FUNCTION [document].[GetPersonPremium]
(
-- Add the parameters for the function here
#DocumentId bigint,
#PersonId bigint,
#PeriodId int,
#PersonRole nvarchar(20)
)
RETURNS DECIMAL (18,2)
AS
BEGIN
-- Declare the return variable here
DECLARE #premiumSum decimal (18,2)
-- Add the T-SQL statements to compute the return value here
set #premiumSum =
(select top 1 pt.Premium from document.Document d
inner join document.Person p on p.DocumentCalculationLayerID = dcl.DocumentCalculationLayerID
inner join document.PersonTasks pt on pt.PersonId = p.PersonId
inner join document.PersonCalculationHelper pch on pch.PersonTaskId = pt.PersonTaskId
inner join document.PersonTaskCalculationHelper ptch on ptch.PersonId = p.PersonId
inner join document.PersonMarkTypes pmt on pmt.ConcernMarkTypeID = ptch.ConcernMarkTypeId
where dcl.DocumentID = #DocumentId and p.PersonId = #PersonId and pch.PeriodId = #PeriodId and pmt.Name = #PersonRole)
-- Return the result of the function
RETURN #premiumSum
END
And I would like to use it from stored procedure like this:
...
Engineer = Coalesce(document.GetPersonPremium(#DocumentId, p.PersonID, 65, 'Intern'), 0.00),
...
Any suggestion?
The answer is for those who come to this page with same or similar problem.
I created a table type:
CREATE TYPE PremiumTableType AS TABLE
(
PersonId bigint,
PeriodId int,
PersonRole nvarchar(20),
)
Included it in function:
ALTER FUNCTION [document].[GetPersonPremium]
(
-- Add the parameters for the function here
#DocumentId bigint,
#PersonId bigint,
#PeriodId int,
#PersonRole nvarchar(20),
#PremiumTableType PremiumTableType readonly
)
RETURNS DECIMAL (18,2)
AS
BEGIN
-- Declare the return variable here
DECLARE #premiumSum decimal (18,2)
-- Add the T-SQL statements to compute the return value here
set #premiumSum = (select p.Premium from #PremiumType p where p.PersonId = #PersonId and p.PeriodId = #PeriodId and p.PersonRole = #PersonRole)
-- Return the result of the function
RETURN #premiumSum
END
In SP declared table type variable
Declare #PremiumTableType PremiumTableType
Inserted data from my temp table
Insert into #PremiumTableType (PersonID, PeriodId, ConcernRole, PersonPremium)
Select p.PersonID, ...
And called function from SP like
document.GetPersonPremium(#DocumentID, p.PersonID, pt.PeriodID, 'Intern', #PremiumTableType)

T_SQL Function with Column Parameters

I built a sql function which takes some values from my table
Such as DS_Consultor, No_Periodo and No_HORAS_REG to use on a function that calculates the total hours per month of every single worker and then calculates the work percentage of every single entry with this function:
ALTER FUNCTION [dbo].[CAL]
(
#EMP NVARCHAR(70),
#PER NVARCHAR(50),
#HORAS DECIMAL(18,1)
)
RETURNS #RES TABLE
(
RES DECIMAL(18, 1)
)
AS
BEGIN
DECLARE #TEMPTABLE TABLE
(
DS_CONSULTOR NVARCHAR(70),
NO_PERIODO NVARCHAR(50),
RECUENTO DECIMAL(18,1)
)
INSERT #tempTable
SELECT OC.DS_CONSULTOR, OC.NO_PERIODO, SUM(OC.NO_HORAS_REG) AS RECUENTO
FROM Occupation OC
GROUP BY OC.DS_CONSULTOR, OC.NO_PERIODO
INSERT #RES
SELECT #HORAS/T1.RECUENTO AS RES
FROM #tempTable T1
WHERE T1.DS_CONSULTOR=#EMP AND T1.NO_PERIODO=#HORAS
RETURN
END
However I can't add the column paramters that i need for a calculated column
SELECT cal.RES
FROM Occupation OC
CROSS APPLY CAL(DS_CONSULTOR, NO_PERIODO, NO_HORAS_REG)--This outputs 'Error
converting data type nvarchar to numeric.'
Is there something worng with the parameters or is it the function inpu/output?

How do I create a function to accept [[customerID]] and return CustName Please look at details

I have been asked to created a Function to accept CustomerID and return CustomerName for the CustomerID, I m a new Student/Developer Please if the question is not clear let me know so i can add more details about it, but that is what I was exactly asked.
functions in SQL are of three types.ignoring rest CLR functions ...
create table test
(
id int,
name varchar(4)
)
insert into test
select 1,'abc'
union all
select 2,'cde'
1.Scalar function takes one value and return one value
now for the above table ,you can create scalar function like below
create function dbo.test
(
#id int
)
returns varchar(4)
as
begin
declare #name varchar(4)
select #name=name from test where id =#id
return #name
End
You invoke it like:
select dbo.test(1)
2.Inline table valued functions:takes a single input same like scalar functions and returns table
create function dbo.test
(
#id int
)
as
returns TABLE
(
select * from test where id=#id)
You invoke it like:
select * from dbo.test(1)
3.Multi table valued function:
create function dbo.test
(
#id int
)
returns
#test table
(
id int,
name varchar(4)
)
as
begin
insert into #test
select * from test where id =#id
return
end
You invoke it like:
select * from dbo.test(1)
Take any one of Itzik Ben Gan books and start learning SQL the way it should be learned

plpgsql how to just execute query in a function or procedure

I am beginning to learn stored procedures and functions in sql in a postgres database.
I need an example to get me going for what I am trying to accomplish.
I need to run a procedure and have it return results. For example something like this:
run_query(name):
begin
return select * from employees where first_name = $name
end
end
I want something like the above to return the result set when I run it. Is this possible? thank you for your help in advance!
Here is the function im trying to create:
CREATE OR REPLACE FUNCTION test() RETURNS TABLE(id INT, subdomain varchar, launched_on_xxx timestamp, UVs bigint, PVs bigint) AS
'SELECT dblink_connect(''other_DB'');
SELECT c.id as id, c.subdomain, c.launched_on_xxx, COALESCE(SUM(tbd.new_unique_visitors), 0) AS UVs, COALESCE(SUM(tbd.page_views), 0) AS PVs
FROM dblink(''SELECT id, subdomain, launched_on_xxx FROM communities'')
AS c(id int, subdomain character varying, launched_on_xxx timestamp)
LEFT OUTER JOIN days_of_center tbd
ON c.id = tbd.community_id
WHERE c.launched_on_xxx < now()
GROUP BY c.id, c.subdomain, c.launched_on_xxx;
SELECT dblink_disconnect();'
LANGUAGE SQL;
Your function could look like this:
CREATE OR REPLACE FUNCTION test()
RETURNS TABLE(id int, subdomain varchar, launched_on_xxx timestamp
,uvs bigint, pvs bigint) AS
$func$
SELECT dblink_connect('other_DB');
SELECT c.id
,c.subdomain
,c.launched_on_xxx
,COALESCE(SUM(tbd.new_unique_visitors), 0) AS uvs
,COALESCE(SUM(tbd.page_views), 0) AS pvs
FROM dblink('
SELECT id, subdomain, launched_on_xxx
FROM communities
WHERE launched_on_xxx < now()')
AS c(id int, subdomain varchar, launched_on_xxx timestamp)
LEFT JOIN days_of_center tbd ON tbd.community_id = c.id
GROUP BY c.id, c.subdomain, c.launched_on_xxx;
SELECT dblink_disconnect();
$func$ LANGUAGE SQL;
Pull the WHERE clause down into the dblink function. It's much more effective not to fetch rows to begin with - instead of fetching them from the external database and then discarding them.
Use dollar-quoting to avoid confusion with quoting. That has become standard procedure with bigger function definitions.
To output it in "table format", call a function returning multiple columns like this:
SELECT * FROM test();
Just about the simplest possible example would be this;
CREATE FUNCTION test() RETURNS TABLE(num INT) AS
'SELECT id FROM table1'
LANGUAGE SQL;
SELECT * FROM test()
An SQLfiddle to test with.
If you need a parameter, here's another example;
CREATE FUNCTION test(sel INT) RETURNS TABLE(val VARCHAR) AS
'SELECT value FROM table1 WHERE id=sel'
LANGUAGE SQL;
SELECT * FROM test(2)
Another SQLfiddle to test with.

SQL UDF Group By Parameter Issue

I'm having some issues with a group by clause in SQL. I have the following basic function:
CREATE FUNCTION dbo.fn_GetWinsYear (#Year int)
RETURNS int
AS
BEGIN
declare #W int
select #W = count(1)
from tblGames
where WinLossForfeit = 'W' and datepart(yyyy,Date) = #Year
return #W
END
I'm trying to run the following basic query:
select dbo.fn_GetWinsYear(datepart(yyyy,date))
from tblGames
group by datepart(yyyy,date)
However, I'm encountering the following error message: Column 'tblGames.Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Any ideas why this is occurring? FYI, I know I can remove the function and combine into one call but I'd like to keep the function in place if possible.
I think you should be calling your function like this.
select dbo.fn_GetWinsYear(datepart(yyyy,getdate()))
OR
select dbo.fn_GetWinsYear('2010')
Essentially you are just passing a year to your function and the function is returning the number of wins for that year.
If you don't know the year, your function could look something like this...
CREATE FUNCTION dbo.fn_GetWinsYear ()
RETURNS #tblResults TABLE
( W INT, Y INT )
AS
BEGIN
INSERT #tblResults
SELECT count(1), datepart(yyyy,[Date])
FROM tblGames
WHERE WinLossForfeit = 'W'
GROUP BY datepart(yyyy,[Date])
RETURN
END
SELECT * FROM dbo.fn_GetWinsYear()