I have a scalar valued function as below:
CREATE FUNCTION [dbo].[GetCustomersTime]
(
#TimeSpanType char(1),
#TimeSpan int,
#CurrentUtc datetime
)
RETURNS datetime
AS
BEGIN
DECLARE #CustomersTime DATETIME;
SELECT #CustomersTime = CASE UPPER(#TimeSpanType)
WHEN 'S' THEN DATEADD(Second, #TimeSpan, #CurrentUtc)
WHEN 'I' THEN DATEADD(Minute, #TimeSpan, #CurrentUtc)
WHEN 'H' THEN DATEADD(Hour, #TimeSpan, #CurrentUtc)
WHEN 'D' THEN DATEADD(Day, #TimeSpan, #CurrentUtc)
WHEN 'W' THEN DATEADD(Week, #TimeSpan, #CurrentUtc)
WHEN 'M' THEN DATEADD(Month, #TimeSpan, #CurrentUtc)
WHEN 'Q' THEN DATEADD(Quarter, #TimeSpan, #CurrentUtc)
WHEN 'Y' THEN DATEADD(Year, #TimeSpan, #CurrentUtc)
ELSE DATEADD(SECOND, #TimeSpan, #CurrentUtc)
END;
RETURN #CustomersTime
END
I would like to use this function in the Entity Framework Query. The query I would like to achieve is below:
SELECT c.CustomerId, c.FirstName, c.LastName, c.OrderDateUtc
FROM dbo.Customer c
--Other INNER JOINs
WHERE dbo.GetCustomersTime(c.[TimeSpanType], c.[TimeSpan], c.OrderDateUtc) > GetUtcDate()
--Additional Conditions
My entity framework query is:
(from c in _context.Customers
//additional joins
where //how to use function
select new
{
c.CustomerId,
c.FirstName,
c.LastName,
c.OrderDateUtc
}).ToList();
Some suggested to use a project and have the GetCustomersTime function in the EF query. But I could not find a real example. Can anyone help with this?
Try the following:
Declare a function in the (SSDL) of your .edmx file. The name of the
function must be the same as the name of the function declared in the
database.
Add a corresponding method to a class in your application code and
apply a EdmFunctionAttribute to the method.
Call the method in a LINQ to Entities query.
For more details check this link.
Looks like I should have searched it better. I found the solution suggested here. The github link is CodeFirstFunctions.
Related
I am writing below Stored Procedure in BigQuery but not sure where did I do wrong.
The 'Out' Parameter does not return anything.
Stored Procedure:
create procedure if not exists test.GetNumTherapistSessions(
in LastNamee string,
out NumSessione int64
)
begin
select count(s.SessionNum) as NumSession from test.Session s
inner join test.Therapist t on s.TherapistID=t.TherapistID
where t.LastName=LastNamee;
end
and Here is how I declare the output parameter and how to call it:
declare NumSession int64;
call test.GetNumTherapistSessions("Risk",NumSession);
Here is the output:
So far everything seems right, but when I select the NumSession, it returns Null:
select NumSession;
Output:
Try SET NumSessione = (select count...);
create procedure if not exists test.GetNumTherapistSessions(
in LastNamee string,
out NumSessione int64
)
begin
SET NumSessione = (
select count(s.SessionNum) as NumSession from test.Session s
inner join test.Therapist t on s.TherapistID=t.TherapistID
where t.LastName=LastNamee
);
end
As shown in this docs of bigquery Link you can use SET to assign any values you need to a specific Variables.
Note that bigquery don't have SELECT INTO statement inside procedure.
Examples:
SET (value1, value2, value3) = (SELECT AS STRUCT c1, c2, c3 FROM table_name WHERE condition)
From this answer:select-into-with-bigquery
Another Example:
UPDATE dataset.Inventory
SET quantity = quantity +
(SELECT quantity FROM dataset.NewArrivals
WHERE Inventory.product = NewArrivals.product),
supply_constrained = false
WHERE product IN (SELECT product FROM dataset.NewArrivals)
Another examples can be found here: Link
I have created one macro in Teradata, now wish to call that macro through Stored Procedure (in Teradata only). The SQL part belongs to macro.
I wrote this procedure in case if execution of macro is not possible through procedure.
Kindly suggest both the option.
CREATE PROCEDURE MDM_STAGE.match_sqls_proc
(
in fname VARCHAR(30),
in match_frst_name VARCHAR (100),
in lname VARCHAR(30),
in match_last_name VARCHAR (100),
in addr1 VARCHAR(1000),
in zip_cd_base varchar(10),
in email VARCHAR(30),
in phone VARCHAR(30),
out msgs VARCHAR(10)
-- INOUT errstr VARCHAR(30)
)
begin
DECLARE SQLTEXT2 VARCHAR (10) ;
DECLARE TBL_COUNT INT ;
select count (*) into :TBL_COUNT from
(
SELECT
CUST.CUST_ID,
CUST.FRST_NAME,
CUST.MATCH_FRST_NAME,
CUST.LAST_NAME,
CUST.MATCH_LAST_NAME,
CUST.GNDR_TYPE_CD,
STREET_ADDR.ADDR_LN_1_TXT,
STREET_ADDR.ADDR_LN_2_TXT,
STREET_ADDR.ADDR_LN_3_TXT,
cast (coalesce (STREET_ADDR.ADDR_LN_1_TXT,'') || ' ' || coalesce
(STREET_ADDR.ADDR_LN_2_TXT,'' ) as varchar (1000)) as addr,
STREET_ADDR.CITY_NAME,
STREET_ADDR.POSTL_CD,
STREET_ADDR.STREET_ADDR_ID,
STREET_ADDR.ADDR_SBTYPE_CD,
ELCTRNC_ADDR.ELCTRNC_ADDR_ID,
ELCTRNC_ADDR.ELCTRNC_ADDR_SBTYPE_CD,
ELCTRNC_ADDR.ELCTRNC_ADDR_TXT,
TLPHN_NUM.TLPHN_LN_NUM,
TLPHN_NUM.TLPHN_NUM_ID
FROM
MDM_STAGE.REF_CUST_V CUST
LEFT OUTER JOIN
MDM_STAGE.REF_STREET_ADDR_V STREET_ADDR
ON
CUST.CUST_ID = STREET_ADDR.CUST_ID
AND
--RDM_ADDRSIM (STREET_ADDR.ADDR_LN_1_TXT ,'2285Main Street' ) =1
RDM_ADDRSIM ( cast (coalesce (STREET_ADDR.ADDR_LN_1_TXT,'') || ' ' ||
coalesce (STREET_ADDR.ADDR_LN_2_TXT,'' ) as varchar (1000)) ,:addr1) =1
AND
SUBSTR(STREET_ADDR.POSTL_CD,0,6) = (:zip_cd_base)
LEFT OUTER JOIN
MDM_STAGE.REF_ELCTRNC_ADDR_V ELCTRNC_ADDR
ON
CUST.CUST_ID = ELCTRNC_ADDR.CUST_ID
/*AND
STREET_ADDR.ADDR_SBTYPE_CD = ELCTRNC_ADDR.ELCTRNC_ADDR_SBTYPE_CD*/
AND
ELCTRNC_ADDR.ELCTRNC_ADDR_TXT= (:email)
LEFT OUTER JOIN
MDM_STAGE.REF_TLPHN_NUM_V TLPHN_NUM
ON
CUST.CUST_ID = TLPHN_NUM.CUST_ID
/*AND
STREET_ADDR.ADDR_SBTYPE_CD = TLPHN_NUM.ADDR_SBTYPE_CD*/
AND
TLPHN_NUM.TLPHN_LN_NUM = (:phone)
WHERE
(RDM_sndx(COALESCE(CUST.Match_FRST_NAME,CUST.CUST_ID))=RDM_sndx(:match_frst_name)
AND
RDM_sndx(COALESCE(STRTOK(CUST.Match_LAST_NAME,' ',1),CUST.CUST_ID))=RDM_SNDX(:match_last_name)
)
AND
(
TLPHN_NUM.CUST_ID IS NOT NULL
OR
ELCTRNC_ADDR.CUST_ID IS NOT NULL
OR
STREET_ADDR.CUST_ID IS NOT NULL
) A
;
IF ( TBL_COUNT > 0 ) THEN SET SQLTEXT2 = 'Match' ;
ELSE
SET Msgs = 'Non-Match' ;
END IF;
end;
Error message -
SPL1027:E(L88), Missing/Invalid SQL statement'E(3707):Syntax error, expected something like an 'EXCEPT' keyword or an 'UNION' keyword or a 'MINUS' keyword between ')' and the word 'A'.'.
No idea, what to add between the word 'A' and ')'. Tried all the possibility but not successful. Not sure how to pass the value of SQL into the 'count' as later call procedure condition is based on that count only.
I have a stored procedure for DB2, where I want to fill a field named SEASON with W for Winter and S for Summer. The procedure is done, but I can't handle the logic. Can someone show me how this works?
This is for a dimension table in a data warehouse.
INSERT into ABC.TIME_DIMEMSION (
DATE,
SEASON,
QUERTER)
VALUES(
DATE(myDate),
CASE
WHEN MONTH(myDate) = 1 THEN
SET SEASON= 'W';
WHEN MONTH(myDate) = 2 THEN
SET SEASON= 'W';
WHEN MONTH(myDate) = 2 THEN
SET SEASON= 'S';
ELSE
SET SEASON= 'X';
END CASE.
QUARTER(loaddate));
I'm getting an error
Look at the description of the CASE expression which you can use in other statements.
It differs from the Case Statement.
You must use a CASE expression in your case:
INSERT into ABC.TIME_DIMEMSION (
DATE,
SEASON,
QUERTER)
VALUES(
DATE(myDate),
CASE MONTH(myDate)
WHEN 1 THEN 'X'
WHEN 2 THEN 'Y'
ELSE 'Z'
END,
QUARTER(loaddate)
);
-- or
INSERT into ABC.TIME_DIMEMSION (
DATE,
SEASON,
QUERTER)
VALUES(
DATE(current timestamp),
CASE
WHEN MONTH(current timestamp)=1 THEN 'X'
WHEN MONTH(current timestamp)=2 THEN 'Y'
ELSE 'Z'
END,
QUARTER(current timestamp)
);
One way to do it (there are other ways) in a stored procedure:
Note: do not use reserved words for table column-names!
declare v_season varchar(10);
declare v_themonth integer;
set v_themonth = month(somedate);
set v_season = case when v_themonth in (12,1,2) then 'Winter'
when v_themonth in (3,4,5) then 'Spring'
when v_themonth in (6,7,8) then 'Summer'
when v_themonth in (9,10,11) then 'Autumn'
end;
insert into abc.time_dimension( thedate, season)
values ( somedate, v_theseason);
Firstname emp_Fullname
--------------------------------------
chetan Patel, Chetan
mike Shah, Mike
ronie Desai, Ronie
create proc stored_procedure
#firstnamer varchar(max)
#fullname varchar(max)
as
begin
select ......
from....
where Firstname in (SELECT Value FROM dbo.FnSplit(#firstname,','))
--and emp_Fullname in (SELECT Value FROM dbo.FnSplit(#fullname,','))
I want result for below statement
exec stored_procedure 'chetan,ronie', 'Patel, Chetan,Shah, Mike'
How can I pass more than 2 emp_fullname in parameter in given stored procedure? Below is my function dbo.FnSplit that worked for multi value Firstname parameter but not working multi value fullname parameter.
ALTER FUNCTION [dbo].[FnSplit]
(
#List nvarchar(2000),
#SplitOn nvarchar(5)
)
RETURNS #RtnValue table (Id int identity(1,1), Value nvarchar(100))
AS
BEGIN
WHILE(Charindex(#SplitOn, #List) > 0)
BEGIN
INSERT INTO #RtnValue (value)
SELECT
VALUE = ltrim(rtrim(Substring(#List, 1, Charindex(#SplitOn, #List) - 1)))
SET #List = SUBSTRING(#List, Charindex(#SplitOn, #List) + len(#SplitOn), len(#List))
END
INSERT INTO #RtnValue (Value)
SELECT
VALUE = ltrim(rtrim(#List))
RETURN
END
Firstname in (SELECT Value FROM dbo.FnSplit(#firstname,'|'))
and emp_Fullname in (SELECT Value FROM dbo.FnSplit(#fullname,'|'))
and I figured that still in SSRS double click on dataset click parmater instead of default value choose expression and set it to "join(#firstname.value,"|")" and samething for other "join(#fullname.value,"|")" and now run it. Multi valye parameter should work find by doing above procedure.
Thanks to my self lol:) it took me 3 days to figured, thought you guys can use it!
I'm trying to do this subquery:
var query =
from cjto in oContext.t_table_1
join cav in oContext.t_table_2 on cjto.cd_code equals cav.cd_code
where cav.dt_time >=
(from tu in oContext.t_table3
where tu.vl_code == "ABCD"
select tu.dt_check_time)
select cav;
However, I get the error:
Operator '>=' cannot be applied to operands of type 'System.DateTime' and 'System.Linq.IQueryable<System.DateTime?>'
How can I implement such query?
Tks
Ok, I got it... I needed to add the FirstOrDefault() so get the first element
var query =
from cjto in oContext.t_table_1
join cav in oContext.t_table_2 on cjto.cd_code equals cav.cd_code
where cav.dt_time >=
(from tu in oContext.t_table3
where tu.vl_code == "ABCD"
select tu.dt_check_time).FirstOrDefault()
select cav;
Tks