SQL code used to create tables, stored procedures, triggers, and scalar functions

Source: Internet
Author: User

-- Create a customer information table
Create Table custominfo
(
CID nvarchar (20) not null primary key,
Cpassword nvarchar (30) not null check (LEN (cpassword)> = 6 ),
Csum int not null default (0)
)
Go

Insert into custominfo (CID, cpassword, csum)
Values ('zhang san', 'zhangsan', 0)
Go
Insert into custominfo (CID, cpassword, csum)
Values ('Li si', 'lisi123 ', 0)
Go
Insert into custominfo (CID, cpassword, csum)
Values ('wang wu', 'wangwu', 0)
Go

-- Create a purchase record table
Create Table buynote
(
Buyannal datetime not null,
CID nvarchar (20) not null foreign key references custominfo (CID ),
PID nvarchar (10) Not null foreign key references product (productid ),
[Count] int not null default (1)
)
Go

-- Create a point level table
Create Table integrallevel
(
Startcount int not null primary key,
[Name] nvarchar (10) Not null
)
Go

Insert into integrallevel (startcount, [name])
Values (0, 'common card users ')
Go
Insert into integrallevel (startcount, [name])
Values (10, 'Copper card users ')
Go
Insert into integrallevel (startcount, [name])
Values (20, 'Silver card users ')
Go
Insert into integrallevel (startcount, [name])
Values (40, 'Card users ')
Go
Insert into integrallevel (startcount, [name])
Values (60, 'vip card users ')
Go

-- Create a trigger
Create trigger buyfoodtrigger
On buynote
For insert
As
Begin
Declare @ CID nvarchar (20), @ count int
Select @ cid = CID, @ COUNT = [count] From inserted
Update custominfo set csum = csum + @ count where cid = @ CID
Print 'Thank you for your purchase. Your points have been recorded! '
End
Go

-- Create an SP for shopping
Create procedure buyfood
@ CID nvarchar (20 ),
@ PID nvarchar (10 ),
@ Count int
As
Begin

Insert into buynote (buyannal, CID, PID, [count]) values (getdate (), @ CID, @ PID, @ count)
Print 'Congratulations! '
End
Go

Execute buyfood 'zhang san', 'zhangsan', 5
Go

-- Create a general customer information table
Create Table totalcustom
(
CID nvarchar (20) not null primary key,
Cpassword nvarchar (30) not null check (LEN (cpassword)> = 6 ),
Csum int not null default (0)
)
Go
Insert into totalcustom (CID, cpassword, csum)
Values ('zhang sanfeng', 'zhangsanfeng', 0)
Go
Insert into totalcustom (CID, cpassword, csum)
Values ('wang la5', 'wanglaowu', 0)
Go

-- Create a scalar function for query level
Create Function getlevelbyid
(
@ CID nvarchar (20)
)
Returns nvarchar (28)
As
Begin
Declare @ csum int, @ level nvarchar (25)
Set @ level = @ CID + '==> no such card number'
If exists (select CID from custominfo where cid = @ CID)
Begin
Set @ csum = (select csum from custominfo where cid = @ CID)
Set @ level = (select top 1 [name] From integrallevel where startcount <= @ csum order by DESC)
Set @ level = @ CID + '---' + @ level
End
Return @ level
End
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.