Create and call SQL server storage functions

Source: Internet
Author: User



1. scalar functions

  1) Create

Create Function fun_max (@ x int, @ y int) -- scalar function returns int asbegin if @ x <@ Y set @ x = @ Y return @ xenddeclare @ x int, @ Y int set @ x = 1 Set @ Y = 2 print 'max = '+ Cast (DBO. fun_max (@ X, @ Y) as char)

Create Function fun_max (@ x int, @ y int) -- scalar function

Returns int

As

Begin

If @ x <@ Y

Set @ x = @ Y

Return @ x

End

  2) Call
Declare @ x int, @ Y int

Set @ x = 1

Set @ Y = 2

Print 'max = '+ Cast (DBO. fun_max (@ X, @ Y) as char)

(Note: SQL statements cannot appear)

2. Table value functions

  1) Create

 

Create Function fun_findid (@ id int)

Returns table

As

Return select * From renter where renterid> @ ID

  2) Call

Select * From fun_findid (1001)

(Note: variables cannot be defined)

 

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.