Sql server存储过程以及一次插入多条记录

首先简单介绍sql server存储过程吧。至于概念含义啥的就不做过多介绍了。它其实和mysql有些类似,语法大同小异。还是用例子说明问题吧。

CREATE PROCEDURE insert_supplier

@supplier_name varchar(30),

@supplier_contact varchar(30),

--------------------------------输入参数

@theoutputstring varchar(50) output

-------------------------------输出参数

AS

BEGIN TRAN

SET XACT_ABORT OFF

-------------------------------事务操作开始与 SET XACT_ABORT ON

-------------------------------和if (@theresult<=0) ROLLBACK TRAN else COMMIT TRAN

-------------------------------构成事务的完整操作

declare @theresult int

set @theresult=1

-------------------------------定义和初始化变量

-------------------------------做一些基本的判定

if (@theresult>=0)

begin

--------------------------------------------------------------------------------------begin

if (@theresult=1)

begin

insert into supplier

(supplier_name,supplier_contact,supplier_phone,supplier_fax,supplier_email,

supplier_postcode,supplier_remark,supplier_address,

supplier_website,supplier_telephone)

values

(@supplier_name,@supplier_contact,@supplier_phone,@supplier_fax,@supplier_email,

@supplier_postcode,@supplier_remark,@supplier_address,

@supplier_website,@supplier_telephone)

if (@@rowcount<>1) set @theresult=-2

end

------------------------------------------------------------------------------------end

end

SET XACT_ABORT ON

if (@theresult<=0) ROLLBACK TRAN else COMMIT TRAN

------------------------------------------------------事务提交或回滚

------------------------------------------------------生成出错信息

if @theresult=0 set @theoutputstring='其他错误'

else

if @theresult=-2 set @theoutputstring='信息修改错误'

else

set @theoutputstring='true'

-----------------------------------------------------输出结果集

select @theoutputstring

GO

根据红色的详细注释,相信大家不难理解这个存储过程的基本语法,其实和mysql是差不多的。因为sql server是支持事务的,所以每个执行都是可以回滚的和mysql的innodb引擎一样。接下来说下如何使用支持插入多条数据的操作,因为sql并没有数组的概念,所以只能用一些字符串拼接并解析的方法来处理。set @prepoint_price = 1set @curpoint_price = charindex('$',@tmp_sale_price)就以这个销售价格为例,如果我想传多条记录中的多个价格,那么可以传之前先把需要传的价格拼接起来,并用一个特殊字符$来间隔。PS:charindex这是定位符号函数,返回第一个此特殊字符的位置。set @sale_price = substring(@tmp_sale_price,@prepoint_price,@curpoint_price-@prepoint_price)然后设置新变量使用substring函数,以上那句就是在tmp_sale_price变量中截取开始到特殊字符为止的变量,这样就可以很轻松获得多条销售价格中的一条。以此类推,就可以不断分析出所有的变量值。最后给出一个简单例子来说明,大家可以其结构,至于业务逻辑可以忽视。

@sale_id varchar(20),

@client_name varchar(128),

@employee_id int,

@transaction_id int,

@warehouse_id varchar(32),

@sale_time datetime,

@sale_num varchar(5),

@sale_totalprice float(8),

@sale_logistics varchar(40),

@sale_logisticscode varchar(40),

@tmp_localitem_id varchar(600),

@tmp_sale_price varchar(600),

@theoutputstring varchar(50) output

AS

BEGIN TRAN

SET XACT_ABORT OFF

declare @theresult int,@curpoint int, @prepoint int,@curpoint_price int, @prepoint_price int, @localitem_id varchar(20),@sale_price varchar(50)

set @theresult=0

if (@theresult>=0)

begin

insert into sale_record(sale_id,client_name,employee_id,transaction_id,warehouse_id,sale_time,sale_num,sale_totalprice,sale_logistics,sale_logisticscode)

values(@sale_id,@client_name,@employee_id,@transaction_id,@warehouse_id,@sale_time,@sale_num,@sale_totalprice,@sale_logistics,@sale_logisticscode)

if (@@rowcount<>1) set @theresult=-2

end

if(@theresult>=0)

begin

set @prepoint = 1

