zoukankan      html  css  js  c++  java
  • 存储过程

    有b_jihua_xiangmu和b_jihua_jsgm两张表,他们都有一个相同的xmbm(编号) 都是从1~N,然后他们又有相同的一列f_cr_jsgm,
    b_jihua_xiangmu这张表中f_cr_jsgm中有1~50条数据,  b_jihua_jsgm这张表有100条数据,但是第1~50条为空,把xiangmu表中的第1~50条update到
    jsgm表中 用到如下存储过程 还没有看懂额.....
    create proc pro_abc
    as
    declare @p1 nvarchar(50),@p2 int;
    declare my_cursor cursor scroll dynamic 
    for SELECT F_CR_JSGM,f_nb_xmbm FROM B_JIHUA_XIANGMU WHERE NOT  F_CR_JSGM IS NULL 
    	open my_cursor 
    	fetch next from my_cursor into @p1,@p2
    	while(@@fetch_status=0)
    	  begin
    		update b_jihua_jsgm set f_cr_jsgm2=@p1 where f_nb_xmbm=@p2
    		fetch next from my_cursor into @p1,@p2
    	  end
    	close my_cursor
    	deallocate my_cursor
    
    又在网上发现如下存储过程
    这个原本不完全的,被我一次次实验搞好了,嘿嘿 这个是给text表中插入10000条数据,
    create proc pro_5
    as
    declare   @i  int, @name nvarchar(50)
    set   @i=0 
    set   @name=''
    while   @i <10000 
    begin      
    	set   @i=@i+1 
    	set	  @name='name'+cast(@i as nvarchar(50)) 
    	insert into  test(lid,lname) values(@i,@name)
    end
    
    但是呢会写不知道原理啊,然后看了下资料:http://www.blueidea.com/tech/program/2006/3972_3.asp
    这个资料好啊   简单明了,办事不少 呵呵
    最后说下其中的艰辛历史啊 血泪啊 ,呵呵
    注意:声明变量、sql命令要在set的下方
    根据我出现的问题,因为上班的时候没有整理,所以只能给大家结果看了,遇到问题就是begin里边写错了,造成@i一直=0所以大家懂得
    正运行呢提示 事务已满, google下 在csdn上边 看到个好东西,就在底下 先备份数据库下 然后截断事务日志 然后压缩日志
    要不然你在增删改查就操作不了啦
    exec pro_5
    --执行存储过程
    drop proc pro_5
    --删除存储过程
    backup   log   jihua   with   no_log 
    go 
    --截断事务日志
    dbcc   shrinkdatabase(jihua) 
    go 
    --压缩日志
    
    看来自己动手,兴趣满满啊
    ------------------------------------------------------------------
    --得到.之前字母的个数
    select charindex('.','abc.doc')
    --得到.之前的字符串
    select substring('abc.doc',0,charindex('.','abc.doc'))
    --在这个存储过程中使用
    declare @temp varchar(50);
    select @temp=substring('abc.doc',0,charindex('.','abc.doc'))
    ------------------------------------------------------------------
    --一个表中有doc和xls两列 它们的前缀应该相同 比如 a.doc  a.xls 如果不同 下面的方法可以修改的
    create proc proc_jihua
    as
    declare @F_BY_IMGNAME   varchar(MAX),@F_BY_CIMGNAME VARCHAR(MAX);
    declare my_cursor cursor scroll dynamic 
    for select substring(F_BY_IMGNAME,0,charindex('.',F_BY_IMGNAME)),F_BY_CIMGNAME from b_jihua_jihua where F_BY_CIMGNAME!=''
    open my_cursor
    fetch  next from my_cursor into @F_BY_IMGNAME,@F_BY_CIMGNAME
    while(@@fetch_status=0)
    begin
    	update b_jihua_jihua set  F_BY_CIMGNAME=@F_BY_IMGNAME+'.xls' where F_BY_IMGNAME=@F_BY_IMGNAME+'.doc' or F_BY_IMGNAME=@F_BY_IMGNAME+'docx'
    	fetch next from my_cursor into @F_BY_IMGNAME,@F_BY_CIMGNAME
    end 
    close my_cursor
    deallocate my_cursor
    --go--这里加上go的话 底下exec就错咧..找不到存储过程  如果没有go这个存储过程只能使用一次的呦
    drop proc  proc_jihua
    go
    --go是接着执行下边的sql语句 没有go 就到这里截止了,底下就不执行了得你重新执行底下的sql语句--
    exec proc_jihua
    ------------------------------------------------------------
    
    --------------------------------------------------------
    --split函数
    Create FUNCTION [dbo].[Split]
     (@origStr varchar(8000),  --待拆分的字符串
      @markStr varchar(100))   --拆分标记,如','
     RETURNS @splittable table
     (
     id   varchar(4000) NOT NULL, --编号ID
     item   varchar(2000) NOT NULL  --拆分后的字符串
     )
    AS 
    BEGIN
     DECLARE @strlen int,@postion int,@start int,@sublen int,@TEMPstr varchar(200),@TEMPid int
     SELECT @strlen=LEN(@origStr),@start=1,@sublen=0,@postion=1,@TEMPstr='',@TEMPid=0
     
     if(RIGHT(@origStr,1)<>@markStr )
     BEGIN
      SET @origStr = @origStr + @markStr
     END
     WHILE((@postion<=@strlen) and (@postion !=0))
      BEGIN
       IF(CHARINDEX(@markStr,@origStr,@postion)!=0)
        BEGIN
         SET @sublen=CHARINDEX(@markStr,@origStr,@postion)-@postion; 
        END
       ELSE
        BEGIN
         SET @sublen=@strlen-@postion+1;
        END
       IF(@postion<=@strlen)
        BEGIN
         SET @TEMPid=@TEMPid+1;
         SET @TEMPstr=SUBSTRING(@origStr,@postion,@sublen);
         INSERT INTO @splittable(id, item) values(@TEMPid,@TEMPstr)
         IF(CHARINDEX(@markStr,@origStr,@postion)!=0)
          BEGIN
           SET @postion=CHARINDEX(@markStr,@origStr,@postion)+1
          END
         ELSE
          BEGIN
           SET @postion=@postion+1
          END
        END
      END
     RETURN
    END
    go
    --使用函数
    select * from Split('2005-05-05','-')
    go
    --删除函数
    drop function split
    
  • 相关阅读:
    一个基于C++11的定时器队列(timerfd,poll实现)
    Mysql学习(一)添加一个新的用户并用golang操作Mysql
    epoll使用详解
    基于C++11实现线程池的工作原理
    ubuntu18.04初始化配置
    muduo网络库学习笔记(五) 链接器Connector与监听器Acceptor
    muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制
    muduo网络库学习笔记(三)TimerQueue定时器队列
    关于 JavaScript 的 null 和 undefined,判断 null 的真实类型
    vue 双向数据绑定原理
  • 原文地址:https://www.cnblogs.com/0banana0/p/2065199.html
Copyright © 2011-2022 走看看