I want inverted triangle using teradata sql. I have written below recursive query and getting the output. But i want to print inverted triangle using stored procedure .
with recursive atr_col(id, val)
AS
(
sel 1, cast('' as varchar(1000)) as val
from (sel 1 as dummy) a
union all
sel tb2.id+1, trim(tb2.val)||''
from atr_col tb2
where tb2.id<=val)
sel val from atr_col order by id desc;
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 a db2 stored procedure that takes in some parameters, gets some data from somewhere and then returns a result set through a cursor.
Now I want to write a table function in db2, that will call this stored procedure, read from the result set and return the data in the result set as a table (eventually I want to use this table function in a join).
I would like to know if this is permitted in db2 (we're using DB2 v10.5), i.e. execute a stored procedure in a table function and fetch and read from the result set from the stored procedure. If so, what is the right syntax for calling the stored procedure and reading the result set inside a table function in db2? Thanks!
Yes, it's possible. See the example below.
--#SET TERMINATOR #
CREATE OR REPLACE PROCEDURE TEST_PROC(P_TABSCHEMA VARCHAR(128))
DYNAMIC RESULT SETS 1
READS SQL DATA
BEGIN
DECLARE C1 CURSOR WITH HOLD WITH RETURN FOR
SELECT TABSCHEMA, TABNAME, COLCOUNT
FROM SYSCAT.TABLES
WHERE TABSCHEMA=P_TABSCHEMA;
OPEN C1;
END#
--CALL TEST_PROC('SYSCAT')#
CREATE OR REPLACE FUNCTION TEST_PROC(P_TABSCHEMA VARCHAR(128))
RETURNS TABLE (
TABSCHEMA VARCHAR(128)
, TABNAME VARCHAR(128)
, COLCOUNT INT
)
READS SQL DATA
BEGIN
DECLARE SQLSTATE CHAR(5);
DECLARE V_TABSCHEMA VARCHAR(128);
DECLARE V_TABNAME VARCHAR(128);
DECLARE V_COLCOUNT INT;
DECLARE V1 RESULT_SET_LOCATOR VARYING;
CALL TEST_PROC(P_TABSCHEMA);
ASSOCIATE RESULT SET LOCATOR (V1) WITH PROCEDURE TEST_PROC;
ALLOCATE C1 CURSOR FOR RESULT SET V1;
L1: LOOP
FETCH C1 INTO V_TABSCHEMA, V_TABNAME, V_COLCOUNT;
IF SQLSTATE<>'00000' THEN LEAVE L1; END IF;
PIPE(V_TABSCHEMA, V_TABNAME, V_COLCOUNT);
END LOOP L1;
CLOSE C1;
RETURN;
END#
SELECT * FROM TABLE(TEST_PROC('SYSCAT'))#
You need to create the DB2 table-function as follows:
CREATE FUNCTION database_schema.function_name ( IN_PARTID VARCHAR(1000) )
RETURNS TABLE ( PARTNO CHAR(25), PARTDS CHAR(30), QUANTITY INT )
BEGIN
RETURN SELECT PARTNO , PARTDS , CASE WHEN QUANTITY > 0 THEN QUANTITY ELSE 0 END QUANTITY
FROM
(
SELECT PARTNO
,MAX(PARTDS) AS PARTDS
,SUM(QUANTITY) AS QUANTITY
FROM database_schema.table_name
WHERE 1=1
AND PARTID = (CAST(IN_PARTID AS INT))
GROUP BY PARTNO
) AA;
END;
Then invoke the table-function as join or straight SQL:
SELECT partno,partds,quantity
FROM TABLE(database_schema.function_name('parameter_1'))
I have a stored procedure that have 2 result sets and I want to call this sp into another stored procedure and get both it’s result sets and work with them. I have searched in Internet and Knew about the new fearture in sql server 2012 for this purpose. It is ‘RESULT SETS’.
Except using ‘Result sets’, is there another solution for call storedprocedure with more than one resultset into another procedure and work with them?
Pinal Dave has an example for using ‘Result Sets’
USE AdventureWorks2008R2
GO
CREATE PROCEDURE mySP1 (#ShiftID INT, #JobCandidateID INT)
AS
-- ResultSet 1
SELECT [ShiftID],[Name],[StartTime],[EndTime],[ModifiedDate]
FROM [HumanResources].[Shift]
WHERE [ShiftID] = #ShiftID
-- ResultSet 2
SELECT [JobCandidateID],[BusinessEntityID],[ModifiedDate]
FROM [HumanResources].[JobCandidate]
WHERE JobCandidateID = #JobCandidateID
CREATE PROCEDURE mySP2
AS
EXEC mySP1 #ShiftID = 2, #JobCandidateID = 5
WITH RESULT SETS
( ( [ShiftID] TINYINT,[Name] NVARCHAR(50),[StartTime] DATETIME, [EndTime] DATETIME,[UpdateDate] DATETIME -- Notice Name Change
),([JobCandidateID] INT,[BusinessEntityID] INT,[ModifiedDate] DATETIME ));
When we use the Result Set, we have the mysp1 results into these:
For ResultSet1:
[ShiftID] TINYINT,[Name] NVARCHAR(50),[StartTime] DATETIME,
[EndTime] DATETIME,[UpdateDate] DATETIME
And for Resultset2:
[JobCandidateID] INT,[BusinessEntityID] INT,[ModifiedDate] DATETIME
But now I want to query on these results in mySp2. How can I do this.
How can I select the values from :
[ShiftID] TINYINT,[Name] NVARCHAR(50),[StartTime] DATETIME
,[EndTime] DATETIME,[UpdateDate] DATETIME
And
[JobCandidateID] INT,[BusinessEntityID] INT,[ModifiedDate] DATETIME
The best solution may be using output variables in mySP1
CREATE PROCEDURE mySP1 (
#ShiftID INT
, #JobCandidateID INT
, #CUR1 CURSOR VARYING OUTPUT
)
SET NOCOUNT ON;
SET #CUR1 = CURSOR
FORWARD_ONLY STATIC FOR
SELECT [ShiftID],[Name],[StartTime],[EndTime],[ModifiedDate]
FROM [HumanResources].[Shift]
WHERE [ShiftID] = #ShiftID
OPEN #CUR1;
GO
to calling this sp:
DECLARE #MyCursor CURSOR;
EXEC dbo.mySP1 #ShiftID = 1, #JobCandidateID = 1, #CUR1 = #MyCursor OUTPUT;
WHILE (##FETCH_STATUS = 0)
BEGIN;
FETCH NEXT FROM #MyCursor;
END;
CLOSE #MyCursor;
DEALLOCATE #MyCursor;
Use a combination of this with a standar output or just declare another cursor output.
I want to save results of a Sparql query :
SELECT ?items ?head
FROM <http://localhost:8890/diplomatiki#>
WHERE {
?exeiId <http://localhost:8890/schemas/diplomatiki/itemid> ?items;
<http://localhost:8890/schemas/diplomatiki/headid> ?head
}
into a table or as a linked Data locally (virtuoso conductor), How can Ι do it.
you could create your own procedure.
like:
create procedure
INSERT_SPARQL_RESULT
(
in query VARCHAR
)
{
DECLARE state, msg, descs, rows any;
exec('SPARQL ' || query, state, msg, vector (), 0, descs, rows);
declare i INTEGER;
for(i:=0;i<LENGTH(rows);i:=i+1){
INSERT INTO tablename VALUES (rows[i][0], rows[i][1]);
}
};
It is important to add 'SPARQL ' before your query.
After successfully creating your procedure:
CALL INSERT_SPARQL_RESULT('
SELECT ?items ?head
FROM <http://localhost:8890/diplomatiki#>
WHERE {
?exeiId <http://localhost:8890/schemas/diplomatiki/itemid> ?items;
<http://localhost:8890/schemas/diplomatiki/headid> ?head
}');
In web application, I am using LINQ to call a procedure, the procedure is parameter procedure. but when I am passing arguments it is giving errors, This is my code:
MyLinqsDataContext DataContext=new MyLinqsDataContext ();
int eno=Convert.ToInt32 (txtempno.Text );
int dep=Convert .ToInt32 (txtDep.Text );
var sqr = from qr in DataContext.USP_Insert_Emp(eno, txtName.Text, dep)
select qr;
But is giving error like:
Could not find an implementation of the query pattern for source type int. Select not found.
This is my Proc :
create procedure USP_Insert_Emp(#empid int,#ename varchar(60),#deptid int)
as
begin
insert into Emp (empid ,ename,deptid ) values (#empid ,#ename ,#deptid)
end
DataContext.USP_Insert_Emp returns an int.
The error you are getting is because you are trying to call Select on an int and not an IEnumerable<T>.