SQLServer (1): Example

Source: Internet
Author: User
SQLServer (1): Example 1. Write a program: output the prime number between 1 and save it to a new prime number table (serial number, prime number. Createtable prime number table (No. intidentity (), prime number int) declare @ iint, @ jint, @ flagbit -- @ I External Loop Variable, @ j inner loop variable, @ flag indicates whether the flag of the prime number is used

SQL Server (1): Example 1. Write a program: output the prime number between 1 and save it to a new prime number table (serial number, prime number. Create table prime number table (No. int identity (), prime number int) declare @ I int, @ j int, @ flag bit -- @ I external cyclic variable, @ j inner cyclic variable, @ flag indicates whether the flag of the prime number is used

SQL Server (1): Example

1. Write a program to output the prime numbers between 1 and save them to a new prime number table (sequence number, prime number.
Create table prime number table (No. int identity (1, 1), prime number int)
Declare @ I int, @ j int, @ flag bit -- @ I external cyclic variable, @ j inner cyclic variable, @ flag indicates whether the flag is a prime number
Set @ I = 2
While @ I <= 1000 -- from 2 to 1000, check the number to see if it is exponential
Begin
Set @ flag = 1
Set @ j = 2
While @ j <= SQRT (@ I) -- from 2 to the square root, check whether the Division is complete.
Begin
If @ I % @ j = 0
Begin
Set @ flag = 0
Break -- ① you can exit the internal loop if it is not a prime number.
End
Set @ j = @ j + 1
End
If @ flag = 1
Insert into prime number table (prime number) values (@ I)
Set @ I = @ I + 1
End -- ② pay attention to indentation and alignment !!! For example, the begin and end of each loop.

2. Compile a function: the Hong Kong Space should be reversed and the first letter of the word should be output in uppercase. For example, enter I am Frank. Output knarn Ma I.
Create function reverse text (@ string varchar (5000 ))
Returns varchar (5000) -- parameter and return type
As
Begin
Declare @ I int, @ flag bit -- @ flag is a case-sensitive flag
Declare @ reverse varchar (5000), @ result varchar (5000)
Set @ I = 1 -- first loop, first implement reverse configuration, and save the result to @ reverse
Set @ reverse =''
While (@ I <= LEN (@ String ))
Begin
Set @ reverse = SubString (@ string, @ I, 1) + @ reverse
Set @ I = @ I + 1
End
Set @ I = 1 -- the second loop, website space, and the first case, server space. The result is saved to @ result.
Set @ flag = 1
Set @ result =''
While (@ I <= LEN (@ reverse ))
Begin
If @ flag = 1
Set @ result = @ result + Upper (SubString (@ reverse, @ I, 1 ))
Else
Set @ result = @ result + Lower (SubString (@ reverse, @ I, 1 ))
If SubString (@ reverse, @ I, 1) in ('','.')
Set @ flag = 1
Else
Set @ flag = 0
Set @ I = @ I + 1
End
Return (@ result)
End

Posted on

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.