Dynamic SQL (where) in Firebird stored procedure - sql

I Have an SP that receive 2 parameters, P1 and P2, like this:
CREATE OR ALTER PROCEDURE MY_PROC (P1 varchar(10), P2 smallint = 1)
RETURNS (
code VARCHAR(10),
name VARCHAR(70),
state VARCHAR(2),
situation VARCHAR(20)
AS
...
...
And I need to generate the where clause based on the P2 parameter, like this:
if (P2=1) then
where (state='SP' and situation='stopped')
elseif (P2=2)
where (state='MG' and situation='moving')
How to use this type of if statement in where clause?

To me your question translates as a simple OR condition in the WHERE clause of a SQL query:
WHERE
(:P2 = 1 AND state='SP' and situation='stopped')
OR (:P2 = 2 AND state='MG' and situation='moving')

The answer of GMB will work fine for most situations, but in more complex cases it may have less desirable performance. An alternative solution would be to build a query string dynamically and execute it with execute statement:
CREATE OR ALTER PROCEDURE MY_PROC (P1 varchar(10), P2 smallint = 1)
RETURNS (
code VARCHAR(10),
name VARCHAR(70),
state VARCHAR(2),
situation VARCHAR(20)
AS
declare query varchar(2048);
begin
query = 'select ......';
if (p2 = 1) then
query = query || ' where (state=''SP'' and situation=''stopped'')';
else if (p2 = 2) then
query = query || ' where (state=''MG'' and situation=''moving'')';
-- if you expect a single result
execute statement query into code, name, state, situation;
-- OR
-- for multiple results
for execute statement query into code, name, state, situation do
suspend;
end

Related

How to check only the first value of the row returned from stored procedure in a SQL query

If I have a stored procedure like this:
get_my_dep(empNum)
and it returns one row ex:
call get_my_dep(567);
dep_code dep_year dep_name
66 2017 HR
How can I check only the first value of the row (dep_code) in my query like this:
SELECT *
FROM rmcandidate a INNER JOIN task b
ON a.task_code = b.task_code
WHERE get_my_dep(emp_num) != 0 -- here I want to check only the dep_code
AND b.active_flag = 1
Presumably the stored procedure is defined as returning multiple values as in:
create procedure get_my_dep(emp_num int)
returning int as dep_code, int as dep_year, char(8) as dep_name;
In this case you could create a wrapper procedure that returns only one of the values and then use that in the WHERE clause. For example:
create procedure get_my_dep_code(emp_num int)
returning int as dep_code;
define dc, dy int;
define dn char(8);
execute procedure get_my_dep(emp_num) into dc, dy, dn;
return dc;
end procedure;
An alternative could be to define the procedure to return a row type. For example:
create row type dep_code_t(dep_code int, dep_year int, dep_name char(8));
create procedure get_my_dep(emp_num int)
returning dep_code_t;
define dc, dy int;
define dn char(8);
...
return row(dc, dy, dn)::dep_code_t;
end procedure;
It is then possible to directly reference an element of the returned row type in a WHERE clause as in:
WHERE get_my_dep(emp_num).dep_code != 0
You can try using a virtual table:
SELECT
*
FROM
rmcandidate AS a
INNER JOIN task AS b
ON
a.task_code = b.task_code
WHERE
b.active_flag = 1
AND 0 !=
(
SELECT
vt1.dep_code
FROM
TABLE (get_my_dep(emp_num)) AS vt1 (dep_code, dep_year, dep_name)
)
;
This was tested on Informix 12.10 .

Conditional WHERE-clause at stored procedures

I want to apply an additional WHERE-Parameter, if a specific value is given. We're currently working on SAP-Hana, but we may are able to adapt strict stored-procedure programming to this plattform. any help is very appreciated! So here's the Code:
PROCEDURE test (
IN id integer,
IN testVal VARCHAR(20) DEFAULT '*',
out ex_return DB-SCHEME
)
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER
--DEFAULT SCHEMA <default_schema_name>
READS SQL DATA AS
BEGIN
ex_return =
SELECT
L."ID",
LW."ID"
FROM DB1 L
INNER JOIN DB2 LW
ON L."id" = LW."id"
WHERE
L."id" = :id
AND LW."testVal" LIKE :testVal -- this one only, if :testVal != '*'
;
END
What have I tried yet? I tried to CONCAT the SELECT with a calculated WHERE (IF-Conditions) an then the EXECUTE-Command, but it seems like SAP HANA doesn't support that. Then I tried to match the requirements with CASE within the WHERE-Claus: WHERE ... CASE :wert <> '*' THEN ... END
AND (LW."testVal" LIKE :testVal OR :testVal = '*')
Why exactly can't you use the APPLY_FILTER function?
PROCEDURE test (
IN id integer,
IN testVal VARCHAR(20) DEFAULT '',
out ex_return DB-SCHEME
)
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER
--DEFAULT SCHEMA <default_schema_name>
READS SQL DATA AS
BEGIN
ex_return =
SELECT
L."ID",
LW."ID"
FROM DB1 L
INNER JOIN DB2 LW
ON L."id" = LW."id"
WHERE
L."id" = :id;
ex_filtered = APPLY_FILTER (:ex_return, :testVal);
END;
In the variable testVal you now can provide the whole condition, e.g.
call test (1, ' "testVal" like ''%ABC%'' ');
All that is covered in the documentation http://help.sap.com/saphelp_hanaplatform/helpdata/en/a0/9d584807f84477a64d7625ca45b089/content.htm?frameset=/en/a9/4461d71e8145a78d990deac4823858/frameset.htm&current_toc=/en/ed/4f384562ce4861b48e22a8be3171e5/plain.htm&node_id=73
Lars

Execute table valued function from row values

Given a table as below where fn contains the name of an existing table valued functions and param contains the param to be passed to the function
fn | param
----------------
'fn_one' | 1001
'fn_two' | 1001
'fn_one' | 1002
'fn_two' | 1002
Is there a way to get a resulting table like this by using set-based operations?
The resulting table would contain 0-* lines for each line from the first table.
param | resultval
---------------------------
1001 | 'fn_one_result_a'
1001 | 'fn_one_result_b'
1001 | 'fn_two_result_one'
1002 | 'fn_two_result_one'
I thought I could do something like (pseudo)
select t1.param, t2.resultval
from table1 t1
cross join exec sp_executesql('select * from '+t1.fn+'('+t1.param+')') t2
but that gives a syntax error at exec sp_executesql.
Currently we're using cursors to loop through the first table and insert into a second table with exec sp_executesql. While this does the job correctly, it is also the heaviest part of a frequently used stored procedure and I'm trying to optimize it. Changes to the data model would probably imply changes to most of the core of the application and that would cost more then just throwing hardware at sql server.
I believe that this should do what you need, using dynamic SQL to generate a single statement that can give you your results and then using that with EXEC to put them into your table. The FOR XML trick is a common one for concatenating VARCHAR values together from multiple rows. It has to be written with the AS [text()] for it to work.
--=========================================================
-- Set up
--=========================================================
CREATE TABLE dbo.TestTableFunctions (function_name VARCHAR(50) NOT NULL, parameter VARCHAR(20) NOT NULL)
INSERT INTO dbo.TestTableFunctions (function_name, parameter)
VALUES ('fn_one', '1001'), ('fn_two', '1001'), ('fn_one', '1002'), ('fn_two', '1002')
CREATE TABLE dbo.TestTableFunctionsResults (function_name VARCHAR(50) NOT NULL, parameter VARCHAR(20) NOT NULL, result VARCHAR(200) NOT NULL)
GO
CREATE FUNCTION dbo.fn_one
(
#parameter VARCHAR(20)
)
RETURNS TABLE
AS
RETURN
SELECT 'fn_one_' + #parameter AS result
GO
CREATE FUNCTION dbo.fn_two
(
#parameter VARCHAR(20)
)
RETURNS TABLE
AS
RETURN
SELECT 'fn_two_' + #parameter AS result
GO
--=========================================================
-- The important stuff
--=========================================================
DECLARE #sql VARCHAR(MAX)
SELECT #sql =
(
SELECT 'SELECT ''' + T1.function_name + ''', ''' + T1.parameter + ''', F.result FROM ' + T1.function_name + '(' + T1.parameter + ') F UNION ALL ' AS [text()]
FROM
TestTableFunctions T1
FOR XML PATH ('')
)
SELECT #sql = SUBSTRING(#sql, 1, LEN(#sql) - 10)
INSERT INTO dbo.TestTableFunctionsResults
EXEC(#sql)
SELECT * FROM dbo.TestTableFunctionsResults
--=========================================================
-- Clean up
--=========================================================
DROP TABLE dbo.TestTableFunctions
DROP TABLE dbo.TestTableFunctionsResults
DROP FUNCTION dbo.fn_one
DROP FUNCTION dbo.fn_two
GO
The first SELECT statement (ignoring the setup) builds a string which has the syntax to run all of the functions in your table, returning the results all UNIONed together. That makes it possible to run the string with EXEC, which means that you can then INSERT those results into your table.
A couple of quick notes though... First, the functions must all return identical result set structures - the same number of columns with the same data types (technically, they might be able to be different data types if SQL Server can always do implicit conversions on them, but it's really not worth the risk). Second, if someone were able to update your functions table they could use SQL injection to wreak havoc on your system. You'll need that to be tightly controlled and I wouldn't let users just enter in function names, etc.
You cannot access objects by referencing their names in a SQL statement. One method would be to use a case statement:
select t1.*,
(case when fn = 'fn_one' then dbo.fn_one(t1.param)
when fn = 'fn_two' then dbo.fn_two(t1.param)
end) as resultval
from table1 t1 ;
Interestingly, you could encapsulate the case as another function, and then do:
select t1.*, dbo.fn_generic(t1.fn, t1.param) as resultval
from table1 t1 ;
However, in SQL Server, you cannot use dynamic SQL in a user-defined function (defined in T-SQL), so you would still need to use case or similar logic.
Either of these methods is likely to be much faster than a cursor, because they do not require issuing multiple queries.

How to pass multiple values to single parameter in stored procedure

I'm using SSRS for reporting and executing a stored procedure to generate the data for my reports
DECLARE #return_value int
EXEC #return_value = [dbo].[MYREPORT]
#ComparePeriod = 'Daily',
#OverrideCompareDate = NULL,
#PortfolioId = '5,6',
#OverrideStartDate = NULL,
#NewPositionsOnly = NULL,
#SourceID = 13
SELECT 'Return Value' = #return_value
GO
In the above when I passed #PortfolioId = '5,6' it is giving me wrong inputs
I need all records for portfolio id 5 and 6 also is this correct way to send the multiple values ?
When I execute my reports only giving #PortfolioId = '5' it is giving me 120 records
and when I execute it by giving #PortfolioId = '6' it is giving me 70 records
So when I will give #PortfolioId = '5,6' it should have to give me only 190 records altogether, but it is giving me more no of records I don't understand where I exactly go wrong .
Could anyone help me?
thanks
all code is too huge to paste , i'm pasting relevant code please suggest clue.
CREATE PROCEDURE [dbo].[GENERATE_REPORT]
(
#ComparePeriod VARCHAR(10),
#OverrideCompareDate DATETIME,
#PortfolioId VARCHAR(50) = '2', --this must be multiple
#OverrideStartDate DATETIME = NULL,
#NewPositionsOnly BIT = 0,
#SourceID INT = NULL
) AS
BEGIN
SELECT
Position.Date,
Position.SecurityId,
Position.Level1Industry,
Position.MoodyFacilityRating,
Position.SPFacilityRating,
Position.CompositeFacilityRating,
Position.SecurityType,
Position.FacilityType,
Position.Position
FROM
Fireball_Reporting.dbo.Reporting_DailyNAV_Pricing POSITION WITH (NOLOCK, READUNCOMMITTED)
LEFT JOIN Fireball.dbo.AdditionalSecurityPrice ClosingPrice WITH (NOLOCK, READUNCOMMITTED) ON
ClosingPrice.SecurityID = Position.PricingSecurityID AND
ClosingPrice.Date = Position.Date AND
ClosingPrice.SecurityPriceSourceID = #SourceID AND
ClosingPrice.PortfolioID IN (
SELECT
PARAM
FROM
Fireball_Reporting.dbo.ParseMultiValuedParameter(#PortfolioId, ',') )
This can not be done easily. There's no way to make an NVARCHAR parameter take "more than one value". What I've done before is - as you do already - make the parameter value like a list with comma-separated values. Then, split this string up into its parts in the stored procedure.
Splitting up can be done using string functions. Add every part to a temporary table. Pseudo-code for this could be:
CREATE TABLE #TempTable (ID INT)
WHILE LEN(#PortfolioID) > 0
BEGIN
IF NOT <#PortfolioID contains Comma>
BEGIN
INSERT INTO #TempTable VALUES CAST(#PortfolioID as INT)
SET #PortfolioID = ''
END ELSE
BEGIN
INSERT INTO #Temptable VALUES CAST(<Part until next comma> AS INT)
SET #PortfolioID = <Everything after the next comma>
END
END
Then, change your condition to
WHERE PortfolioId IN (SELECT ID FROM #TempTable)
EDIT
You may be interested in the documentation for multi value parameters in SSRS, which states:
You can define a multivalue parameter for any report parameter that
you create. However, if you want to pass multiple parameter values
back to a data source by using the query, the following requirements
must be satisfied:
The data source must be SQL Server, Oracle, Analysis Services, SAP BI
NetWeaver, or Hyperion Essbase.
The data source cannot be a stored procedure. Reporting Services does
not support passing a multivalue parameter array to a stored
procedure.
The query must use an IN clause to specify the parameter.
This I found here.
I spent time finding a proper way. This may be useful for others.
Create a UDF and refer in the query -
http://www.geekzilla.co.uk/view5C09B52C-4600-4B66-9DD7-DCE840D64CBD.htm
USE THIS
I have had this exact issue for almost 2 weeks, extremely frustrating but I FINALLY found this site and it was a clear walk-through of what to do.
http://blog.summitcloud.com/2010/01/multivalue-parameters-with-stored-procedures-in-ssrs-sql/
I hope this helps people because it was exactly what I was looking for
Either use a User Defined Table
Or you can use CSV by defining your own CSV function as per This Post.
I'd probably recommend the second method, as your stored proc is already written in the correct format and you'll find it handy later on if you need to do this down the road.
Cheers!
I think, below procedure help you to what you are looking for.
CREATE PROCEDURE [dbo].[FindEmployeeRecord]
#EmployeeID nvarchar(Max)
AS
BEGIN
DECLARE #sqLQuery VARCHAR(MAX)
Declare #AnswersTempTable Table
(
EmpId int,
EmployeeName nvarchar (250),
EmployeeAddress nvarchar (250),
PostalCode nvarchar (50),
TelephoneNo nvarchar (50),
Email nvarchar (250),
status nvarchar (50),
Sex nvarchar (50)
)
Set #sqlQuery =
'select e.EmpId,e.EmployeeName,e.Email,e.Sex,ed.EmployeeAddress,ed.PostalCode,ed.TelephoneNo,ed.status
from Employee e
join EmployeeDetail ed on e.Empid = ed.iEmpID
where Convert(nvarchar(Max),e.EmpId) in ('+#EmployeeId+')
order by EmpId'
Insert into #AnswersTempTable
exec (#sqlQuery)
select * from #AnswersTempTable
END

parameter convert from sql server to oracle

This parameter in SQL Server query - I copy to the Oracle query I am writing but it does not compile:
Compilation errors for PROCEDURE OGEN.DBD_NOT_GET_NOTES_DETAIL
Error: PLS-00103: Encountered the symbol "(" when expecting one of the
following:
:= . ) , # % default character
The symbol ":=" was substituted for "(" to continue. Line: 6 Text: , NOTETYPE NUMERIC(1) = 1
How can I code this in Oracle?
The complete SQL Server T-SQL query:
ALTER PROCEDURE [OEN].[DB_NOT_GET_NOTES_DETAIL]
(
#FACILITYKEY CHAR(4),
#DATEFROM DATETIME,
#DATETHRU DATETIME,
#UNITSTR VARCHAR(250),
#NOTETYPE NUMERIC(1) = 1
)
AS
BEGIN
SELECT P.FACILITY_KEY, P.PAT_NUMBER, P.PATIENT_ID,
OEN.DATEONLY(N.CREATED_ON) CREATED_ON, N.NOTE_HEADER,
N.CREATED_BY, P.LAST_NAME, P.FIRST_NAME, P.MIDDLE_NAME, P.UNIT_CODE
FROM OEN.EN_M_PATIENT_MAST P INNER JOIN OPTC.NOT_M_MAST N
ON (P.PAT_NUMBER = N.PAT_NUMBER AND N.FACILITY_KEY = #FACILITYKEY)
WHERE N.NOTE_STATUS = 0
AND (OEN.DATEONLY(N.CREATED_ON) BETWEEN OEN.DATEONLY(#DATEFROM) AND OEN.DATEONLY(#DATETHRU))
AND (#UNITSTR IS NULL OR #UNITSTR = '' OR CHARINDEX(P.UNIT_CODE, #UNITSTR) % 2 = 1)
AND #NOTETYPE = 1
END
The Oracle version:
CREATE OR REPLACE PROCEDURE OEN.DBD_NOT_GET_NOTES_DETAIL (
FACILITYKEY varchar2
, DATEFROM DATE
, DATETHRU DATE
, UNITSTR varchar2
, NOTETYPE NUMERIC(1) = 1
, OCURSOR OUT SYS_REFCURSOR
) as
BEGIN
OPEN OCURSOR FOR
SELECT P.FACILITY_KEY,
P.PAT_NUMBER,
P.PATIENT_ID,
OEN.DATEONLY(N.CREATED_ON) CREATED_ON, N.NOTE_HEADER,
N.CREATED_BY, P.LAST_NAME, P.FIRST_NAME, P.MIDDLE_NAME, P.UNIT_CODE
FROM OEN.EN_M_PATIENT_MAST P
INNER JOIN OPTC.NOT_M_MAST N ON (P.PAT_NUMBER = N.PAT_NUMBER AND N.FACILITY_KEY = FACILITYKEY)
WHERE N.NOTE_STATUS = 0
AND (OEN.DATEONLY(N.CREATED_ON) BETWEEN OEN.DATEONLY(DATEFROM) AND OEN.DATEONLY(DATETHRU))
AND CREATED_ON BETWEEN DATEFROM AND DATETHRU
AND (UNITSTR IS NULL OR P.UNIT_CODE = UNITSTR);
END;
Parameters to functions should not have length, scale, or precision. So the NOTETYPE parameter would need to be declared
NOTETYPE NUMERIC
If you want to assign a default value for a parameter, the syntax is
<<parameter declaration>> DEFAULT <<default value>>
Putting it together, your parameter declaration should be
, NOTETYPE NUMERIC DEFAULT 1
As a general stylistic matter, though this probably won't cause any errors
I would strongly suggest that parameters to procedures be anchored to an appropriate type in the data model. So, for example, FACILITYKEY OPTC.NOT_M_MAST.FACILITY_KEY%TYPE. That allows the parameter to adjust if in the future you need to do something like increase the length of a column.
I would strongly suggest adopting a naming convention for parameters that differentiates them from database columns. Prefixing the parameter names is a popular one (i.e. p_facility_key optc.not_m_mast.facility_key%type). Since you're not using the # prefix like you do in SQL Server, it is very easy to inadvertently have a parameter name that matches the name of a column in a table. Since name resolution gives preference to column names over local variables, that makes it very easy to write code that is inadvertently using a column rather than a local variable.
For example, this function will return every row in the EMP table.
CREATE OR REPLACE FUNCTION get_emps( empno IN emp.empno%type )
RETURN sys_refcursor
IS
l_rc sys_refcursor;
BEGIN
OPEN l_rc
FOR SELECT *
FROM emp e
WHERE e.empno = empno;
RETURN l_rc;
END;
Since the goal of this method is to return something to the caller, not to do a computation, it really ought to be declared as a FUNCTION rather than a PROCEDURE and ought to RETURN the sys_refcursor rather than having an OUT parameter.
My guess is you need
NOTETYPE NUMBER(1) := 1;