set @curpoint = charindex('$',@tmp_localitem_id)

set @prepoint_price = 1

set @curpoint_price = charindex('$',@tmp_sale_price)

while @curpoint>0 and @theresult>=0

begin

set @localitem_id = substring(@tmp_localitem_id,@prepoint,@curpoint-@prepoint)

set @sale_price = substring(@tmp_sale_price,@prepoint_price,@curpoint_price-@prepoint_price)

insert into sale_detail(localitem_id,transaction_id,sale_id,sale_price)values(@localitem_id,@transaction_id,@sale_id,@sale_price)

if (@@rowcount<>1) set @theresult=-3

if(@theresult>=0)

begin

update storage set localitem_num = '0' where localitem_id = @localitem_id

if (@@rowcount<>1) set @theresult=-4

end

set @prepoint=@curpoint+1

set @prepoint_price=@curpoint_price+1

set @curpoint=charindex('$',@tmp_localitem_id,@prepoint)

set @curpoint_price=charindex('$',@tmp_sale_price,@prepoint_price)

end

end

SET XACT_ABORT ON

set @theresult=1

if (@theresult<=0) ROLLBACK TRAN else COMMIT TRAN

if @theresult=0 set @theoutputstring='其他错误'

else

if @theresult=-2 set @theoutputstring='信息修改错误'

else

if @theresult=-3 set @theoutputstring='查询详细表有误'

else

if @theresult=-2 set @theoutputstring='修改库存有误'

else

if @theresult=1 set @theoutputstring='true'

select @theoutputstring

(0)

相关推荐

  • 一条insert语句批量插入多条记录

    用sql语句插入数据到数据库可能很多人都知道,但如果用一条insert语句批量插入多条记录 到数据库,或许知道的人会少点,下面我们来看下. 操作方法 01 首先我们的测试表是一条数据都没有的. 02 ...

  • 如何向SQL SERVER数据库插入数据

    很多新手朋友并不知道怎么向数据库插入数据,很多朋友还在傻傻的用着黑白窗口敲着长长的代码,你们可以跟过去说拜拜了,下面我来教大家如何简便的向数据库插入数据,和快速的向数据库插入信息. heidisql操 ...

  • win7(windows 7)系统下安装SQL2005(SQL Server 2005)图文教程

    由于工作需要,今天要在电脑上安装SQL Server 2005。以往的项目都是使用Oracle,MS的数据库还真的没怎么用过,安装Oracle已经轻车熟路,但装SQL Server好像还有点小麻烦,所 ...

  • sql server数据库表中插入数据不执行insert语句

    Sql server提供了良好的可视化编辑,不用写insert语句也可以插入数据.以sql server2012为例介绍. 操作方法 01 登陆sql server2012,打开数据库,右击要插入数据 ...

  • SQL SERVER 如何应用存储过程

    运用SQL SERVER的时候,存储过程是我们经常需要使用的功能.它可以帮助我们完成复杂的业务逻辑.那么如何应用SQL SERVER的存储过程呢?下面小编给大家讲解一下. 操作方法 01 首选在SQL ...

  • SQL Server数据类型的介绍

    同时又比它增加了许多更先进的功能具有使用方便可伸缩性好与相关软件集成程度高等优点可跨越从运行Microsoft Windows 98 的膝上型电脑到运行Microsoft Windows 2000 的 ...

  • 【SQL Server】SQL触发器经验详解

    自从上次在中使用了触发器,有读者询问我一些关于触发器的相关信息,个人推荐首先你需要先去把触发器最基础的东西了解清楚,然后通过这次的,希望能在你理解的基础上,加深你对触发器的了解,能够在需要的时候想到触 ...

  • 如何使用SQL Server命令BCP 导入导出EXCEL数据

    sql语句导出sever中的数据到Excel中 操作方法 01 SQL Server BCP 导入导出使用 Bcp 导出导入数据高效,比使用SQL Server Management Stdio 提供 ...

  • SQL SERVER的数据类型

    操作方法 01 1.SQL SERVER的数据类型 数据类弄是数据的一种属性,表示数据所表示信息的类型.任何一种计算机语言都定义了自己的数据类型.当然,不同的程序语言都具有不同的特点,所定义的数据类型 ...