SQL server 存储过程的建立和调用

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
简介: SQL server 存储过程的建立和调用存储过程的建立和调用--1.1准备测试需要的数据库:test,数据表:物料表,采购表if not exists (select * from master.dbo.

SQL server 存储过程的建立和调用
存储过程的建立和调用
--1.1准备测试需要的数据库:test,数据表:物料表,采购表
if not exists (select * from master.dbo.sysdatabases where name='test')
create database test
go
use test
go

if object_id('test..物料表') is null
begin
create table 物料表(编号 varchar(6),名称 varchar(40),类别 varchar(20))

insert into 物料表
select 'A00001','46寸电视机','电视机' union all
select 'A00002','52寸电视机','电视机' union all
select 'A00003','60寸电视机','电视机' union all
select 'A00004','39寸电视机','电视机' union all
select 'A00005','16升洗衣机','洗衣机' union all
select 'A00007','美的1匹空调','空调' union all
select 'A00008','格力1匹空调','空调'

select * from 物料表
end

if object_id('test..采购表') is null
begin
create table 采购表(编号 varchar(6),名称 varchar(40),单价 numeric(10,2),数量 int,小计 numeric(10,2),日期 datetime)

insert into 采购表
select 'A00001','46寸电视机',5000.00,10,5000*10,'2016-10-01' union all
select 'A00002','52寸电视机',5500.00,20,5500*20,'2016-10-02' union all
select 'A00003','60寸电视机',6500.00,10,6500*10,'2016-10-03' union all
select 'A00004','39寸电视机',3000.00,10,3000*10,'2016-10-04' union all
select 'A00005','16升洗衣机',1500.00,10,1500*10,'2016-10-04' union all
select 'A00007','美的1匹空调',2500.00,20,2500*20,'2016-10-05' union all
select 'A00008','格力1匹空调',2800.00,10,2800*10,'2016-10-05'

select * from 采购表
end

--1.2 建立1个修改物料表名称的存储过程,同时更新采购表名称(注意:建立存储过程的语句,要单独分开执行,即不能和上面建立测试环境的语句在一起)
create proc dbo.update_wl(@bh varchar(6),@mc varchar(40),@ut int output)
as
begin
declare @rs int
set @ut=1
begin transaction

update 物料表 set 名称=@mc where 编号=@bh
set @ut=@@rowcount
if @ut> 0 --@@rowcount为系统变量,影响行数,大于0表示更新成功,同步采购表
begin
  if exists(select 编号 from 采购表 where 编号=@bh) --如果采购表存在改编号记录,同步
  begin   
    update 采购表 set 名称=@mc where 编号=@bh
    set @ut=@@rowcount  --如果同步成功,必定返回大于0值
  end
end

if @ut > 0 
 commit
else
 rollback transaction

end

--1.3 在SQL2000 中的调用方法
declare @bh varchar(6),@mc varchar(40),@ut int
set @bh='a00002'
set @mc='49寸电视机'

exec update_wl @bh,@mc,@ut output
select @ut
select * from 物料表
select * from 采购表

--只增加1个物料,采购没数据
--insert into 物料表
--select 'A00009','美的2匹空调','空调'

set @bh='a00009'
set @mc='美的2匹空调'

exec update_wl @bh,@mc,@ut output
select @ut
select * from 物料表
select * from 采购表

dbo.update_wl(@bh varchar(6),@mc varchar(40),@ut int output)
这个存储过程,是带参数返回值的,如果返回值大于0,表示更新成功。
相当执行了2条命令
update 物料表 set 名称=@mc where 编号=@bh
update 采购表 set 名称=@mc where 编号=@bh
但用存储过程,使用了事务,当2条语句都成功执行时,才都执行,要是第2条,没成功执行时,会回滚

**在VFP中调用
local bh,mc,ut
bh='A00002'
mc='49寸电视机'
ut=0
sqlexec(句柄,'exec update_wl ?bh,?mc,?@ut')
?ut &&查看返回值,0为没更新,大于0为更新

-- SQL2000中调用,'B00002'是物料表中没有的,此时,返回值 0
declare @bh varchar(6),@mc varchar(40),@ut int
set @bh='B00002'
set @mc='49寸电视机'

exec update_wl @bh,@mc,@ut output
select @ut
select * from 物料表
select * from 采购表

-- 1.4 存在过程的修改,在SQL2000中进行,把 create 改为 alter 就可以。
alter proc dbo.update_wl(@bh varchar(6),@mc varchar(40),@ut int output)
as
begin
declare @rs int --此行多余
set @ut=1
begin transaction

 update 物料表 set 名称=@mc where 编号=@bh
 set @ut=@@rowcount
 if @ut> 0 --@@rowcount为系统变量,影响行数,大于0表示更新成功,同步采购表
begin
   if exists(select 编号 from 采购表 where 编号=@bh) --如果采购表存在改编号记录,同步
  begin   
     update 采购表 set 名称=@mc where 编号=@bh
     set @ut=@@rowcount  --如果同步成功,必定返回大于0值
  end
 end

 if @ut > 0 
  commit
 else
  rollback transaction

end

** 1.5 在VFP中,创建SQL2000的存储过程
TEXT TO lcSqlStr TEXTMERGE NOSHOW PRETEXT 4
create proc dbo.update_wl(@bh varchar(6),@mc varchar(40),@ut int output)
as
begin
declare @rs int --此行多余
set @ut=1

begin transaction
  update 物料表 set 名称=@mc where 编号=@bh
  set @ut=@@rowcount
  if @ut> 0 --@@rowcount为系统变量,影响行数,大于0表示更新成功,同步采购表
begin
    if exists(select 编号 from 采购表 where 编号=@bh) --如果采购表存在改编号记录,同步
  begin   
      update 采购表 set 名称=@mc where 编号=@bh
      set @ut=@@rowcount  --如果同步成功,必定返回大于0值
  end
  end

  if @ut > 0 
   commit
  else
   rollback transaction

end
ENDTEXT
?SQLEXEC(句柄,lcSqlStr)
**就这么简单

LOCAL lcSql,lcServer,lcUid,lcPwd,lcPwd,lnHandle

lcServer = "atm8505"
lcUid = "sa"
lcPwd = "123456"
lcDbs = "test"
**把上面的参数,改为你自己的

lcSql=[driver=sql server;server=] + lcServer + [;uid=] + lcUid + [;pwd=] + lcPwd + [;database=] + lcDbs
lnHandle=sqlstringconnect(lcSql)

LOCAL bh,mc,ut
bh='A00002'
mc='49寸电视机'
ut=0

IF lnHandle > 0
?ut && 输出 0
SQLEXEC(lnHandle,'exec update_wl ?bh,?mc,?@ut')
?ut && 更新成功,输出大于1
ENDIF

*还有1种调用,用 call

IF lnHandle > 0
?ut && 输出 0
SQLEXEC(lnHandle,"{call update_wl(?bh,?mc,?@ut)}")
?ut && 更新成功,输出大于1
ENDIF

原文地址https://www.cnblogs.com/liu224/p/10736111.html

相关实践学习
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
相关文章
|
20天前
|
SQL IDE Java
Java连接SQL Server数据库的详细操作流程
Java连接SQL Server数据库的详细操作流程
|
28天前
|
SQL DataWorks NoSQL
DataWorks产品使用合集之如何将SQL Server中的数据转存到MongoDB
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
251 1
|
4天前
|
SQL 存储 关系型数据库
关系型数据库SQL Server学习
【7月更文挑战第4天】
12 2
|
8天前
|
SQL 存储 测试技术
|
7天前
|
SQL 机器学习/深度学习 搜索推荐
SQL SERVER 转换失败
【6月更文挑战第25天】
|
12天前
|
SQL 关系型数据库 分布式数据库
PolarDB产品使用问题之如何迁移SQL Server
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
11天前
|
SQL 存储 机器人
SQL Server 中 RAISERROR 的用法详解
SQL Server 中 RAISERROR 的用法详解
|
21天前
|
SQL 存储 关系型数据库
关系型数据库中的SQL Server
【6月更文挑战第11天】
52 3
|
20天前
|
SQL IDE Java
Java连接SQL Server数据库的详细操作流程
Java连接SQL Server数据库的详细操作流程
|
1天前
|
SQL 存储 数据库
SQL Server性能优化策略与实践
在使用Microsoft SQL Server(简称MSSQL)进行数据库管理时,性能优化是确保系统高效运行、提升用户体验的关键环节