CS

SQL - ์ €์žฅ ํ”„๋กœ์‹œ์ €(Stored Procedure)

sun_young 2024. 11. 12. 23:45
๐Ÿ”Ž ์ €์žฅ ํ”„๋กœ์‹œ์ €๋ž€?
DB ๋‚ด๋ถ€์— ์ €์žฅ๋œ ์ผ๋ จ์˜ SQL ๋ช…๋ น๋ฌธ๋“ค์„ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์ฟผ๋ฆฌ ์ง‘ํ•ฉ

 

* ํ”„๋กœ์‹œ์ €๋ฅผ ๋งŒ๋“ค์–ด๋‘๋ฉด, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์—ฌ๋Ÿฌ ์ƒํ™ฉ์— ๋”ฐ๋ผ ํ•ด๋‹น ์ฟผ๋ฆฌ๋ฌธ์ด ํ•„์š”ํ•  ๋•Œ ์ธ์ž ๊ฐ’๋งŒ ์ „๋‹ฌํ•˜์—ฌ ์‰ฝ๊ฒŒ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฌผ์„ ๋ฐ›์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค. 

 

ํ”„๋กœ์‹œ์ € ์ƒ์„ฑ

CREATE OR REPLACE PROCEDURE ํ”„๋กœ์‹œ์ €๋ช…(๋ณ€์ˆ˜๋ช…1 IN ๋ฐ์ดํ„ฐ ํƒ€์ž…, ๋ณ€๊ตฌ๋ช…2 OUT ๋ฐ์ดํ„ฐ ํƒ€์ž…)
IS
[
๋ณ€์ˆ˜๋ช…1 ๋ฐ์ดํ„ฐ ํƒ€์ž…;
๋ณ€์ˆ˜๋ช…2 ๋ฐ์ดํ„ฐ ํƒ€์ž…;
..
]
BEGIN
 ํ•„์š”ํ•œ ๊ธฐ๋Šฅ;
END;
  • ์ธ์ž ๊ฐ’์€ ํ•„์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค.
  • BEGIN๊ณผ END ์‚ฌ์ด์— ์ธ์ž ๊ฐ’์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ”„๋กœ์‹œ์ € ํ˜ธ์ถœ

EXEC ํ”„๋กœ์‹œ์ €๋ช…;

 

ํ”„๋กœ์‹œ์ €์˜ ์žฅ์ ๊ณผ ๋‹จ์ 

์žฅ์ 

  • ์ตœ์ ํ™” & ์บ์‹œ
    ํ”„๋กœ์‹œ์ €์˜ ์ตœ์ดˆ ์‹คํ–‰ ์‹œ ์ตœ์ ํ™” ์ƒํƒœ๋กœ ์ปดํŒŒ์ผ๋˜๋ฉฐ, ๊ทธ ์ดํ›„ ํ”„๋กœ์‹œ์ € ์บ์‹œ์— ์ €์žฅ๋œ๋‹ค.
    ๋งŒ์•ฝ ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉ๋  ๋•Œ ๋‹ค์‹œ ์ปดํŒŒ์ผ ์ž‘์—…์„ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  ์บ์‹œ์—์„œ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋œ๋‹ค.  
  • ์œ ์ง€ ๋ณด์ˆ˜ & ์žฌํ™œ์šฉ
    ์ž‘์—…์ด ๋ณ€๊ฒฝ๋  ๋•Œ ๋‹ค๋ฅธ ์ž‘์—…์€ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  ํ”„๋กœ์‹œ์ € ๋‚ด๋ถ€์—์„œ ์ˆ˜์ •๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.
  • ํŠธ๋ž˜ํ”ฝ ๊ฐ์†Œ
    ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ง์ ‘ SQL๋ฌธ์„ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ , ํ”„๋กœ์‹œ์ €๋ช…์— ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ ๋‹ด์•„ ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค.
    SQL๋ฌธ์ด ์„œ๋ฒ„์— ์ด๋ฏธ ์ €์žฅ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„ ๋„คํŠธ์›Œํฌ ์ƒ ํŠธ๋ž˜ํ”ฝ์ด ๊ฐ์†Œ 
  • ๋ณด์•ˆ ๊ฐ•ํ™”
    ์‚ฌ์šฉ์ž๋ณ„๋กœ ํ…Œ์ด๋ธ”์— ๊ถŒํ•œ์„ ์ฃผ๋Š” ๊ฒŒ ์•„๋‹Œ ์ €์žฅ ํ”„๋กœ์‹œ์ €์—๋งŒ ์ ‘๊ทผ ๊ถŒํ•œ์„ ์คŒ์œผ๋กœ์จ ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์ •๋ณด๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋…ธ์ถœํ•˜์ง€ ์•Š๊ณ  ํ”„๋กœ์‹œ์ €์—์„œ ์„ ํƒํ•œ ์ •๋ณด๋งŒ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋‹ค.

