SQL Server 2005数据库中的Output子句
2008-09-04 10:02:10 来源:WEB开发网再举一个例子如下,假如有表如下
create table Employee(EmpID int identity(1, 1) constraint Employee_pk primary key, FirstName varchar(100), LastName Varchar(100), Sex char(1), Salutation as case when Sex =’M’ then ’Mr.’ when Sex =’F’ then ’Ms.’ else ’’ end, Salary Money, Deductions as case When Salary <3000 then .20 * Salarywhen Salary between 30000 and 50000 then .26*Salary when Salary between 50001 and 75000 then .30*Salaryelse .35*Salary end, DOJ datetime constraint DOJ_DEF default getdate())
很容易的一个表,其中用到了case子句,分别对Salutation ,deductions,doj等进行了设置。而我们可以再搞一个存储过程,如下:
create procedure employee_insert@Fname varchar(100),@Lname varchar(100),@sex char(1),@Salary Moneyasdeclare @empoutput table (EmpID int ,FirstName varchar(100), LastName Varchar(100),Sex char(1), Salutation char(3),Salary Money,Deductions Money,DOJ datetime )insert into Employee(FirstName,LastName,Salary,sex)OUTPUT inserted.* into @empoutputvalues(@Fname ,@Lname ,@Salary,@sex )select * from @empoutput
然后在调用该存储过程时,可以这样来用USE [MyDatabase]
GOexec employee_insert ’Miho’,’Yoshikawa’,’F’,146000
这样,将在执行的时候,将往employee表里插入数据,并且,将定义了一个临时的变量empoutput,注意哦,这个可以以表的形式定义的,这里很容易看的出来,并且,将插入到employee表的数据同时输出到这个empoutput变量中去.我们执行这个存储过程后,会发现如下结果
EmpID,FirstName,LastName,Sex,Salutation,Salary,Deductions,DOJ
7,Miho,Yoshikawa,F,Ms.,146000.00,51100.00,2006-04-09 23:16:44.920
,并且employee表中的确插入了记录
-------------------------------------------------------------------------------------
调用带有Output参数的存储过程.txt
create PROCEDURE SP_RealTimeDisplay
(
@FromEmp nvarchar(20),
@FromDep nvarchar(20),
@i1 nvarchar(20) output,
@i2 nvarchar(20) output,
@i3 nvarchar(20) output
)
AS
set nocount on
--公司批复
select @i1=(select count(FsignID) as Sum1 From T_MessageSignature where FSignID=@FromEmp and FLastSignID=1 and FSignRemark is null)
--会签意见
select @i2=(select count(FsignID) as Sum1 From T_MessageSignature where FSignID=@FromEmp and FLastSignID=0 and FSignRemark is null)
--考勤审批
select @i3=(select count(FKaoQinID) as Sum1 From T_KaoQin where FDepID like @FromDep and FCheckerID=0)
GO
--调用,先定义再输出
DECLARE @a nvarchar(20)
DECLARE @b nvarchar(20)
DECLARE @c nvarchar(20)
execute SP_RealTimeDisplay ’106000003’,’012708%’,@a output,@b output,@c output
select @a as aa, @b as bb,@c as cc
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
- ››SQL SERVER无法安装成功,sqlstp.log文件提示[未发...
- ››Sql Server中通过父记录查找出所有关联的子记录
- ››SqlServer触发器、存储过程和函数
- ››SQL Server 中的事务(含义,属性,管理)
- ››Sqlite数据库插入和读取图片数据
- ››Sql server 2005拒绝了对对象 'xx表' (数...
- ››Sql server 2005拒绝了对对象 'xx表' (数...
更多精彩
赞助商链接