WEB开发网
开发学院数据库MSSQL Server SQL Server 2005数据库中的Output子句 阅读

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),

再举一个例子如下,假如有表如下

 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

上一页  1 2 

Tags:SQL Server 数据库

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接