从创建数据库到存储过程与用户自定义函数的小感
发布时间:2024-01-16 23:42:05
标签:存储过程,自定义函数
create database MyDb
on
(
name=mainDb,
filename='c:\MyDb\mainDb.mdf',
size=10,
maxsize=100,
filegrowth=4
),
(
name=secondDb,
filename='C:\MyDb\secondDb.ndf',
size=15,
maxsize=28,
filegrowth=2
)
log on
(
name=log_Db,
filename='C:\MyDb\log_Db',
size=20,
filegrowth=10%
)
--创建数据库的一般格式
use mydb
create table student
(
stuId int primary key identity (1,1),
stuName varchar (20) not null,
stuAge int not null check(stuAge between 20 and 50),
stuSex varchar(4) not null check(stusex in('F','M')),
stuDept varchar(20) check( stuDept in('软工系','环艺系','电子商务系')),
stuAddress varchar(20) not null
)
drop table student
select * from student
insert into student values ('孙业宝',22,'M','软工系','河北省邢台市')
insert into student values ('孙婷',20,'F','电子商务系','河北省邢台市')
insert into student values ('孟几',22,'F','电子商务系','河北省邢台市')
insert into student values ('小五',22,'M','软工系','河北省革要市')
insert into student values ('王丹丹',22,'M','软工系','河北省阜阳市')
insert into student values ('陈海波',22,'M','软工系','河北省合肥市')
--单一的输入输出参数的存储过程,
create proc Myproc
@Dept varchar(20),@count int output
As
if not exists(select * from student where Studept=@dept)
print '没有指定类型的学生存在!!'
else
select @count=Count(*) from student where studept=@dept
drop proc myproc
--执行该存储过程
declare @result int
Exec myproc '软工系',@result output
print @result
--多输入输出的存储过程.
create proc Searchstu
@area varchar(20),@Sex varchar(2),@count int output,@avg_age int output
as
select @count=count(*),@avg_age=Avg(stuage) from student
where stuaddress=@area and stusex=@sex
--执行该存储过程
declare @stuNo int ,@stuAvg_age int
exec searchstu '河北省邢台市','M',@stuNo output,@stuAvg_age output
select @stuNo as 学生总数,@stuavg_age as 平均年龄
--用户自定义的函数(求立方体体积定义标题函数返回单一值)
create function dbo.CubicVolume
(@CubeLength int,@CubeHenght int,@CubeWidth int)
Returns int
as
begin
return (@CubeLength*@CubeHenght*@CubeWidth)
end
drop function CubicVolume
--调用该方法
select dbo.CubicVolume(10,10,10)
--用户自定义的函数(内嵌表形式,返回一个表)
create function f_stuInfo(@studept varchar(20))
returns table
as
return
(
select * from student where studept=@studept
)
--调用该方法
select * from dbo.f_stuInfo('软工系')
--用户自定义的函数(多语句表值函数,返回一个用户想要显的部分数据的表)
create function f_stuSexTye(@stuDept varchar(10))
returns @t_stuDetailInfo table(
stuName varchar(20),
stuAge int ,
stuSex varchar(4)
)
as
begin
insert into @t_stuDetailInfo
select Stuname,stuage,
Case stusex
when 'M' then '男'
when 'F' then '女'
end
from student
where stuDept=@studept
return
end
--调用该方法函数
select * from dbo.f_stuTye('软工系')
猜你喜欢
今天继续给大家介绍Python相关知识,本文主要内容是Python asyncio异步编程常见问题。一、asyncio编程简单示例首先,我们
前言jsonpath是一个可以在复杂的json数据中根据用户指定的规则找到特定数据的库。本文利用jsonpath对接口进行封装,旨在写一个对
本文实例讲述了mysql存储过程之游标(DECLARE)原理与用法。分享给大家供大家参考,具体如下:我们在处理存储过程中的结果集时,可以使用
单一数据读取方式:第一种:slice_input_producer()# 返回值可以直接通过 Session.run([images, la
前言开发环境:Centos 7 + Python 3.5.1 + Qt Creator(只是使用Qt Creator编译而已,并没有使用QT
环境配置系统:Windows10版本:python 3.8Turtle扫盲1.绘图窗体的设置turtle.setup(width, heig
前言在javascript中,我们都知道使用var来声明变量。javascript是函数级作用域,函数内可以访问函数外的变量,函数外不能访问
django-debug-toolbar介绍django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试
代码如下:using System; using System.Data; using System.Configuration; usin
第一题: give you two var a and b, print the value of a+b, just do it!根据提议
python使用ctypes模块调用windows api GetVersionEx获取当前系统版本,没有使用python32#!c:/py
np.percentilenumpy.percentile(a, q, axis=None, out=None, overwrite_inp
概述concurrent.futures 是 3.2 中引入的新模块,它为异步执行可调用对象提供了高层接口。可以使用 ThreadPoolE
mysql日期相减的天数函数DATEDIFF() 函数返回两个日期之间的天数。语法DATEDIFF(date1,date2)date1 和
本文实例讲述了Python实现的HMacMD5加密算法。分享给大家供大家参考,具体如下:什么是 HMAC-MD5?1、比如你和对方共享了一个
如下所示:import cv2import mathimport numpy as npdef move(img): height, wid
一、运行时恐慌panicpanic是一种在运行时抛出来的异常。比如"index of range"。panic的详情:p
前言Vue 框架通过数据双向绑定和虚拟 DOM 技术,帮我们处理了前端开发中最脏最累的 DOM 操作部分, 我们不再需要去考虑如何操作 DO
本文主要讲解的是表单,这个其实对于做过网站的人来说,并不陌生,而且可以说是最为常用的提交数据的Form表单。本文主要来讲解一下内容:1.基本
之前项目有一个需求,业务人员使用中文编写一些自定义公式,然后需要我们后台执行将结果返回到界面上,于是就基于有限状态机写了这个词法分析器,比较