Can we have a cursor on mathematical functions - sql

I am trying to create a sql cursor on a sum of sum.
CURSOR DATA_CURSOR IS
SELECT SUM(A)+SUM(B) FROM DATA_TABLE
I guess that may not be the correct way to do it.... how do I create it and how to fetch it?

You need to name the new column you created in the query e.g.:
CURSOR DATA_CURSOR IS
SELECT SUM(A)+SUM(B) AS SUM_AB
FROM DATA_TABLE

Related

Redshift Cursor

I am trying to get maximum length of each column of a table in Redshift database with this query:
select MAX(LEN(Trim(col1)))mx_len from tbl1;
and insert result into a tmp table like:
tbl1 col1 50
For this, I am trying to write a cursor in redshift to fetch column name one by one and insert data into tmp table. I am getting column names from following query:
select columnname from information_schema.columns where table_name = 'tbl1'
but unable to write a cursor, can anyone help me with this. Thanks in advance.
You haven't describes how you are trying to declare a cursor so it is a little hard to know what the problem is. So I'll describe a generic path to making a cursor and fetching from it.
First a cursor only lives for the duration of the transaction so you must begin and keep open a transaction.
BEGIN;
Next you need to declare the cursor and what data it will contain.
DECLARE <cursor_name> CURSOR FOR SELECT X, Y, Z FROM ...
Then you need to fetch a row from the cursor.
FETCH FORWARD 1 FROM <cursor_name>;
Then get the next row.
FETCH NEXT FROM <cursor_name>;
And on and on. When done close the cursor and end the transaction.
CLOSE <cursor_name>;
END;
Is this what you are doing? Is this not working?

Execute stored procedure before select

I am trying to reset my random number seed before executing a SELECT FROM. However, every time I run it, I have to run both statements separately. Ideally, I would like to have the following work:
BEGIN
exec dbms_random.seed(6);
SELECT * FROM myTable
ORDER BY dbms_random.value()
END
I get an error Encountered the symbol DBMS_RANDOM when expecting one of the following :=.(#%; however if I only run exec dbms_random.seed(6); it works.
EXEC[UTE] is a SQLPLUS command, you can not use it into a PLSQL block.
Also, you need an INTO to use a SELECT query within a PLSQL block. If your query can give more than one row, you woud have to use a BULK COLLECT INTO.
Your code could be something like:
DECLARE
something myTable%rowtype;
BEGIN
dbms_random.seed(6);
SELECT *
INTO something
FROM myTable
ORDER BY dbms_random.value();
END;
However, I do not recommend using things like select *; it would be better to explicitly write the columns you need to get.
Also what's the use of an order by in a select into statement which is defined to only return 1 row?
It seems you want to select 1 random row of many which needs a cursor like so:
DECLARE
CURSOR something_cur IS
SELECT *
FROM myTable
ORDER BY dbms_random.value();
something_rec something_cur%ROWTYPE;
BEGIN
dbms_random.seed(6);
OPEN something_cur;
FETCH something_cur INTO something_rec;
CLOSE something_cur;
END;
/

Analyze_Compensation in oracle

Can anyone please tell me for what purpose analyze_compensation is used in oracle?
I got this term in a code in bulk collect. PFB the link:
http://www.oracle.com/partners/campaign/o28plsql-095155.html
Suppose you want to print all the rows and columns of table i.e(Select * from table_name;
) in PL/SQL
THEN in that case :-
dbms_output.put_line() wont be able to print the table data when there is more than one row... Hence we use analyze_compensation() .
It is a procedure , which you need to declare in DECLARE BLOCK of PL/SQL
analyze_compensation is used to print the whole table data i.e Select * from table_name;

Access data within a SQL Server cursor

Is it possible to access the temp data created within a SQL Server cursor? I would like to set some conditions within the cursor but those depends on the data that the cursor has example:
Cursor declaration.....
if (#Column = (SELECT MAX(ID) from cursor_name/table where Rid = #Rid))
begin
....
I'm actually doing this using a table variable that is being read by the cursor but I wonder if there's a better way to achieve this, any suggestions?
Thanks

How to select query result inside stored procedure

I'm writing a stored procedure in SQL Server 2008 which contains a SELECT DISTINCT statement and another simple Select statement which is based on the result of first statement.
How to use the table returned by the SELECT DISTINCT statement i.e the UnitNumber column value in second Select statement?
Stored procedure:
CREATE PROCEDURE ExtractPacket
AS
BEGIN
SET NOCOUNT ON;
-- Select statements to check the number of unit
SELECT DISTINCT UnitNumber from dbo.CP_TemplateHandler
END
GO
you can create a temp table and fill it by first SELECT DISTINCT and then in second Select use that.
Excuse me for answer in order comment(i can comment yet :( )
I suggest that use First Select Distinct as a sub query of second Select Distinct query.