sql:MySql create FUNCTION,VIEW,PROCEDURE

use geovindu;

#函数

DELIMITER $$
drop function if exists f_GetDepartmentName $$
CREATE function f_GetDepartmentName
(
   did int
)
returns nvarchar(400)
begin
declare str nvarchar(100);
select DepartmentName into str from DepartmentList where DepartmentID=did;
return IFNULL(str,‘‘);
end;
$$
DELIMITER ;

DELIMITER $$
DROP FUNCTION IF EXISTS `geovindu`.`f_GetAuthorName` $$
CREATE FUNCTION `geovindu`.`f_GetAuthorName` (did int) RETURNS varchar(400)
BEGIN
   declare str varchar(100);
return(select ifnull(AuthorName,‘‘) from AuthorList where AuthorID=did);
END $$
DELIMITER ;

#测试
SELECT  f_GetDepartmentName(1);
SELECT  f_GetAuthorName(1);

#视图

/*视图*/
select * from geovindu.views;

desc View_BookInfoList;

show create view View_BookInfoList;

select * from View_BookInfoList;

DELIMITER $$
drop view IF EXISTS View_BookInfoList  $$
CREATE VIEW `geovindu`.`View_BookInfoList` AS
  select BookInfoID , BookInfoKind , BookInfoPlace ,BookInfoSeries , BookInfoAuthor , BookInfoPress , BookInfoLanguage , BookInfoStatus , BookInfoOperatorId ,  BookInfoISBN , BookInfoBarCode , BookInfoName ,    BookInfoRemarks ,BookInfoAddDate,BookInfoPublish ,BookInfoPrice,
BookKindList.BookKindName,BookPlaceList.BookPlaceName,f_GetAuthorName(BookInfoAuthor)
from BookInfoList,BookKindList,BookPlaceList
where BookInfoList.BookInfoKind=BookKindList.BookKindID  and BookInfoList.BookInfoPlace=BookPlaceList.BookPlaceID;
$$
DELIMITER ;

#删除
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`DeleteBookKind` $$
CREATE PROCEDURE `geovindu`.`DeleteBookKind` (IN param1 INT)
BEGIN
         Delete From bookkindlist WHERE BookKindID  = param1;
END $$
DELIMITER ;

#查询所有
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`proc_Select_BookKindListAll` $$
CREATE PROCEDURE `geovindu`.`proc_Select_BookKindListAll` ()
BEGIN
    SELECT * FROM bookkindlist;
END $$
DELIMITER ;

select * from  `geovindu`.`bookkindlist`;
SELECT * FROM bookkindlist;

#统计
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`BookKindCount` $$
CREATE PROCEDURE `geovindu`.`BookKindCount` (OUT param1ID INT)
BEGIN
        select COUNT(*) into param1ID  From bookkindlist;
END $$
DELIMITER ;

#更新
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`proc_Update_BookKindList` $$
CREATE PROCEDURE `geovindu`.`proc_Update_BookKindList`
(
    IN param1ID Int,
    IN param1Name NVarChar(1000),
    IN param1Parent Int
)
BEGIN
IF NOT EXISTS (SELECT * FROM BookKindList WHERE BookKindName=param1Name) then #如果存在相同的记录,不更新名称
UPDATE BookKindList
	SET
		BookKindName=param1Name ,
		BookKindParent=param1Parent
	where
		BookKindID=param1ID;
ELSE
    UPDATE BookKindList
	SET BookKindParent=param1Parent
	where
		BookKindID=param1ID;
END IF;
END $$
DELIMITER ;

#查询一条
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`proc_Select_BookKindList` $$
CREATE PROCEDURE `geovindu`.`proc_Select_BookKindList` (IN param1 INT)
BEGIN
        SELECT * FROM BookKindList WHERE BookKindID = param1;
END $$
DELIMITER ;

call proc_Select_BookKindList (1);

#插入一条
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`proc_Insert_BookKindList` $$
CREATE PROCEDURE `geovindu`.`proc_Insert_BookKindList`
(
    IN param1Name NVarChar(1000),
    IN param1Parent Int
)
BEGIN
        insert into BookKindList(BookKindName,BookKindParent) values(param1Name,param1Parent);
END $$
DELIMITER ;

