DB2_使用別名
目的:
測試DB2使用別名,本指令碼摘錄自DB2安裝目錄admin_scripts/public_alias.db2。
版本:Windows DB2 Express-C V9.7
說明:由於該版本不支援DBMS_OUTPUT,部分操作報:SQL8004N 找不到所請求功能的有效許可證金鑰。
操作步驟:
使用"db2cmd db2 -td@"進入互動模式,執行後續操作。
-- SAMPLE DETAILS
--
-- (1) Admin user creates PUBLIC alias "app", "tbsp" for table
-- SYSIBMADM.APPLICATIONS and SYSIBMADM.TBSP_UTILIZATION respectively.
--
-- (2) Admin creates a module database_monitoring in dba_object schema so
-- that DBAs can use it.
--
-- (3) Admin user creates public alias dbms_monit for module database_monitoring.
--
-- (4) Admin user alters the module by adding procedures.
--
-- (5) User bob and pat use the module database_monitoring for monitoring the database,
-- but they use dbms_monit alias for monitoring.
--
-- (6) User pat creates one more module of same name dbms_monit in the schema "pat".
--
-- (7) User pat calls the procedure in different ways.
--
-- (8) User bob calls the procedure in different ways.
--
-- ***************************************************/
-- SET UP */
-- ***************************************************/
-- Connect to Sample
CONNECT TO sample@
echo@
echo ********************************@
echo USE OF PUBLIC ALIASES FOR TABLE @
echo ********************************@
echo@
-- Create schema to store objects used by DBA
CREATE SCHEMA dba_object@
SET CURRENT SCHEMA = dba_object@
SET CURRENT PATH = CURRENT PATH, dba_object@
-- Create public alias for table SYSIBMADM.APPLICATIONS
CREATE PUBLIC ALIAS app FOR TABLE SYSIBMADM.APPLICATIONS@
-- Create public alias for table SYSIBMADM.TBSP_UTILIZATION
CREATE PUBLIC ALIAS tbsp FOR TABLE SYSIBMADM.TBSP_UTILIZATION@
-- Create module database_monitoring
CREATE MODULE database_monitoring@
-- Grant execute privilege to user bob
GRANT EXECUTE ON MODULE database_monitoring TO USER bob@
-- Reset connection
CONNECT RESET@
-- Connect to sample
CONNECT TO sample@
-- Alter module database_monitoring to publish procedure
-- tbsp_detail. Users will use full module name to alter
-- the module.
ALTER MODULE dba_object.database_monitoring PUBLISH
PROCEDURE tbsp_detail()@
-- Alter module database_monitoring to publish procedure
-- app_detail. Users will use full module name to alter
-- the module.
ALTER MODULE dba_object.database_monitoring PUBLISH
PROCEDURE app_detail()@
-- Alter module database_monitoring to add procedure
-- tbsp_detail. Users will use full module name to alter
-- the module.
ALTER MODULE dba_object.database_monitoring ADD
PROCEDURE tbsp_detail()
BEGIN
-- Declare variables
DECLARE v_tbsp_id INTEGER;
DECLARE v_tbsp_name CHAR(15);
DECLARE v_tbsp_type CHAR(5);
DECLARE v_tbsp_content_type CHAR(10);
DECLARE v_tbsp_util_prcntg DECIMAL(5,2);
DECLARE v_dbpgname CHAR(20);
DECLARE c_tbsp_statistics CURSOR;
SET c_tbsp_statistics = CURSOR FOR SELECT TBSP_ID, TBSP_NAME,
TBSP_TYPE, TBSP_CONTENT_TYPE, TBSP_UTILIZATION_PERCENT,
DBPGNAME
FROM tbsp;
-- Open cursor c_tbsp_statistics
OPEN c_tbsp_statistics;
CALL DBMS_OUTPUT.NEW_LINE;
CALL DBMS_OUTPUT.PUT_LINE('---------------------');
CALL DBMS_OUTPUT.PUT_LINE('TABLESPACE STATISTICS');
CALL DBMS_OUTPUT.PUT_LINE('---------------------');
CALL DBMS_OUTPUT.NEW_LINE;
CALL DBMS_OUTPUT.PUT ('TBSPACE ID'||' '|| 'TBSPACE NAME'||' '||
'TYPE'||' '||'CONTENT TYPE'||' '|| '% USED'||' '||'DBPAGENAME');
CALL DBMS_OUTPUT.NEW_LINE;
CALL DBMS_OUTPUT.PUT_LINE('----------'||' '||'------------'||' '||
'----'||' '||'------------'||' '||'------'||' '||'----------');
fetch_loop:
LOOP
-- Fetch values from cursor
FETCH FROM c_tbsp_statistics INTO
v_tbsp_id, v_tbsp_name, v_tbsp_type, v_tbsp_content_type,
v_tbsp_util_prcntg, v_dbpgname;
IF c_tbsp_statistics IS NOT FOUND
THEN LEAVE fetch_loop;
END IF;
CALL DBMS_OUTPUT.NEW_LINE;
CALL DBMS_OUTPUT.PUT(v_tbsp_id);
CALL DBMS_OUTPUT.PUT(' ');
CALL DBMS_OUTPUT.PUT(' ');
CALL DBMS_OUTPUT.PUT(v_tbsp_name);
CALL DBMS_OUTPUT.PUT(' ');
CALL DBMS_OUTPUT.PUT(v_tbsp_type);
CALL DBMS_OUTPUT.PUT(' ');
CALL DBMS_OUTPUT.PUT(v_tbsp_content_type);
CALL DBMS_OUTPUT.PUT(' ');
CALL DBMS_OUTPUT.PUT(v_tbsp_util_prcntg);
CALL DBMS_OUTPUT.PUT(' ');
CALL DBMS_OUTPUT.PUT(v_dbpgname);
CALL DBMS_OUTPUT.NEW_LINE;
END LOOP fetch_loop;
-- Close cursor c_tbsp_statistics
CLOSE c_tbsp_statistics;
END@
echo @
echo ********************************@
echo USE OF PUBLIC ALIAS FOR MODULE @
echo ********************************@
echo @
-- Now admin user creates public alias for object dba_object.database_monitoring
-- which can be accessed by all the users who have privilege to use this
-- object, without using the full object name.
CREATE PUBLIC ALIAS dbms_monit FOR MODULE dba_object.database_monitoring@
-- Reset connection
CONNECT RESET@
echo @
echo ********************************@
echo USE OF PRIVATE ALIAS FOR MODULE @
echo ********************************@
echo @
-- User bob calls the procedure in different ways
CONNECT TO SAMPLE USER bob USING bob1234@
SET SERVEROUTPUT ON@
-- Call module by using full object name
CALL dba_object.database_monitoring.tbsp_detail()@
-- Create private alias of object dba_object.database_monitoring
-- to avoid use of full object name
CREATE ALIAS db_monitoring FOR MODULE dba_object.database_monitoring@
-- Call module by using private alias
CALL db_monitoring.tbsp_detail()@
-- Call module by using public alias
CALL dbms_monit.tbsp_detail()@
-- Connect to sample
CONNECT TO sample@
-- Alter module database_monitoring to add procedure
-- app_detail. Users will use full module name to alter
-- the module.
ALTER MODULE dba_object.database_monitoring ADD
PROCEDURE app_detail()
BEGIN
-- Declare variables
DECLARE v_agent_id INTEGER ;
DECLARE v_app_name CHAR(20);
DECLARE v_auth_id CHAR(15);
DECLARE v_app_id CHAR(26);
DECLARE v_app_status CHAR(15);
DECLARE c_app_detail CURSOR;
SET c_app_detail = CURSOR FOR SELECT AGENT_ID, APPL_NAME,
AUTHID, APPL_ID, APPL_STATUS
FROM app
ORDER BY AGENT_ID ASC;
-- Open cursor c_app_detail
OPEN c_app_detail;
CALL DBMS_OUTPUT.NEW_LINE;
CALL DBMS_OUTPUT.PUT_LINE('------------------');
CALL DBMS_OUTPUT.PUT_LINE('APPLICATION STATUS');
CALL DBMS_OUTPUT.PUT_LINE('------------------');
CALL DBMS_OUTPUT.NEW_LINE;
CALL DBMS_OUTPUT.PUT
('AGENT ID '||' '|| 'APPLICATION NAME '||' '||
'AUTHORIZATION ID '||' '|| 'APPLICATION ID '||' '||
'STATUS');
CALL DBMS_OUTPUT.NEW_LINE;
CALL DBMS_OUTPUT.PUT_LINE('--------'||' '||
'----------------'||' '||' -----------------'||' '||
'-------------------------'||' '|| '-----------');
fetch_loop:
LOOP
-- Fetch values from cursor
FETCH FROM c_app_detail INTO
v_agent_id, v_app_name, v_auth_id, v_app_id,
v_app_status;
IF c_app_detail IS NOT FOUND
THEN LEAVE fetch_loop;
END IF;
CALL DBMS_OUTPUT.NEW_LINE;
CALL DBMS_OUTPUT.PUT(v_agent_id);
CALL DBMS_OUTPUT.PUT(' ');
CALL DBMS_OUTPUT.PUT(v_app_name);
CALL DBMS_OUTPUT.PUT(' ');
CALL DBMS_OUTPUT.PUT(v_auth_id);
CALL DBMS_OUTPUT.PUT(' ');
CALL DBMS_OUTPUT.PUT(v_app_id);
CALL DBMS_OUTPUT.PUT(' ');
CALL DBMS_OUTPUT.PUT(v_app_status);
CALL DBMS_OUTPUT.NEW_LINE;
END LOOP fetch_loop;
-- Close cursor c_app_detail
CLOSE c_app_detail;
END@
-- Reset connection
CONNECT RESET@
-- User bob calls the procedure in different ways
CONNECT TO SAMPLE USER bob using bob1234@
SET SERVEROUTPUT ON@
-- Call procedure by using full object name
CALL dba_object.database_monitoring.app_detail()@
-- Call procedure by using private alias
CALL db_monitoring.app_detail()@
-- Call procedure by using public alias
CALL dbms_monit.app_detail()@
-- Reset connection
CONNECT RESET@
echo@
echo ******************@
echo OBJECT RESOLUTION @
echo ******************@
echo@
-- Connect to sample
CONNECT TO sample USER pat USING pat1234@
-- User pat creates one more module of same name in "pat" schema
CREATE MODULE dbms_monit@
-- Alter module dbms_monit to publish same procedure app_detail
ALTER MODULE dbms_monit PUBLISH
PROCEDURE app_detail()@
ALTER MODULE dbms_monit ADD
PROCEDURE app_detail()
BEGIN
-- Declare variables
DECLARE v_agent_id INTEGER ;
DECLARE v_app_name CHAR(20);
DECLARE v_app_status CHAR(15);
DECLARE c_app_detail CURSOR;
SET c_app_detail = CURSOR FOR SELECT AGENT_ID, APPL_NAME,
APPL_STATUS
FROM app
ORDER BY AGENT_ID ASC;
-- Open cursor c_app_detail
OPEN c_app_detail;
CALL DBMS_OUTPUT.NEW_LINE;
CALL DBMS_OUTPUT.PUT_LINE('------------------');
CALL DBMS_OUTPUT.PUT_LINE('APPLICATION STATUS');
CALL DBMS_OUTPUT.PUT_LINE('------------------');
CALL DBMS_OUTPUT.NEW_LINE;
CALL DBMS_OUTPUT.PUT
('AGENT ID '||' '|| 'APPLICATION NAME '||' '|| 'STATUS');
CALL DBMS_OUTPUT.NEW_LINE;
CALL DBMS_OUTPUT.PUT_LINE('--------'||' '||
'----------------'||' '|| '-----------');
fetch_loop:
LOOP
-- Fetch values from cursor
FETCH FROM c_app_detail INTO
v_agent_id, v_app_name, v_app_status;
IF c_app_detail IS NOT FOUND
THEN LEAVE fetch_loop;
END IF;
CALL DBMS_OUTPUT.NEW_LINE;
CALL DBMS_OUTPUT.PUT(v_agent_id);
CALL DBMS_OUTPUT.PUT(' ');
CALL DBMS_OUTPUT.PUT(v_app_name);
CALL DBMS_OUTPUT.PUT(' ');
CALL DBMS_OUTPUT.PUT(v_app_status);
CALL DBMS_OUTPUT.NEW_LINE;
END LOOP fetch_loop;
-- Close cursor c_app_detail
CLOSE c_app_detail;
END@
-- User pat calls the procedure in different ways
SET SERVEROUTPUT ON@
-- Call procedure of schema pat
CALL dbms_monit.app_detail()@
echo "Above output is expected"@
echo@
-- Reset connection
CONNECT RESET@
-- User bob drops procedure from module
CONNECT TO sample user bob using bob1234@
SET SERVEROUTPUT ON@
-- Alter module by dropping procedure
ALTER MODULE pat.dbms_monit
DROP PROCEDURE app_detail@
echo "Above output is expected"@
echo@
-- Call procedure
CALL dbms_monit.app_detail()@
-- User pat drops procedure from module
CONNECT TO sample USER pat USING pat1234@
SET SERVEROUTPUT ON@
-- Alter module by dropping procedure
ALTER MODULE dbms_monit
DROP PROCEDURE app_detail@
-- Call procedure
CALL dbms_monit.app_detail()@
echo "Above output is expected"@
echo@
-- Drop module dbms_monit
DROP MODULE dbms_monit@
-- Call procedure after dropping module
CALL dbms_monit.app_detail()@
echo "Above output is expected"@
echo@
-- Reset connection
CONNECT RESET@
-- Drop modules and aliases
CONNECT TO sample@
DROP PUBLIC ALIAS app FOR TABLE@
DROP PUBLIC ALIAS tbsp FOR TABLE @
DROP ALIAS bob.db_monitoring FOR MODULE@
DROP MODULE dba_object.database_monitoring@
DROP PUBLIC ALIAS dbms_monit FOR MODULE@
DROP SCHEMA dba_object RESTRICT@
CONNECT RESET@
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/18922393/viewspace-702290/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- yii別名的定義和別名的獲取以及別名的使用
- DB2_簡單命令列DB2命令列
- DB2_更新SQL欄位DB2SQL
- 使用別名訪問MSSQL ExpressSQLExpress
- SQLite中的SELECT子句使用別名SQLite
- MySQL不支援DELETE使用表別名?MySqldelete
- linux怎麼使用alias建立命令別名?linux系統使用alias建立命令別名的技巧Linux
- 使用Mac命令別名,提升工作效率Mac
- 模板別名
- TypeScript type 型別別名TypeScript型別
- 使用Node.js為require設定別名(alias)Node.jsUI
- MySQL 中 DELETE 語句中可以使用別名麼?MySqldelete
- SQL Server 別名(as)SQLServer
- [Git] 別名(alias)Git
- vscode配置別名VSCode
- TypeScript 強大的型別別名TypeScript型別
- MyBatis-02-別名MyBatis
- [20200417]xdate別名.txt
- Laravel Sail別名配置LaravelAI
- Webpack 中css 如何 import 使用 alias別名 相對路徑WebCSSImport
- sql中別名as,不寫,以及使用雙引號總結SQL
- Git定義命令別名Git
- [20200214]xargs與別名.txt
- [20191128]date命令別名.txt
- 1.5 - Laravel 5.6 - Alias 別名Laravel
- Linux-別名設定Linux
- vue3 配置 @ 別名Vue
- php: 操作elasticsearch的別名PHPElasticsearch
- Laravel 模型使用軟刪除-左連線查詢-表起別名Laravel模型
- 如何使用虛擬主機cPanel新建別名共享一個網站網站
- vue3+vite專案中使用alias設定目錄別名VueVite
- Linux命令學習( 使用命令,檢視手冊,取別名等)Linux
- 使用 ABAP Open SQL 的 Select AS 別名,提高程式碼可讀性SQL
- Mac 上 ssh 別名登入Mac
- Linux基礎命令---alias別名Linux
- 不可或缺的 Bash 別名
- 命令別名:保護和服務
- [20211213]完善date命令別名.txt
- SpringBoot 引數別名實現Spring Boot