database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
ㆍPostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
PostgreSQL Q&A 3001 게시물 읽기
No. 3001
Retrive comma separated PK attributes
작성자
정재익
작성일
2001-06-22 07:58
조회수
1,906

Submitted on: 03-23-2001

Description:

Retrieves a comma delimited list of attribute names making up the primary key of the relation passed as argument $1

Code:

 

CREATE FUNCTION get_pk (text) RETURNS text AS '

-----------------------------------------------------------------

-- Function: get_pk

-- Purpose: Retrieves a comma delimited

-- list of attribute names

-- making up the primary key

-- of the relation passed as argument $1

-- Licence: BSD

-- Author: Joseph Conway

-----------------------------------------------------------------

DECLARE

l_pk RECORD;

l_pk_list text;

l_cntr int4;

l_relname ALIAS FOR $1;

BEGIN

l_pk_list := '''';

l_cntr := 0;

FOR l_pk IN

SELECT pg_attribute.attname FROM pg_class, pg_attribute, pg_index WHERE pg_class.relname = l_relname AND pg_class.oid = pg_attribute.attrelid AND pg_class.oid = pg_index.indrelid AND pg_index.indkey[0] = pg_attribute.attnum AND pg_index.indisprimary = ''t''

union all

SELECT pg_attribute.attname FROM pg_class, pg_attribute, pg_index WHERE pg_class.relname = l_relname AND pg_class.oid = pg_attribute.attrelid AND pg_class.oid = pg_index.indrelid AND pg_index.indkey[1] = pg_attribute.attnum AND pg_index.indisprimary = ''t''

union all

SELECT pg_attribute.attname FROM pg_class, pg_attribute, pg_index WHERE pg_class.relname = l_relname AND pg_class.oid = pg_attribute.attrelid AND pg_class.oid = pg_index.indrelid AND pg_index.indkey[2] = pg_attribute.attnum AND pg_index.indisprimary = ''t''

union all

SELECT pg_attribute.attname FROM pg_class, pg_attribute, pg_index WHERE pg_class.relname = l_relname AND pg_class.oid = pg_attribute.attrelid AND pg_class.oid = pg_index.indrelid AND pg_index.indkey[3] = pg_attribute.attnum AND pg_index.indisprimary = ''t''

union all

SELECT pg_attribute.attname FROM pg_class, pg_attribute, pg_index WHERE pg_class.relname = l_relname AND pg_class.oid = pg_attribute.attrelid AND pg_class.oid = pg_index.indrelid AND pg_index.indkey[4] = pg_attribute.attnum AND pg_index.indisprimary = ''t''

union all

SELECT pg_attribute.attname FROM pg_class, pg_attribute, pg_index WHERE pg_class.relname = l_relname AND pg_class.oid = pg_attribute.attrelid AND pg_class.oid = pg_index.indrelid AND pg_index.indkey[5] = pg_attribute.attnum AND pg_index.indisprimary = ''t''

union all

SELECT pg_attribute.attname FROM pg_class, pg_attribute, pg_index WHERE pg_class.relname = l_relname AND pg_class.oid = pg_attribute.attrelid AND pg_class.oid = pg_index.indrelid AND pg_index.indkey[6] = pg_attribute.attnum AND pg_index.indisprimary = ''t''

union all

SELECT pg_attribute.attname FROM pg_class, pg_attribute, pg_index WHERE pg_class.relname = l_relname AND pg_class.oid = pg_attribute.attrelid AND pg_class.oid = pg_index.indrelid AND pg_index.indkey[7] = pg_attribute.attnum AND pg_index.indisprimary = ''t''

LOOP

if l_cntr > 0 then

l_pk_list := l_pk_list || '','';

end if;

l_pk_list := l_pk_list || l_pk.attname;

l_cntr := l_cntr + 1;

END LOOP;

return l_pk_list;

END;

' LANGUAGE 'plpgsql';

[Top]
No.
제목
작성자
작성일
조회
3004가장 근접한 ZIP code 찾기
정재익
2001-06-22
2544
3003Number of affected rows
정재익
2001-06-22
1931
3002eval() for PostgreSQL
정재익
2001-06-22
1959
3001Retrive comma separated PK attributes
정재익
2001-06-22
1906
3000Delete duplicate records in a table
정재익
2001-06-22
1909
2999Error catching
정재익
2001-06-21
1835
2998Operator --- IMP (>>> operator as logical implication)
정재익
2001-06-21
1804
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.019초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다