Content-Type and X-Content-Type-Options Content-Type and X-Content-Type-Options do $$beginfor cnt in 1..10 loopraise notice 'cnt: %', cnt;end loop;end; $$
The Do command will not return rows. we have to use NOTICES or RAISE other messages with language plpgsql .Result Comes on executing above Query NOTICE: cnt: 2NOTICE: cnt: 3NOTICE: cnt: 4NOTICE: cnt: 5NOTICE: cnt: 6NOTICE: cnt: 7NOTICE: cnt: 8NOTICE: cnt: 9NOTICE: cnt: 10DOThe following code uses the for loop statement to iterate over ten numbers from 10 to 1 and display each of them in each iteration:
Now We can use loop in Function also to get desired resultExample 2Create table TblPriorityGraph ( priorityID serial primary key ,priorityName varchar(8), Counttotal int, yearcurrent int, MinYear int, MaxYear int )
Below we will select record from other table TblTaskTracking
any column from table will be selected in ""
and table name will also be selected in "" CREATE OR REPLACE FUNCTION public.fn_setprioritycountforgraph()RETURNS TABLE(priorityid integer, priorityname text, counttotal integer, yearcurrent integer, minyear integer, maxyear integer)LANGUAGE 'plpgsql'AS $BODY$DECLARE L_Counter int:=( SELECT Extract(year from Min ("DateTaskAssigned")) AS maxyear FROM "TblTaskTracking");DECLARE L_CloseCounter int:=( SELECT Extract(year from MAX ("DateTaskAssigned")) AS maxyear FROM "TblTaskTracking");DECLARE L_MinYear int:=L_Counter;DECLARE L_MaxYear int:=L_CloseCounter;BEGINWHILE ( L_Counter <= L_CloseCounter) loopinsert into TblPriorityGraph (priorityID,priorityName,Counttotal,yearcurrent,MinYear,MaxYear)select "PriorityID","Priority",count(*),L_Counter,L_MinYear,L_MaxYearfrom "TblTaskTracking" where "PriorityID"=1and Extract( year from "DateTaskAssigned")=L_Counter and "IsActive"='Y'group by "PriorityID","Priority";L_Counter := L_Counter + 1 ;END loop ;return queryselect * from TblPriorityGraph;end$BODY$;
In the Above function you can see declaration of data and loop execution process.
So we can you loop to insert data to table and get result from table.
So in dynamic way to make a function we can make such function which
can use all query like insert ,loop,select,return,Declare etc.
Loop basically used to get record from table by year and insert it
to other table and later on we will get result from it
Thanks
SQL stands for Structured Query Language.
SQL is a standard language for accessing and manipulating databases and records
It helps to Manage Data in database according to Our Need.
Let see what we can do with SQL
1.Create a Database .
2.Create table inside database to store data.
3.Insert data using SQL Query.
4.Update data using SQL Query
5.Delete data using SQL Query
6.Retrive Record from Datable as per our need.
7.Set permission to Database and Table.
8.We can create Store Procedure to fetch record or manipulate record.
We have to use Some Command to Manipulate Database record.
Insert,Update,Delete,Select...........etc.
There are Some Syntax in SQL which is used to Manipulate record in database
which is ANSI standard Syntax and Command
.Like To Fetch all records From Table we will write
select * from TableName
To Delete Record From Table
Delete TableName where ID=X
This will delete record from table where ID is X
Order by in Sql is used to sort record in ascending or desending order
We will write query like
SELECT X, Y, ...
FROM mytable
ORDER BY x, y,Z ... ASC|DESC;
by default it gives record in ascending order but get record in ascending order we will use order by Desc
Example
| StudentID | StudentName | Age | Marks |
|---|---|---|---|
| 1 | Navneet | 11 | 82 |
| 2 | Nitesh | 16 | 90 |
Select * from Studenttable order by age desc
Result as below
| StudentID | StudentName | Age | Marks |
|---|---|---|---|
| 2 | Nitesh | 16 | 90 |
| 1 | Navneet | 11 | 82 |