mysql stored procedures with return values
2012-08-08 17:27
330 查看
存储过程的功能非常强大,在某种程度上甚至可以替代业务逻辑层,
接下来就一个小例子来说明,用存储过程插入或更新语句。
1、数据库表结构
所用数据库为Sql Server2008。
2、创建存储过程
(1)实现功能:1)有相同的数据,直接返回(返回值:0);
2)有主键相同,但是数据不同的数据,进行更新处理(返回值:2);
3)没有数据,进行插入数据处理(返回值:1)。
根据不同的情况设置存储过程的返回值,调用存储过程的时候,根据不同的返回值,进行相关的处理。
(2)下面编码只是实现的基本的功能,具体的Sql代码如下:
Create proc sp_Insert_Student
@No char(10),
@Name varchar(20),
@Sex char(2),
@Age int,
@rtn int output
as
declare
@tmpName varchar(20),
@tmpSex char(2),
@tmpAge int
if exists(select * from Student where No=@No)
begin
select @tmpName=Name,@tmpSex=Sex,@tmpAge=Age from Student where No=@No
if ((@tmpName=@Name) and (@tmpSex=@Sex) and (@tmpAge=@Age))
begin
set @rtn=0 --有相同的数据,直接返回值
end
else
begin
update Student set Name=@Name,Sex=@Sex,Age=@Age where No=@No
set @rtn=2 --有主键相同的数据,进行更新处理
end
end
else
begin
insert into Student values(@No,@Name,@Sex,@Age)
set @rtn=1 --没有相同的数据,进行插入处理
end
execute:
declare @rtn int
exec sp_Insert_Student '1101','张三','男',23,@rtn output
if @rtn=0
print '已经存在相同的。'
else if @rtn=1
print '插入成功。'
else
print '更新成功'
接下来就一个小例子来说明,用存储过程插入或更新语句。
1、数据库表结构
所用数据库为Sql Server2008。
2、创建存储过程
(1)实现功能:1)有相同的数据,直接返回(返回值:0);
2)有主键相同,但是数据不同的数据,进行更新处理(返回值:2);
3)没有数据,进行插入数据处理(返回值:1)。
根据不同的情况设置存储过程的返回值,调用存储过程的时候,根据不同的返回值,进行相关的处理。
(2)下面编码只是实现的基本的功能,具体的Sql代码如下:
Create proc sp_Insert_Student
@No char(10),
@Name varchar(20),
@Sex char(2),
@Age int,
@rtn int output
as
declare
@tmpName varchar(20),
@tmpSex char(2),
@tmpAge int
if exists(select * from Student where No=@No)
begin
select @tmpName=Name,@tmpSex=Sex,@tmpAge=Age from Student where No=@No
if ((@tmpName=@Name) and (@tmpSex=@Sex) and (@tmpAge=@Age))
begin
set @rtn=0 --有相同的数据,直接返回值
end
else
begin
update Student set Name=@Name,Sex=@Sex,Age=@Age where No=@No
set @rtn=2 --有主键相同的数据,进行更新处理
end
end
else
begin
insert into Student values(@No,@Name,@Sex,@Age)
set @rtn=1 --没有相同的数据,进行插入处理
end
execute:
declare @rtn int
exec sp_Insert_Student '1101','张三','男',23,@rtn output
if @rtn=0
print '已经存在相同的。'
else if @rtn=1
print '插入成功。'
else
print '更新成功'
相关文章推荐
- mysql stored procedures with return values
- mysql stored procedures with return values
- 转贴 Using MySQL Stored Procedures with PHP mysql/mysqli/pdo
- Getting Started with MySQL Stored Procedures
- MySQL stored procedures with Ruby
- [转]How to get return values and output values from a stored procedure with EF Core?
- Stored Procedures with Multiple Result Sets
- QMetaMethod::invoke: Unable to invoke methods with return values in queued connections
- Working C# code for MySql5.5 Stored Procedures IN parameters
- Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python
- Using DataAdapters and stored procedures with Sybase and ODBC.NET
- Using NHibernate With Stored Procedures
- Stored procedures versioning with SQL Server and Visual SourceSafe
- MySQL Working with NULL Values
- Introduction to MySQL Stored Procedures
- MySQL Error Handling in Stored Procedures
- Snippet: Fetching results after calling stored procedures using MySQL Connector/Python
- The return types for the following stored procedures could not be detected
- Oracle query - how to make count to return values with 0
- sql: MySQL and Microsoft SQL Server Stored Procedures IN, OUT using csharp code