Unable to find User-defined Function - snowflake-cloud-data-platform

I have a UDF defined as follows:
Create Or Replace Function Minder_Mvp.Sprint4.Get_Customer_Key(Store_Key VARCHAR)
Returns Table (Customer_Key BIGINT)
AS
'Select Customer_Key From CustomerBrand
Where CustomerBrand.Brand_Key = 1'
;
And then I attempt to call it like this:
Select Minder_Mvp.Sprint4.Get_Customer_Key('457');
When I do so, I get the following error:
Unknown user-define function Minder_MVP.SPRINT4.GET_CUSTOMER_KEY
Must be something obvious but I am not seeing it.

It is an user defined table function:
A UDTF can be accessed in the FROM clause of a query.
Select Minder_Mvp.Sprint4.Get_Customer_Key('457');
=>
SELECT *
FROM TABLE(Minder_Mvp.Sprint4.Get_Customer_Key('457')) s;

Related

Schema as a parameter in a function

Is it possible to have a schema as a parameter in a function such as
CREATE FUNCTION get_data_for_weekly_front_end_collections_summary(#schema VARCHAR)
RETURNS TABLE
AS RETURN
(
SELECT *
FROM #schema.db1
)
The error:
TSQL doesn't support parametrizing object names in queries, and user-defined functions don't support dynamic SQL. But you can do something like this:
CREATE FUNCTION get_data_for_weekly_front_end_collections_summary(#schema NVARCHAR(200))
RETURNS TABLE
AS RETURN
(
select *
from a.db1
where #schema = 'a'
union all
select *
from b.db1
where #schema = 'b'
)

Calling User-Defined Function in select statement returning xml data

I created a user-defined function in SQL Server 2012 that returns XML. I would like to call the function in a SELECT statement. Is this possible?
When I try doing it, I get the error:
The FOR XML clause is not allowed in a ASSIGNMENT statement.
I want the SELECT statement to return a set of these named methods that have dependencies of other named methods within their logic.
In the main CTE, I get the latest versions of methods that have dependencies. The UDF goes thru the logic of each method and returns any methods called within it. So, I want to call the UDF in the SELECT statement and return XML of the dependent method names.
The function works and returns XML data. This is the function:
ALTER FUNCTION [dbo].[GetCalledMLMs]
(
-- Add the parameters for the function here
#MLM_Txt nvarchar(MAX)
)
RETURNS XML
AS
BEGIN
-- Declare the return variable here
DECLARE #CalledMLMs XML
Declare #MLMTbl table (pos int, endpos int, CalledMLM nvarchar(200))
--Logic to get the data...
Select #CalledMLMs = CalledMLM from #MLMTbl FOR XML PATH
-- Return the result of the function
RETURN #CalledMLMs
END
This is the CTE that calls the UDF.
;with cte as
(
select distinct Name, max(ID) as LatestVersion
from MLM_T
where Logic like '%:= MLM %' and Logic not like '%standard_libs := mlm%'
group by Name
)
select MLM2.Name, LatestVersion,
dbo.GetCalledMLMs(MLM2.Logic) as CalledMLMs
from cte join MLM_T MLM2 on cte.Name = MLM2.Name
and cte.LatestVersion = MLM2.ID
and MLM2.Active = 1 and MLM2.Status in (3, 4)
When running this query I get the error that XML is not allowed to be used in assignment statement.
Is there any way to call a function in the SELECT statment that returns an XML data type?
If you want to set a variable to a value you have to use SET and a scalar value on the right side.
The syntax SELECT #SomeVariable=SomeColumn FROM SomeTable is not possible with FOR XML (and rather dangerous anyway...), because the XML is not a column of the SELECT but something after the process of selecting.
Your problem is situated here:
Select #CalledMLMs = CalledMLM from #MLMTbl FOR XML PATH
Try to change this to
SET #CalledMLMs = (SELECT CalledMLM FROM #MLMTbl FRO XML PATH);
I solved the problem by changing the function to return a table, not XML.
So it looks like this:
FUNCTION [dbo].[GetCalledMLMsTbl]
(
-- Add the parameters for the function here
#MLM_Txt nvarchar(MAX)
)
--RETURNS XML
RETURNS #MLMTbl TABLE
(
pos int,
endpos int,
CalledMLM nvarchar(200)
)
AS
BEGIN
--logic here
insert into #MLMTbl (pos, endpos, CalledMLM) Values (#startpos, #endpos, #MLM_name)
RETURN
END
Then I called the function in the 'from' clause in the select
;with cte as
(
select distinct Name, max(ID) as LatestVersion
from CV3MLM
where Logic like '%:= MLM %' and Logic not like '%standard_libs := mlm%'
--and Name not like '%V61_CCC'
group by Name
)
select MLM2.Name, LatestVersion, C.CalledMLM
from cte join MLM_tbl MLM2 on cte.Name = MLM2.Name and cte.LatestVersion = MLM2.ID
and MLM2.Active = 1 and MLM2.Status in (3, 4)
cross apply dbo.GetCalledMLMsTbl(MLM2.Logic) C
order by MLM2.Name, LatestVersion

Convert scalar function to table function

I have a scalar function that returns a value from a select statement if it returns a value for the appropriate variables. If there is no return value, the scalar function returns one of the input parameters as the result.
How can I solve this in a table function?
Can I may be use this:
WITH Param
AS (
SELECT ID
,Data
FROM Configuration
WHERE NAME = 'NameOfConfiguration'
UNION ALL
SELECT NULL
,'Default Value'
)
SELECT TOP 1 Data
FROM Param
ORDER BY ID DESC
instead of this:
IF (LEN(ISNULL(#returnvalue, '')) = 0)
BEGIN
RETURN #thisvalue
END
RETURN #returnvalue
Table valued function and scalar valued functions are entirely different.
https://msdn.microsoft.com/en-us/library/ms191320.aspx?f=255&MSPPError=-2147217396
This link would give you how to create table valued and scalar functions.
Please feel to revert back if you have need more clarification :)

How do sql can understand or convert a nvarchar is name of a table in sql server

I Waint run code in SQL SERVER
ALTER FUNCTION return_table (#table nvarchar(250))
RETURNS TABLE
AS
RETURN
(
SELECT * FROM #table
)
none using PROCEDURE . THANK FOR HELP
The only way to make this work is truly horrible for both aesthetic and (probably) performance reasons. It also assumes that all tables that might be passed as parameters have the same structure:
ALTER FUNCTION return_table (#table sysname)
RETURNS TABLE
AS
RETURN
(
SELECT * FROM TableA where #table = 'TableA'
UNION ALL
SELECT * FROM TableB where #table = 'TableB'
UNION ALL
SELECT * FROM TableC where #table = 'TableC'
/* add more UNIONs and SELECTs for each table that you want to use */
)
A function requires an explicit definition of the return value type, e.g. the columns being returned. The SQL statement you provided will not work in such a manner for the following reasons:
ALTER FUNCTION return_table (#table nvarchar(250)) -- You have declared a parameter of type table, this is not the right way of doing this
RETURNS TABLE -- you haven't defined the structure of the table being returned, this needs explicitly defining
AS
RETURN
(
SELECT * FROM #table -- using SELECT * is bad practice as the structure of the table may change and then conflict with the structure of the table being returned
)
The first part of the problem is declaring a parameter of type TABLE; this question has good examples on how to get around doing this. Quick summary: you need to declare the table as a type before passing in the type as the parameter to your function:
CREATE TYPE MyTableParameterDefinition AS TABLE (
[ColumnName] NVARCHAR(250) NULL
)
This type can then be passed as a parameter to your function:
CREATE FUNCTION myFunctionName (
#TableVariable MyTableParameterDefinition READONLY
)...--INSERT CODE HERE--
I'm uncertain whether you can use a similar method for the return type and would suggest against this given the implication of the contract defined by the function. Better practice would be to define the structure of the table being returned and explicitly select the columns from the table:
ALTER FUNCTION return_table (
#table MyTableParameterDefinition
)
RETURNS TABLE
(
-- Explicitly define columns returned by the function
[ColumnName] NVARCHAR(250) NULL
)
AS
RETURN
(
SELECT
[ColumnName]
FROM
#table
)

Use aggregate functions in create function

I use below code to create function but when i want to execute it occur error
CREATE FUNCTION getFactorPriceFunction
(
#factorCode BIGINT
)
RETURNS bigint
AS BEGIN
RETURN
(
select SUM(price*coun) as total from CustomerFactor inner join CustomerFactorDetails
on CustomerFactor.code=CustomerFactorDetails.factorCode inner join ProductDetails on
ProductDetails.code=CustomerFactorDetails.productDetailsCode
where factorCode=#factorCode AND final=1
)
END
execute:
select total from getFactorPriceFunction(100)
error:
Invalid object name 'getFactorPriceFunction'
It's called dbo.getFactorPriceFunction.
The invocation is goes like this:
select dbo.getFactorPriceFunction(100) as total
Since it is a scalar value function ,it should be executed like
select dbo.getFactorPriceFunction(100)
If you are returning a table then it should be
select total from dbo.getFactorPriceFunction(100)

Resources