SQL Server 函數的使用 Function

來源:互聯網
上載者:User

create table student(id varchar2(5) primary key,name varchar2(20) not null,sex char(2) check(sex='男' or sex='女'))--向student中插入一條資料,用函數來驗證插入是否正確create or replace function student_insert_check(f_id in varchar2,f_name in varchar2,sex in varchar2)return varchar2 isc_id number;beginif f_id is null thenreturn('學號不可為空,記錄未成功插入');end if;if f_name is null return ('姓名不可為空,記錄未成功插入');end if;if sex !='男' or sex!='女'return('性別僅限於男或者女,記錄未成功插入')end if;insert into student values(f_id,f_name,sex);commit; -- SQL控制事務之commit命令用法詳解 :COMMIT命令用於把事務所做的修改儲存到資料庫,它把上一個COMMIT或ROLLBACK命令之後的全部事務都儲存到資料庫。return('記錄插入成功');end dd;================================================================================= create function average1(@cnum char(20)) --建立一個帶參數的函數 returns int --傳回值為一個int類型的整數 as begin declare @aver int  --聲明一個@aver變數 select @aver=    --查詢這個變數 (    --給變數賦值select AVG(成績) from xs_kc where 課程號=@cnumgroup by 課程號 ) return @aver  --返回變數 end go  select dbo.average1(101) --掉用剛剛建立的函數average1

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.