টেবিল ক্রিয়েট করা জন্য কোড :
পুরা কোড টা দেখতে এই রকম হবে :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table instructor ( | |
ID varchar(5), | |
name varchar(20) not null, | |
dept_name varchar(20), | |
salary numeric(8,2) check (salary > 29000), | |
primary key (ID), | |
foreign key (dept_name) references department | |
on delete set null | |
); |
মনে করি আমর উপরের মত করে একটা টেবিল তৈরি করেছি । এখন আমরা এমন একটা ফাংশন বানাতে চাচ্ছি যেটা ওই টেবিল থেকে কিছু কলাম বা সবগুলি কলাম নিয়ে নতুন নতুন একটা টেবিল আমাদের কে রিটার্ন করবে কিছু শর্তের উপর নির্ভর করে । এটা করার জন্য প্রথমে ওই টেবিল থেকে আমি যে যে কলাম নিতে চাই তার সবগুই নিয়ে এটা অবজেক্ট টাইপ বানাতা হবে নিচের মত করে । আমি টাইপ টির নাম দিয়েছি obj_type .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- First we have to create an object same as our table | |
-- from where we want to return table like .. | |
create or replace TYPE obj_type AS OBJECT ( | |
id VARCHAR2(5 byte), | |
name VARCHAR2(20 byte), | |
dep_name VARCHAR2(20 byte), | |
salary NUMBER(8, 2) | |
); |
আমরা যে অবজেক্ট টাইপ [ obj_type ] বানালাম সেই টাইপের নতুন একটা [ obj_of_table ] একই ধরনের অবজেক্ট বানাতে হবে নিচের মত করে ।
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Now create a type 'as table of' the above object | |
create or replace type obj_of_table | |
as table of obj_type; |
আমাদের নতুন টাইপ এর নাম দিলাম obj_of_table ফাংশনের রিটার্ন টাইপ হিসাবে কিন্তু এটাই ব্যবহার করতে হবে । এখন আমরা টেবিল রিটার্ন করার জন্য ফাংশন বানাতে পারি । এই ফাংশনের রিটার্ন টাইপ হবে obj_of_table এবং টেবিল টি আমরা new_table নামের ভারিয়েবলে জমা করে রাখব এবং সব শেষ আমরা new_table কে রিটার্ন করে দিব ।
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or replace function return_table_func(new_id in int) | |
return obj_of_table | |
as new_table obj_of_table; -- সেমিক্লোন না দিলে কিন্তু এরর দিবে ;) | |
-- new_table = variable name and obj_of_table = variable type [like varchar] | |
begin | |
-- put list of column and keep in mind | |
-- here type is obj_type() not obj_of_table() | |
-- in select section :) | |
select obj_type(id, name, dept_name, salary) | |
bulk collect into new_table -- This will put the value into new_table | |
from instructor -- take value from instructor | |
where instructor.id = new_id; -- condition that you want | |
return (new_table); -- new_table | |
end; |
সব কাজ শেষ :) এখন ফাংশনটি কল করার পালা । মনে করি ফাংশনের প্যারামিটার হিসাবে আমি আইডি 10 পাঠাব এবং যার আইডি এই আইডির সাথে মিলে যাবে সেই অনুসাতে টেবিল রিটার্ন করবে । ফাংশনটি কল করা জন্য নিচের মত করে লিখতে হবে ।
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Call Function | |
select * from table (RETURN_TABLE_FUNC(10)); |
পুরা কোড টা দেখতে এই রকম হবে :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table instructor ( | |
ID varchar(5), | |
name varchar(20) not null, | |
dept_name varchar(20), | |
salary numeric(8,2) check (salary > 29000), | |
primary key (ID), | |
foreign key (dept_name) references department | |
on delete set null | |
); | |
-- First we have to create an object same as our table | |
-- from where we want to return table like .. | |
create or replace TYPE obj_type AS OBJECT ( | |
id VARCHAR2(5 byte), | |
name VARCHAR2(20 byte), | |
dep_name VARCHAR2(20 byte), | |
salary NUMBER(8, 2) | |
); | |
-- New create a type 'as table of' the above object | |
create or replace type obj_of_table as table of obj_type; | |
create or replace function return_table_func(new_id in int) | |
return obj_of_table | |
as new_table obj_of_table; -- সেমিক্লোন না দিলে কিন্তু এরর দিবে :) | |
begin | |
-- put list of column and keep in mind | |
-- here type is obj_type() not obj_of_table() | |
-- in select section :) | |
select obj_type(id, name, dept_name, salary) | |
bulk collect into new_table -- This will put the value into new_table | |
from instructor -- take value from instructor | |
where instructor.id = new_id; -- condition that you want | |
return (new_table); -- new_table | |
end; | |
-- Call Function | |
select * from table (RETURN_TABLE_FUNC(10)); |
সব কিছু ঠিকঠাক ভাবে রান করতে পারলে কোন সমস্যা হওয়ার কথা না । তার পরও ... কিছু হলে কিন্তু আমি কোন ভাবেই দায়ি থাকব না :P এতক্ষণ সময় নষ্ট করে লিখা টি পড়ার জন্য ধন্যবাদ :) সবার পরীক্ষা ভাল হোক । হ্যাপি কোডিং :)
Comments
Post a Comment