FireBird Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
파이어버드 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
IBPhoenix
FireBird Main site
볼랜드포럼 광고 모집

FireBird FAQ
[4] 자동으로 일련번호를 부여하는 방법
박지훈.임프 [cbuilder] 7570 읽음    2005-07-01 21:51
질문: 초보자(우)님

MS SQL은 자동으로 번호를 부여를 할 수있잔아요
IDENTITY 값을 주어서 데이터를 입력 할때 마다 생성이 되잔아요
인터베이스에는 그런것이 엄나요
아시는분 답변 쩜 부탁 합니다.

답변: 김백일님
 
물론 가능합니다.
GENERATOR를 쓰면 됩니다.
다음은 InterBase Tutorial에 있는 내용입니다. 참고하세요.

▶ Create a generator

1. Begin by checking the employee numbers in the Employee table, to confirm that the
highest employee number currently in use is 145:

SELECT emp_no from Employee
ORDER BY emp_no

Note: The statement above returns all the employee numbers so that you can confirm that 145 is the highest. The following statement produces the same information more efficiently:

SELECT max(emp_no) from Employee

2. Triggers often use generators, and the trigger you create in the next exercise is an example of one. Execute the following statement to create a generator called emp_no_gen.

CREATE GENERATOR emp_no_gen

3. Now initialize the generator to 145, the highest value currently in use.

SET GENERATOR emp_no_gen TO 145


▶ Create a trigger that generates a value

1. The next statements define a trigger named set_emp_no that makes use of the
emp_no_gen generator to generate unique sequential employee numbers and insert them
into the Employee table.

CREATE TRIGGER set_emp_no FOR Employee
BEFORE INSERT AS
BEGIN
  New.emp_no = gen_id(emp_no_gen, 1);
END

This statement says that the set_emp_no trigger will fire before an insert operation, and that it will create a new value for emp_no by calling the gen_id() function on the emp_no_gen generator with an increment of 1.

2. To test the generator, execute the following INSERT statement:

INSERT INTO Employee (first_name, last_name, dept_no, job_code, job_grade,
job_country, hire_date, salary, phone_ext)
VALUES ('Reed', 'Richards', '671', 'Eng', 5, 'USA', '07/27/95', '34000', '444')

Notice that you did not include a value for the emp_no column in the INSERT statement. Look at the new record by entering

SELECT * from Employee WHERE last_name = 'Richards'

The employee number is 146. Remember that the highest employee number before you created
the generator and inserted a new row was 145. The trigger has automatically assigned the new
employee the next employee number.

3. If your INSERT ran without errors and your SELECT returns the correct result set, commit
your work.

원문: http://firebird.borlandforum.com/impboard/impboard.dll?action=read&db=fb_qna&no=272

+ -

관련 글 리스트
4 자동으로 일련번호를 부여하는 방법 박지훈.임프 7570 2005/07/01
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.