๋‹จ์ 

  • ๋‚ฎ์€ ํ˜ธํ•œ์„ฑ
    ๊ตฌ๋ฌธ ๊ทœ์น™์ด SQL/PSM ํ‘œ์ค€๊ณผ์˜ ํ˜ธํ™˜์„ฑ์ด ๋‚ฎ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ ์ž์‚ฐ์œผ๋กœ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋‚˜์˜๋‹ค.
  • ๋””๋ฒ„๊น… ์–ด๋ ค์›€
    ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์–ด๋””์„œ ์ž˜๋ชป๋๋Š”์ง€ ๋””๋ฒ„๊น…ํ•˜๋Š” ๊ฒƒ์ด ํž˜๋“ค ์ˆ˜ ์žˆ๋‹ค. 
  • ๋‚ฎ์€ ์ฒ˜๋ฆฌ ์„ฑ๋Šฅ
    ๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž ์—ฐ์‚ฐ์—์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ธ C๋‚˜ Java๋ณด๋‹ค ์„ฑ๋Šฅ์ด ๋Š๋ฆฌ๋‹ค.

 

Procedure์™€ Function์˜ ์ฐจ์ด

์ถœ์ฒ˜ : https://mjn5027.tistory.com/47

 

* ํ”„๋กœ์‹œ์ €๋Š” ํด๋ผ์ด์–ธํŠธ์—์„œ ๊ฐ’์„ ๋ฐ›์•„ ์„œ๋ฒ„์—์„œ ์ž‘์—…์„ ํ•œ ๋’ค ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌํ•˜์ง€๋งŒ, ํ•จ์ˆ˜๋Š” ํด๋ผ์ด์–ธํŠธ์—์„œ ๊ฐ’์„ ๋ฐ›๊ณ  ์„œ๋ฒ„์—์„œ ํ•„์š”ํ•œ ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ํด๋ผ์ด์–ธํŠธ์—์„œ ์ž‘์—…์„ ํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

  • ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ ํด๋ผ์ด์–ธํŠธ์—์„œ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ์‹œ์ €๋ณด๋‹ค๋Š” ๋Š๋ฆฌ๋‹ค.
  • ํ•จ์ˆ˜๋Š” ํ…Œ์ด๋ธ”์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋”๋ณด๊ธฐ

[์ฐธ๊ณ ]

https://velog.io/@sweet_sumin/%EC%A0%80%EC%9E%A5-%ED%94%84%EB%A1%9C%EC%8B%9C%EC%A0%80-Stored-Procedure

 

์ €์žฅ ํ”„๋กœ์‹œ์ € (Stored Procedure)

DB ๋‚ด๋ถ€์— ์ €์žฅ๋œ ์ผ๋ จ์˜ SQL ๋ช…๋ น๋ฌธ๋“ค์„ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์ฟผ๋ฆฌ์˜ ์ง‘ํ•ฉ. ์ฆ‰, DB์— ๋Œ€ํ•œ ์ž‘์—…์„ ์ •๋ฆฌํ•œ ์ ˆ์ฐจ๋ฅผ RDBMS(๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ)์— ์ €์žฅํ•œ ์ฟผ๋ฆฌ์˜ ์ง‘ํ•ฉ์ด๋‹ค. ์˜

velog.io

 

https://github.com/gyoogle/tech-interview-for-developer/blob/master/Computer%20Science/Database/%EC%A0%80%EC%9E%A5%20%ED%94%84%EB%A1%9C%EC%8B%9C%EC%A0%80(Stored%20PROCEDURE).md

 

tech-interview-for-developer/Computer Science/Database/์ €์žฅ ํ”„๋กœ์‹œ์ €(Stored PROCEDURE).md at master · gyoogle/tech-interv

๐Ÿ‘ถ๐Ÿป ์‹ ์ž… ๊ฐœ๋ฐœ์ž ์ „๊ณต ์ง€์‹ & ๊ธฐ์ˆ  ๋ฉด์ ‘ ๋ฐฑ๊ณผ์‚ฌ์ „ ๐Ÿ“–. Contribute to gyoogle/tech-interview-for-developer development by creating an account on GitHub.

github.com

 

https://infoinhere.tistory.com/78

 

Procedure๊ณผ Function์˜ ์ฐจ์ด

Procedure์™€ Function์˜ ์ฐจ์ด Procedure๋Š” ๋กœ์ง์„ ๊ธฐ์ˆ ํ•˜์—ฌ ํ•ด๋‹น ์—…๋ฌด ์ฒ˜๋ฆฌ๋ฅผ ์ง์ ‘ ํ•˜๋Š” ์šฉ๋„๋กœ ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ , Function์€ ๋กœ์ง์„ ๋„์™€์ฃผ๋Š” ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. Procedure์€ select, where๋ฌธ ๋“ฑ์—์„œ ์‚ฌ์šฉ์ด ๋ถˆ

infoinhere.tistory.com

 

'CS' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๊ต์ฐฉ์ƒํƒœ  (0) 2024.11.16
์จ๋“œ ํŒŒํ‹ฐ(3rd party)๋ž€?  (0) 2024.11.14
SQL - DB ํŠธ๋žœ์žญ์…˜(Transaction)  (2) 2024.11.13
B Tree & B+ Tree  (0) 2024.11.11
SQL - ์ธ๋ฑ์Šค(Index)  (3) 2024.11.10