博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用存储过程实现获取字符串中的数据添加到列中
阅读量:4322 次
发布时间:2019-06-06

本文共 2645 字,大约阅读时间需要 8 分钟。

create table goods(gid int identity(1,1) not null primary key,gname nvarchar(20))goalter proc splitstring@goodlist varchar(1000),   ----字符串@spilststring varchar(10)=','  ----分割符asbegin    declare @end int,@start int,@values char(20)    ----@end 分割符位置,@start开始截取位置,@values截取的信息    set @start=1    set @end=charindex(@spilststring,@goodlist,@start)   ----获得分割符的位置    while(@start

 

 

or

分解多个字符串

分隔字符串存储过程 CutStringcreate proc CutString (@sourcestring varchar(100) output,   ----输入的字符串                       @outstring varchar(10) output)       ----输出的字符串asdeclare @position int set     @position = charindex(',',@sourcestring)            ----分割符的位置if (@position = 0)                                          ----说明后面没有数据了,这是最后一个  begin     set @outstring = @sourcestring     set @sourcestring = null  endelse  begin     set @outstring = substring(@sourcestring,1,@position-1)     set @sourcestring = substring(@sourcestring,@position+1,(len(@sourcestring)-@position))  end ps:截取字符串‘aa,bb,cc,dd’   第一次: 截取字符串‘aa,bb,cc,dd’  输出  aa   第二次: 截取字符串‘bb,cc,dd’  输出 bb   第三次: 截取字符串'cc,dd'  输出 cc   第四次: 截取字符串‘dd’  输出 dd

使用上面的方法:

 

将多个字符串添加到表中

 

order表:

orderid(订购id)     
customerid(顾客id)
employeeid(服务员id)
Order Details表:
customerid(顾客id)
productid(编号)
unitprice(单价)
quantity(数量)
discount(折扣)
Create proc SubmitOrder (          @customerid    varchar(5),      @employeeid    int,      @productidlist varchar(100),       @unitpricelist varchar(100),          @quantitylist  varchar(100),          @discountlist  varchar(100))asset xact_abort on        --开启自动事务会滚,出任何错误都自动回滚   declare @orderid int   declare @productid varchar(20)   declare @unitprice varchar(20)   declare @quantity  varchar(20)   declare @discount  varchar(20)   begin transaction    --使用事务来执行     insert into Orders(CustomerID,EmployeeID) values(@customerid,@employeeid)     --select top 1 OrderID from Orders order by OrderID desc     select @orderid = @@identity          --@@identity取得刚刚完成插入的全局自动增长列的值     while (  @productidlist is not null )        begin           exec  CutString @productidlist output,@productid output           exec  CutString @unitpricelist output,@unitprice output           exec  CutString @quantitylist  output,@quantity  output           exec  CutString @discountlist  output,@discount  output           insert into [Order Details] values(                        @orderid,                        convert(int,@productid),                        convert(money,@unitprice),                        convert(int,@quantity),                        convert(real,@discount))           --delete        end    Commit transaction

 

转载于:https://www.cnblogs.com/ang-664455/p/7118571.html

你可能感兴趣的文章
(转)arguments.callee移除AS3匿名函数的侦听
查看>>
onNewIntent调用时机
查看>>
MYSQL GTID使用运维介绍(转)
查看>>
04代理,迭代器
查看>>
解决Nginx+PHP-FPM出现502(Bad Gateway)错误问题
查看>>
Java 虚拟机:互斥同步、锁优化及synchronized和volatile
查看>>
2.python的基本数据类型
查看>>
python学习笔记-day10-01-【 类的扩展: 重写父类,新式类与经典的区别】
查看>>
查看端口被占用情况
查看>>
浅谈css(块级元素、行级元素、盒子模型)
查看>>
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
PHP开源搜索引擎
查看>>
12-FileZilla-响应:550 Permission denied
查看>>
ASP.NET MVC 3 扩展生成 HTML 的 Input 元素
查看>>
LeetCode 234. Palindrome Linked List
查看>>
编译HBase1.0.0-cdh5.4.2版本
查看>>
结构体指针
查看>>
迭代器
查看>>
Food HDU - 4292 (结点容量 拆点) Dinic
查看>>
Ubuntu安装Sun JDK及如何设置默认java JDK
查看>>