SQLServer向MySQL移植笔记(一)自定义函数


这个是我在移植数据库后,从SQL Server移植过来的。对于SQL大侠们,这个算是毛毛雨了。
SQL Server2000的自定义函数如下:
  1. CREATE FUNCTION getfiletype (@kuozhanming   varchar(100))  
  2. RETURNS varchar(100) 
  3. AS  
  4. BEGIN 
  5.     declare @filetype    varchar(100)
  6.     declare @index int
  7.     set @filetype=''
  8.     if (@kuozhanming is null and @kuozhanming = '')
  9.         select @filetype = ''
  10.     
  11.         
  12.     else
  13.         begin
  14.             SET @kuozhanming = Reverse(@kuozhanming)
  15.             SELECT @index = charindex('.',@kuozhanming) 
  16.             if (@index < 2)
  17.                 select @filetype = ''
  18.             else
  19.                 begin   
  20.                     SET @kuozhanming = left(@kuozhanming,@index - 1)
  21.                     SET @kuozhanming = Reverse(@kuozhanming)
  22.                 
  23.                     SET @kuozhanming = Lower(@kuozhanming)
  24.                     
  25.                     
  26.                     SELECT @filetype = 
  27.                      CASE( @kuozhanming)
  28.                         WHEN 'doc' THEN  'application/msword'
  29.                         WHEN 'log' THEN 'application/octet-stream'
  30.                         WHEN 'ini' THEN 'application/octet-stream'
  31.                         WHEN 'exe' THEN 'application/octet-stream'
  32.                         WHEN 'dll' THEN 'application/octet-stream'
  33.                         WHEN 'rar' THEN 'application/octet-stream'
  34.                         WHEN 'txt' THEN 'text/html'
  35.                         WHEN 'html' THEN 'text/html'
  36.                         WHEN 'htt' THEN 'text/html'
  37.                         
  38.                         WHEN 'bmp' THEN 'image/bmp'
  39.                         WHEN 'jpg' THEN 'image/pjpeg'
  40.                         WHEN 'avi' THEN 'video/avi'
  41.                         else @kuozhanming
  42.                     end 
  43.                 end
  44.         end
  45.     
  46.     return @filetype
  47. END

MySQL实现相同功能的的自定义函数如下
  1. drop function  if exists getfiletype;
  2. DELIMITER |
  3. /* 
  4.  取出文件名中扩展名的文件名称 
  5.  比如 
  6.  c:/1234/5678.txt 
  7.  则返回 txt对应的 text/html; 
  8. */ 
  9. CREATE FUNCTION getfiletype (t_kuozhanming   varchar(100))  
  10. RETURNS varchar(100
  11. BEGIN 
  12.     declare t_filetype    varchar(100);
  13.     declare t_index int;
  14.     set t_filetype='';
  15.     if (t_kuozhanming is null or t_kuozhanming = '') then
  16.         set t_filetype = '';
  17.     else
  18.         SET t_kuozhanming = Reverse(t_kuozhanming);
  19.         SET t_index = locate('.',t_kuozhanming) ;
  20.         if (t_index < 2) then
  21.             set t_filetype = '';
  22.         else
  23.             SET t_kuozhanming = left(t_kuozhanming,t_index - 1);
  24.             SET t_kuozhanming = Reverse(t_kuozhanming);
  25.             SET t_kuozhanming = Lower(t_kuozhanming);
  26.             SELECT CASE( t_kuozhanming)
  27.                         WHEN 'doc' THEN  'application/msword'
  28.                         WHEN 'log' THEN 'application/octet-stream'
  29.                         WHEN 'ini' THEN 'application/octet-stream'
  30.                         WHEN 'exe' THEN 'application/octet-stream'
  31.                         WHEN 'dll' THEN 'application/octet-stream'
  32.                         WHEN 'rar' THEN 'application/octet-stream'
  33.                         WHEN 'txt' THEN 'text/html'
  34.                         WHEN 'html' THEN 'text/html'
  35.                         WHEN 'htt' THEN 'text/html'
  36.                         
  37.                         WHEN 'bmp' THEN 'image/bmp'
  38.                         WHEN 'jpg' THEN 'image/pjpeg'
  39.                         WHEN 'avi' THEN 'video/avi'
  40.                         else t_kuozhanming
  41.                     end into t_filetype;
  42.         end if;
  43.     end if;
  44.     
  45.     return t_filetype;
  46. end|
  47. DELIMITER ;

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值