Sql 用于创建表、存储过程、触发器、标量函数的代码
2010-10-20 16:43
267 查看
--建立客户信息表
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('张三','zhangsan',0)
go
insert into CustomInfo(cid,cpassword,csum)
values('李四','lisi123',0)
go
insert into CustomInfo(cid,cpassword,csum)
values('王五','wangwu',0)
go --创建购买记录表
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 table IntegralLevel
(
startcount int not null primary key,
[Name] nvarchar(10) not null
)
go insert into IntegralLevel(startcount,[Name])
values(0,'普通卡用户')
go
insert into IntegralLevel(startcount,[Name])
values(10,'铜卡用户')
go
insert into IntegralLevel(startcount,[Name])
values(20,'银卡用户')
go
insert into IntegralLevel(startcount,[Name])
values(40,'金卡用户')
go
insert into IntegralLevel(startcount,[Name])
values(60,'贵宾卡用户')
go --建立一个触发器
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 '感谢您的购买,您的积分已经被记录下来!'
end
go --制作购物的SP
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 '恭喜您购买成功了!'
end
go execute BuyFood '张三','zhangsan',5
go --建立一个总的客户信息表
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('张三丰','zhangsanfeng',0)
go
insert into ToTalCustom(cid,cpassword,csum)
values('王老五','wanglaowu',0)
go --制作一个用于查级别的标量函数
create function GetLevelByID
(
@cid nvarchar(20)
)
returns nvarchar(28)
as
begin
declare @csum int,@level nvarchar(25)
set @level = @cid + '==>无此卡号'
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
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('张三','zhangsan',0)
go
insert into CustomInfo(cid,cpassword,csum)
values('李四','lisi123',0)
go
insert into CustomInfo(cid,cpassword,csum)
values('王五','wangwu',0)
go --创建购买记录表
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 table IntegralLevel
(
startcount int not null primary key,
[Name] nvarchar(10) not null
)
go insert into IntegralLevel(startcount,[Name])
values(0,'普通卡用户')
go
insert into IntegralLevel(startcount,[Name])
values(10,'铜卡用户')
go
insert into IntegralLevel(startcount,[Name])
values(20,'银卡用户')
go
insert into IntegralLevel(startcount,[Name])
values(40,'金卡用户')
go
insert into IntegralLevel(startcount,[Name])
values(60,'贵宾卡用户')
go --建立一个触发器
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 '感谢您的购买,您的积分已经被记录下来!'
end
go --制作购物的SP
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 '恭喜您购买成功了!'
end
go execute BuyFood '张三','zhangsan',5
go --建立一个总的客户信息表
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('张三丰','zhangsanfeng',0)
go
insert into ToTalCustom(cid,cpassword,csum)
values('王老五','wanglaowu',0)
go --制作一个用于查级别的标量函数
create function GetLevelByID
(
@cid nvarchar(20)
)
returns nvarchar(28)
as
begin
declare @csum int,@level nvarchar(25)
set @level = @cid + '==>无此卡号'
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
相关文章推荐
- Sql 用于创建表、存储过程、触发器、标量函数的代码
- 刷新SQL Server所有视图、函数、存储过程 更多 sql 此脚本用于在删除或添加字段时刷新相关视图,并检查视图、函数、存储过程有效性。 [SQL]代码 --视图、存储过程、函数名称 DECLARE @NAME NVARCHAR(255); --局部游标 DECLARE @CUR CURSOR --自动修改未上状态为旷课 SET @CUR=CURSOR SCROLL DYNAMIC FO
- 创建数据库、表、主外键、各种约束、存储过程、视图、索引、事务使用、触发器、创建登录账号、数据库用户 Sql语句示例
- VC++ 表格、完整性约束、视图、触发器、存储过程的创建SQL语句
- SQL Server 中存储过程,函数,触发器代码存放于sys.sql_modules
- 如何用存储过程及SQL代码创建登录、用户和角色
- mssql:t-sql;创建表;给表添加约束;使用变量;事务,索引,视图;存储过程;触发器trigger;播放器http://www.smartgz.com/blog/Article/956.asp
- 在VB.NET中 用代码 利用SQL语句创建数据库、表、存储过程、视图、索引、规则、修改表、查看数据等的方法
- 关于 sql 语法 创建表 附加 分离 备份 存储过程 触发器
- sql生成(mssql对于表的delete和update做备份的触发器)代码的存储过程
- mssql:t-sql;创建表;给表添加约束;使用变量;事务,索引,视图;存储过程;触发器trigger
- SQL查询出表、存储过程、触发器的创建时间和最后修改时间示例
- SQL数据用于易飞数据库值部门,客户,供应商代码(标量函数)返回名称
- T-SQL语句创建存储过程和触发器练习
- [Database]创建存储过程并在ASP.NET中的调用带参数的存储过程与及调用带参数的SQL语句对比
- sql 中存储过程的创建和使用
- 从 syscomments 取系统存储过程、用户存储过程、视图、函数、触发器等对象的sql定义语句
- python(11-4 数据库的事务 触发器 存储过程 慢查询及sql优化)
- SQL:创建触发器
- SQL使用代码创建数据完整性,约束