일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- tkinter Radio 동적버튼
- Django-allauth
- check_password
- graphene-django
- GraphQL
- array
- allauth
- python
- Django
- SQL
- FastAPI
- numpy
- flask
- for loop
- Today
- Total
객
[PostgreSQL] FUNCTION 예시 본문
FUNCTION 예제
참고 : PostgreSQL 7.2 programmer's Guide 중 12장
-----------------------------------------------------------------------------------------------
CREATE FUNCTION "TP1"(integer , numeric) RETURNS integer AS $$
UPDATE bank SET balence = balance - $2 WHERE accountno = $1;
SELECT 1;
$$ LANGUAGE SQL;
SELECT "TP1"(17 , 100.00);
※ FUNCTION 명은 "" 이 없을때는 소문자로 인식 됨 대문자로 인식을 시키려면
"" 사용 해야함.
------------------------------------------------------------------------------------------------
CREATE FUNCTION TP1(integer , numeric) RETURNS numeric AS $$
UPDATE bank SET blance = balance - $2 WHERE accountno = $1;
SELECT balance FROM bank WHERE accountno = $1;
$$ LANGUAGE SQL;
------------------------------------------------------------------------------------------------
CREATE FUNCTION clean_EMP() RETURNS integer AS $$
DELETE FROM EMP WHERE EMP.salary <= 0;
SELECT 1 AS ignore_this;
$$ LANGUAGE SQL;
------------------------------------------------------------------------------------------------
CREATE FUNCTION one() RETURNS integer AS $$
SELECT 1 as RETURNS;
$$ LANGUAGE SQL;
------------------------------------------------------------------------------------------------
CREATE FUNCTION listchildren(TEXT) RETURNS SETOF text AS $$
SELECT name FROM nodes WHERE parent = $1
$$ LANGUAGE SQL;
------------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION getAllFoo() RETURNS SETOF foo AS
DECLARE
r foo%rowtype;
BEGIN
FOR r IN SELECT * FROM foo
WHERE fooid > 0
LOOP
-- can do some processing here
RETURN NEXT r; -- return current row of SELECT
END LOOP;
RETURN;
END
$BODY$
LANGUAGE 'plpgsql' ;
SELECT * FROM getallfoo();
------------------------------------------------------------------------------------------------
create function editproc(uid varchar , pwdVal varchar , seqVal integer)
returns table(seq integer ,thread integer , writer varchar , email varchar , title varchar , mode smallint , readcount int , content text ) as $$
declare passcheck boolean;
begin
passcheck := check_password($1, $2, $3);
if passcheck = true then
return query select b.seq , b.thread , b.writer , b.email , b.title , b.mode , b.readcount , b.content from board as b where b.seq=$3;
else
return query(select '해당 레코드가 없습니다.');
end if;
end;
$$ language plpgsql
------------------------------------------------------------------------------------------------
-- 데이터가 필드에 존재 하는지 여부
CREATE FUNCTION dataSearch(code varchar(10)) RETURNS int4 AS $$
DECLARE
result integer = 0;
r_val integer =0;
BEGIN
select count(*) from tbStore into result where s_code = $1;
if result = 1 then
r_val = 1;
else
r_val = 0;
end if;
return r_val;
END;
$$ LANGUAGE plpgsql;
------------------------------------------------------------------------------------------------
-- 테이블에 저장 하기 위해 작성한 function
CREATE FUNCTION dateInsert(a varchar(50), b varchar(10), c varchar(14), d varchar(80) , e varchar(10) , OUT result integer ) RETURNS int4 AS $$
BEGIN
INSERT into tbStore(s_stname , s_code , s_tel , s_address , s_name)
VALUES(a ,b , c , d , e)
RETURNING s_code into result;
END;
$$ LANGUAGE plpgsql;
'[PL] > DB' 카테고리의 다른 글
[ cubrid ] 자바저장 함수 암호화 구현예제 (0) | 2014.01.03 |
---|---|
번호 자동 증가 (0) | 2013.11.21 |
[postgresql] substring 비교 (0) | 2013.08.04 |
[MSSQL]해당년도 동안 입력된 레코드의 갯수 알아보는 스크립트 (0) | 2011.11.16 |
[MSSQL]코드 자동증가값 스크립트 (0) | 2011.11.16 |