完整的数据库代码,实现小型数据库的所有功能,包括登录判断,注入式攻击,存储过程。。。。。。。。。
)
+ '/' + convert(nvarchar(10),datepart(month,'2011-10-25'))
+ '/' + convert(nvarchar(10),datepart(day,'2011-10-25'))
--按出生年份统计学生人数
--sid counts years
select count(sid),datepart(year,birthday) from student group by datepart(year,birthday)
select * from @table
declare @table nvarchar(10)
set @table = 'student'
exec('select * from ' + @table)
create proc sp_AA
@table varchar(10)
as
exec ('select * from ' + @table)
exec sp_AA 'student'
--编写通用版的分页存储过程
--自定义函数
create function fun(@i int) returns nvarchar(10)
as
begin
return convert(nvarchar(10),@i)
end
print dbo.fun(10)--dbo 当前数据库里有效
--定义函数 输入姓名后返回学号
create function fun2(@sname nvarchar(10)) returns int
as
begin
declare @i int
set @i = (select sid from student where sname=@sname)
return @i
end
print dbo.fun2('张三')
--查询学号,课程号,分数 同时在分数栏将<60的成绩直接输出为不及格
--学号 课程 分数
--1001 1 95
--1002 1 不及格
alter function fun3(@score int) returns nvarchar(10)
as
begin
declare @r nvarchar(10)
if @score<60
set @r = '不及格'
else
set @r = convert(nvarchar(10),@score)
return @r
end
select sid,cid,dbo.fun3(score) from score
--在同一列,输出 Employees 表中的lastname 和 firstname 字段
create function fun4(@lastname varchar(10),@firstname varchar(10)) returns varchar(20)
as
begin
return @lastname +'-'+@firstname
end
select employeeid,dbo.fun4(lastname,firstname),title from employees
--计算 order details 表 每条订单的总价
select * from [order details]
create function fun5(@unitprice money,@quantity smallint,@discount real) returns smallint
as
begin
return @unitprice * @quantity * (1 - @discount)
end
select *,dbo.fun5(unitprice,quantity,discount) from [order details]
--游标 不占用物理内存,全部是临时文件
declare cur1 cursor for select sid,sname,class from student
open cur1
declare @sid int, @sname nvarchar(10),@class nvarchar(10),@i int
set @i = 1
fetch from cur1 into @sid,@sname,@class --fetch 取游标所在的行的值
while @@fetch_status = 0
begin
print convert(nvarchar(10),@i)+'. '+ convert(nvarchar(10),@sid)+','+@sname+','+@class
set @i =@i + 1
fetch from cur1 into @sid,@sname,@class
end
close cur1
deallocate cur1
--给所有分数<60的人加送10分
declare cur2 cursor for select * from score
open cur2
declare @sid int,@cid int,@score int
fetch from cur2 into @sid,@cid,@score
while @@fetch_status = 0 --0语句成功 -1语句失败或行不在结果集中 -2提取的行不存在
begin
if @score < 60
update score set score = @sc
ore + 10 where sid = @sid and cid = @cid
fetch from cur2 into @sid,@cid,@score
end
close cur2
deallocate cur2 --清空资源
--找出重名的人,并使用合适的方式返回