使用sql语句在sqlserver上创建用户表
首先判断数据库中是否存在该表,存在就删除再新建,不存在就直接新建。
–用户表用于记录用户信息
if exists(select * from sysobjects where name=‘User’)
drop table User
go
create table User
(
UserId int primary key identity(1,1), --编号
UserName nvarchar(50)not null, --姓名
Pwd nvarchar(50)not null, --密码
Stype int not null, --部门编号
)
–查询所得视图数据
–创建部门表
if exists(select * from sysobjects where name=‘TypeName’)
drop table TypeName
go
create table TypeName
(
Id int primary key identity(1,1), --部门编号
etype nvarchar(50)not null, --部门名称
)
–查询所得视图数据
–使用两表联查
select User.UserId,User.UserName,User.Pwd,TypeName.etype
from User inner join TypeName
on User.Stype=TypeName.Id;
–所得视图数据
–通过姓名使用存储过程查询数据查看个人信息
if exists(select * from sysobjects where name=‘usp_MessageByName’)
drop proc usp_MessageByName
go
create proc usp_MessageByName
@UserName nvarchar(50)
as
select User.UserId,User.UserName,User.Pwd,TypeName.etype
from User inner join TypeName
on User.Stype=TypeName.Id and UserName=@UserName
go
–执行
exec usp_MessageByName ‘孙莹婷’
–所得视图数据