#插入一条返回值
DELIMITER $$
DROP PROCEDURE IF EXISTS `geovindu`.`proc_Insert_BookKindOut` $$
CREATE PROCEDURE `geovindu`.`proc_Insert_BookKindOut` (IN param1Name NVarChar(1000),IN param1Parent Int,OUT ID INT)
BEGIN
     IF NOT EXISTS (SELECT * FROM BookKindList WHERE BookKindName=param1Name) then   #如果存在相同的记录,不添加
        INSERT INTO BookKindList (BookKindName,BookKindParent)VALUES(param1Name ,param1Parent);
        #set ID=Last_insert_id()
        SELECT LAST_INSERT_ID() into ID;
      end if;
END $$
DELIMITER ;

  

时间: 2024-09-28 21:04:54

sql:MySql create FUNCTION,VIEW,PROCEDURE的相关文章

mysql create function error and solution way.

Error Code: 1418. This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable reason : mysql的设置默认是不允许创建函数 解决办法1: 执

MySQL This function has none of DETERMINISTIC, NO SQL...错误1418 的原因分析及解决方法

MySQL开启bin-log后,调用存储过程或者函数以及触发器时,会出现错误号为1418的错误: ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL,or READS SQL DATA in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creat

MySQL 自定义函数CREATE FUNCTION实例

分享一个MySQL 自定义函数CREATE FUNCTION的实例.mysql> delimiter $$mysql> CREATE FUNCTION myFunction-> (in_string VARCHAR(255),-> in_find_str VARCHAR(20),-> in_repl_str VARCHAR(20))->-> RETURNS VARCHAR(255)-> BEGIN-> DECLARE l_new_string VARC

oracle 备份数据库对象(存储过程PROCEDURE,FUNCTION,VIEW,TRIGGER...)

开发过程中,需要不停的备份数据库对象, 特别是存储过程, 每次手动备份不免很低能啊 历经几次修改终于, 完美了,O(∩_∩)O哈哈~      (当然,你也可以再改简便一点~~~) select dbms_metadata.get_ddl('PROCEDURE',"PROCEDURE_NAME",'NAG') 遇到大存储过程老是丢东西不说, 对象名 还老是 "用户名"."对象名" 的格式,腻烦人!~ CREATE OR REPLACE PROCE

MySQL This function has none of DETERMINISTIC, NO SQL...错误1418 的原因分析及解决方法 (转)

解决方法: 解决办法也有两种, 第一种是在创建子程序(存储过程.函数.触发器)时,声明为DETERMINISTIC或NO SQL与READS SQL DATA中的一个, 例如: CREATE DEFINER = CURRENT_USER PROCEDURE `NewProc`()     DETERMINISTIC BEGIN #Routine body goes here... END;; 第二种是信任子程序的创建者,禁止创建.修改子程序时对SUPER权限的要求,设置log_bin_trust

SQL Server 在多个数据库中创建同一个存储过程(Create Same Stored Procedure in All Databases)

原文:SQL Server 在多个数据库中创建同一个存储过程(Create Same Stored Procedure in All Databases) 一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 遇到的问题(Problems) 实现代码(SQL Codes) 方法一:拼接SQL: 方法二:调用模板存储过程创建存储过程: 总结 扩展阅读 参考文献(References) 二.背景(Contexts) 在我的数据库服务器上,同一个实例

Sql Create Function简单例子

create function test(@Num varchar(20))--@Num 参数 returns varchar(50) --返回值类型 as begin declare @MSG varchar(20) if(@Num =1) select @MSG ='正确' else select @MSG ='错误' return @MSG end --调用函数select dbo.test(2)--传递参数2返回结果:错误 --创建返回Table类型的函数 create function

Create function through MySQLdb

http://stackoverflow.com/questions/745538/create-function-through-mysqldb How can I define a multi-statement function or procedure in using the MySQLdb lib in python? Example: import MySQLdb db = MySQLdb.connect(db='service') c = db.cursor() c.execut

Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

catalog 1. How to Add New Functions to MySQL 2. Features of the User-Defined Function Interface 3. User-Defined Function 4. UDF Argument Processing 5. UDF Return Values and Error Handling 6. UDF Compiling and Installing 7. Adding a New Native Functio