Use function for SQL Server functions

Source: Internet
Author: User

CREATE TABLE student (ID VARCHAR2 (5) Primary key,name VARCHAR2 (+), not null,sex char (2) Check (sex= ' male ' or sex= ' female ')--to Stude NT inserts a piece of data, using a function to verify that the insert is correct create or replace function (f_id in Varchar2,f_name in Varchar2,sex in varchar2) return VARCHAR2 isc_ ID number;beginif f_id is null thenreturn (' number cannot be empty, record is not successfully inserted '), end if;if f_name is null return (' name cannot be empty, record is not successfully inserted '); end if;if S Ex! = ' Male ' or sex!= ' female ' return (' gender limited to male or female, record not successfully inserted ') end If;insert into student values (f_id,f_name,sex); commit; --The commit command of the SQL control transaction is explained in detail: The commit command is used to save the changes made by the transaction to the database, and it saves all the transactions after the last commit or ROLLBACK command to the database. Return (' record inserted successfully '); end dd;================================================================================= Create function Average1 (@cnum char (20))--Creates an argument returns INT--an integer with a return value of type int as BEGIN declare @aver INT--Declares a @aver variable Sele CT @aver =---Query this variable (--assign value to a variable select AVG (score) from XS_KC where course number [email Protected]group by course number) return @aver--return variable End Go Select Dbo.average1 (101)--Drop the function you just created Average1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.