zoukankan      html  css  js  c++  java
  • sql中的高级编程(函数,存储过程,视图)

    一、函数:用sql写一个函数,调用这个函数,返回一张数据表table

    CREATE FUNCTION FunName (  
     
    )  
    RETURNS @TempTable table    
    (  
    roleid int ,rolename nvarchar(100)
    )  
      
    AS  
    BEGIN  
    declare @roleid int 
    declare @rolename  nvarchar(100)
    
    select @roleid=RoleId  , @rolename =RoleName from CICRole 
    
    insert into @TempTable  values(
    @roleid , @rolename
    )
    
    
    RETURN 
    
    END
    

    注解:

    1.CICRole 为一个数据库中现有的数据表;
    
    
    2.这个函数执行过程就是先声明三个变量 :TempTable(数据类型为table),roleid(数据类型为int),rolename(数据类型为nvarchar(100));查询CICRole这个表中的RoleId和RoleName字段的值,并将查询到的值赋值给变量rileid和rolename;然后将该值        
      insert到TempTable这个变量中(实际就是向这个表中添加数据);最后返回这个变量TempTable(即返回了一张数据表table)。
    
    
    3.对于TempTable这个变量,它在RETURNS @TempTable table (...)的时候声明,在insert into @TempTable  values(...)时候赋值。
    

    最后:

     记录一个比较有意思但是用处不大的函数:输入一个中文字符串,返回该串的首字母拼音大写,比如输入“五月天”,返回“WYT”。这个函数但就功能来说,蛮腻害,但是据我们的DBA说,对性能影响有点大。
    
    USE [Leading]
    GO
    
    /****** Object:  UserDefinedFunction [dbo].[f_GetPy]    Script Date: 04/12/2017 18:32:34 ******/
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE   function   [dbo].[f_GetPy](@str   nvarchar(4000)) 
    returns   nvarchar(4000) 
    as 
    begin 
    declare   @strlen   int,@re   nvarchar(4000) 
    declare   @t   table(chr   nchar(1)   collate   Chinese_PRC_CI_AS,letter   nchar(1)) 
    insert   into   @t(chr,letter) 
        select   '吖 ', 'A '   union   all   select   '八 ', 'B '   union   all 
        select   '嚓 ', 'C '   union   all   select   '咑 ', 'D '   union   all 
        select   '妸 ', 'E '   union   all   select   '发 ', 'F '   union   all 
        select   '旮 ', 'G '   union   all   select   '铪 ', 'H '   union   all 
        select   '丌 ', 'J '   union   all   select   '咔 ', 'K '   union   all 
        select   '垃 ', 'L '   union   all   select   '嘸 ', 'M '   union   all 
        select   '拏 ', 'N '   union   all   select   '噢 ', 'O '   union   all 
        select   '妑 ', 'P '   union   all   select   '七 ', 'Q '   union   all 
        select   '呥 ', 'R '   union   all   select   '仨 ', 'S '   union   all 
        select   '他 ', 'T '   union   all   select   '屲 ', 'W '   union   all 
        select   '夕 ', 'X '   union   all   select   '丫 ', 'Y '   union   all 
        select   '帀 ', 'Z ' 
        select   @strlen=len(@str),@re= ' ' 
        while   @strlen> 0 
        begin 
            select   top   1   @re=letter+@re,@strlen=@strlen-1 
            from   @t   a   where   chr <=substring(@str,@strlen,1) 
            order   by   chr   desc 
            if   @@rowcount=0 
                select   @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1 
        end 
    return(@re) 
    end
    
    GO
  • 相关阅读:
    js下载doxc 文件示例和部分后缀对应的content-type 总结
    使用react-app-rewired和customize-cra对默认webpack自定义配置
    koa2使用es7 的装饰器decorator
    vue history 模式打包部署在域名的二级目录的配置指南
    linux 安装 node 环境
    javascript 正则表达式之分组与前瞻匹配详解
    vue的$emit 与$on父子组件与兄弟组件的之间通信
    mysql 的基本操作总结--增删改查
    mysql 常用的时间日期函数小结
    小程序封装request请求,统一API
  • 原文地址:https://www.cnblogs.com/Naylor/p/6700702.html
Copyright © 2011-2022 走看看