Application of T-SQL syntax structure

Source: Internet
Author: User

T-SQL
1. Arithmetic Operations, logical operations, comparison operations, bit operations, string connection operations, etc.
2. Program Structure
Sequence Structure, select structure, and Cycle Structure
I. Classic applications with a structure selected
-- Define three variables
Declare @ hyuser varchar (50), @ hypwd varchar (50), @ MSG varchar (50)
-- Query the value of a Variable
Select @ hyuser = 'liping', @ hypwd = '000000'
-- Determine whether the values of the following variables are equal to the values of the queried variables.
If @ hyuser = 'hystu1'
Begin
If @ hypwd = '20140901'
Set @ MSG = 'logon successful'
Else
Set @ MSG = 'incorrect password'
End
Else if @ hyuser = 'hystu2'
Begin
If @ hypwd = '20140901'
Set @ MSG = 'logon successful'
Else
Set @ MSG = 'input error'
End
Else if @ hyuser = 'hystu3'
Begin
If @ hypwd = '20140901'
Set @ MSG = 'logon successful'
Else
Set @ MSG = 'logon failed'
End
Else
Set @ MSG = 'user name is incorrect. Please log on again'
Print @ msg
Go

==================

Determine whether the user name exists, whether the user password is correct, and whether the user is successfully logged on to the system with a prompt
------- Create a table in the database
Use master
Create Table hyuser (
Hyid int identity (1, 1) primary key,
Hyname varchar (50) unique,
Hypwd varchar (50)
)

-------- insert a record to the table
insert into hyuser (hyname, hypwd) values ('stu1', '000000')
insert into hyuser (hyname, hypwd) values ('stu2', '123')
insert into hyuser (hyname, hypwd) values ('stu3', '123 ')
---------- program writing
declare @ hyuser varchar (50), @ hypwd varchar (50), @ MSG varchar (50), @ num int,
@ num1 int
select @ hyuser = 'stu1', @ hypwd = '000000'
select @ num = count (*) from hyuser where hynam E = @ hyuser
If @ num> = 1
begin
select @ num1 = count (*) from hyuser where hyname = @ hyuser and hypwd = @ hypwd
If @ num1> = 1
set @ MSG = 'login successful! '
else
set @ MSG =' the password is incorrect. Please enter it again! '
end
else
set @ MSG =' the user name is incorrect. Please enter it again! '
Print @ MSG
go

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.