|
想写一个通用的用户管理数据库,因此,今天写了如下 T-SQL 脚本,练习自己的写脚本能力。现在放在这里,也许对一些网友有作用。呵呵......
2012-01-12
朱亦文- ---=========================================================================---
- --- 创建 [Person] 数据库
- --- 日期:2011-01-12
- ---=========================================================================---
- USE [master]
- GO
- IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'Person')
- DROP DATABASE [Person]
- GO
- CREATE DATABASE [Person] ON
- (
- NAME = N'Person',
- FILENAME = N'D:\DB\Person\Person.mdf',
- SIZE = 1024KB,
- MAXSIZE = UNLIMITED,
- FILEGROWTH = 1024KB
- )
- LOG ON
- (
- NAME = N'Person_log',
- FILENAME = N'D:\DB\Person\Person_log.ldf',
- SIZE = 1024KB,
- MAXSIZE = UNLIMITED,
- FILEGROWTH = 1024KB
- )
- GO
- USE [Person]
- GO
- ---=========================================================================---
- --- 创建表
- ---=========================================================================---
- GO
- --------------------------------------------------
- --- 表 :USysUsers
- --- 用途:存储用户
- --------------------------------------------------
- CREATE TABLE [dbo].[USysUsers](
- [UID] [int] IDENTITY(1000,1) NOT NULL,
- [UserName] [nvarchar](50) NOT NULL,
- [Password] [varchar](256) NOT NULL DEFAULT (''),
- [IsSystem] [bit] NOT NULL DEFAULT (0),
- [Description] [nvarchar](256) NULL,
- [Enabled] [bit] NOT NULL DEFAULT (1),
- [CreateTime] [datetime] NOT NULL DEFAULT (getdate()),
- CONSTRAINT [PK_USysUsers] PRIMARY KEY CLUSTERED
- (
- [UID] ASC
- ),
- CONSTRAINT [IX_USysUsers] UNIQUE NONCLUSTERED
- (
- [UserName] ASC
- )
- )
- GO
- --- 创建系统帐户
- SET IDENTITY_INSERT [dbo].[USysUsers] ON
- GO
- INSERT [dbo].[USysUsers]
- ([UID],[UserName],[Password],[IsSystem],[Description],[Enabled]) VALUES
- (0, N'Guest', '', 1, N'匿名来宾用户', 0)
- INSERT [dbo].[USysUsers]
- ([UID],[UserName],[Password],[IsSystem],[Description],[Enabled]) VALUES
- (1, N'Administrator', 'sys', 1, N'系统管理员', 1)
- GO
- SET IDENTITY_INSERT [dbo].[USysUsers] OFF
- GO
- --------------------------------------------------
- --- 函数:fbUSysGeustIsEnabled
- --- 目的:判定是否允许匿名来宾用户
- --- 返回:0 - 不允许,1 - 允许
- --- 依赖:dbo.USysUsers
- --------------------------------------------------
- CREATE FUNCTION [dbo].[fbUSysGeustIsEnabled]()
- RETURNS bit
- AS
- BEGIN
- DECLARE @ret bit
- -- 从[dbo].[USysUsers]表中获取 Guest 用户启用
- -- 作为系统是否允许匿名
- SELECT @ret = [Enabled] FROM [dbo].[USysUsers] WHERE [UID] = 0
- IF @ret IS NULL
- SET @ret = 0
- RETURN @ret
- END
- GO
- --------------------------------------------------
- --- 过程:_paUSysSetUserEnabled
- --- 目的:设置用户是否启用
- --- 参数:
- --- @UID - 用户 ID
- --- @Enabled - 0-不允许,1-允许
- --- 返回:0 - 不成功
- --- 依赖:dbo.USysUsers
- --------------------------------------------------
- CREATE PROCEDURE [dbo].[_paUSysSetUserEnabled]
- (
- @UID int,
- @Enabled bit
- )
- AS
- BEGIN
- IF @Enabled IS NOT NULL
- UPDATE [dbo].[USysUsers]
- SET Enabled = @Enabled
- WHERE UID = @UID
- RETURN @@ROWCOUNT
- END
- GO
- --------------------------------------------------
- --- 过程:paUserDisableGuest
- --- 目的:停用匿名用户
- --- 返回:0 - 不成功
- --- 依赖:dbo.USysUsers/dbo._paUSysSetUserEnabled
- --------------------------------------------------
- CREATE PROCEDURE [dbo].[paUserDisableGuest]
- AS
- BEGIN
- DECLARE @return_value int
- EXEC @return_value = [dbo].[_paUSysSetUserEnabled]
- @UID = 0,
- @Enabled = 0
- RETURN @return_value
- END
- GO
- --------------------------------------------------
- --- 过程:paUserEnableGuest
- --- 目的:启用匿名用户
- --- 返回:0 - 不成功
- --- 依赖:dbo.USysUsers/dbo._paUSysSetUserEnabled
- --------------------------------------------------
- CREATE PROCEDURE [dbo].[paUserEnableGuest]
- AS
- BEGIN
- DECLARE @return_value int
- EXEC @return_value = [dbo].[_paUSysSetUserEnabled]
- @UID = 0,
- @Enabled = 1
- RETURN @return_value
- END
- GO
- --------------------------------------------------
- --- 过程:paUserDisableUser
- --- 目的:停用用户
- --- 返回:0 - 不成功
- --- 依赖:dbo.USysUsers/dbo._paUSysSetUserEnabled
- --------------------------------------------------
- CREATE PROCEDURE [dbo].[paUserDisableUser](@UID int)
- AS
- BEGIN
- DECLARE @return_value int
- EXEC @return_value = [dbo].[_paUSysSetUserEnabled]
- @UID = @UID,
- @Enabled = 0
- RETURN @return_value
- END
- GO
- --------------------------------------------------
- --- 过程:paUserEnableUser
- --- 目的:启用用户
- --- 返回:0 - 不成功
- --- 依赖:dbo.USysUsers/dbo._paUSysSetUserEnabled
- --------------------------------------------------
- CREATE PROCEDURE [dbo].[paUserEnableUser](@UID int)
- AS
- BEGIN
- DECLARE @return_value int
- EXEC @return_value = [dbo].[_paUSysSetUserEnabled]
- @UID = @UID,
- @Enabled = 1
- RETURN @return_value
- END
- GO
- --------------------------------------------------
- --- 表 :USysUserExtender
- --- 用途:存储用户扩展属性
- --- 依赖:dbo.USysUsers
- --------------------------------------------------
- CREATE TABLE [dbo].[USysUserExtender](
- [UID] [int] NOT NULL,
- [RealName] [nvarchar](50) NULL,
- [Gender] [nchar](1) NULL,
- [Birthday] [datetime] NULL,
- [Email] [nvarchar](256) NULL,
- [Company] [nvarchar](50) NULL,
- [Post] [nvarchar](10) NULL, /* 职位/岗位/职务 */
- [Title] [nvarchar](10) NULL, /* 称谓/头衔/职称 */
- [Mobile] [varchar](50) NULL,
- [Tel] [varchar](50) NULL,
- [Province] [nvarchar](10) NULL,
- [City] [nvarchar](10) NULL,
- [ZipCode] [varchar](6) NULL,
- CONSTRAINT [PK_USysUserExtender] PRIMARY KEY CLUSTERED
- (
- [UID] ASC
- )
- )
- GO
- --- 设置 USysUserExtender 与 USysUsers 关系
- --- 级联更新、级联删除
- ALTER TABLE [dbo].[USysUserExtender]
- WITH
- CHECK ADD CONSTRAINT [FK_USysUserExtender_USysUsers]
- FOREIGN KEY([UID]) REFERENCES [dbo].[USysUsers] ([UID])
- ON UPDATE CASCADE
- ON DELETE CASCADE
- GO
- --------------------------------------------------
- --- 函数:_ftUSysUsers
- --- 用途:获取用户列表
- --- 依赖:dbo.USysUsers/dbo.USysUserExtender
- --------------------------------------------------
- CREATE FUNCTION [dbo].[_ftUSysUsers]
- (
- @System bit = NULL,
- @Enabled bit = NULL
- )
- RETURNS TABLE
- AS
- RETURN
- (
- SELECT
- u.UID,
- u.UserName,
- u.Password,
- u.IsSystem,
- u.Description,
- u.CreateTime,
- e.RealName,
- e.Gender,
- e.Birthday,
- e.Email,
- e.Company,
- e.Post,
- e.Title,
- e.Mobile,
- e.Tel,
- e.Province,
- e.City,
- e.ZipCode
- FROM
- dbo.USysUsers AS u LEFT OUTER JOIN
- dbo.USysUserExtender AS e ON u.UID = e.UID
- WHERE
- ((u.Enabled = @Enabled) OR (@Enabled IS NULL)) AND
- ((u.IsSystem = @System) OR (@System IS NULL))
- )
- GO
- --------------------------------------------------
- --- 视图:vUsersAll
- --- 用途:所有用户
- --- 依赖:dbo.USysUsers/dbo.USysUserExtender/dbo._ftUSysUsers
- --------------------------------------------------
- CREATE VIEW [dbo].[vUsersAll]
- AS
- SELECT * FROM [dbo].[_ftUSysUsers](NULL, NULL)
- GO
- --------------------------------------------------
- --- 视图:vUsersSystem
- --- 用途:所有系统用户
- --- 依赖:dbo.USysUsers/dbo.USysUserExtender/dbo._ftUSysUsers
- --------------------------------------------------
- CREATE VIEW [dbo].[vUsersSystem]
- AS
- SELECT * FROM [dbo].[_ftUSysUsers](1, NULL)
- GO
- --------------------------------------------------
- --- 视图:vUsersSystemCurrent
- --- 用途:当前系统用户
- --- 依赖:dbo.USysUsers/dbo.USysUserExtender/dbo._ftUSysUsers
- --------------------------------------------------
- CREATE VIEW [dbo].[vUsersSystemCurrent]
- AS
- SELECT * FROM [dbo].[_ftUSysUsers](1, 1)
- GO
- --------------------------------------------------
- --- 视图:vUsersSystemDisabled
- --- 用途:当前禁用的系统用户
- --- 依赖:dbo.USysUsers/dbo.USysUserExtender/dbo._ftUSysUsers
- --------------------------------------------------
- CREATE VIEW [dbo].[vUsersSystemDisabled]
- AS
- SELECT * FROM [dbo].[_ftUSysUsers](1, 0)
- GO
- --------------------------------------------------
- --- 视图:vUsersGeneral
- --- 用途:所有一般用户
- --- 依赖:dbo.USysUsers/dbo.USysUserExtender/dbo._ftUSysUsers
- --------------------------------------------------
- CREATE VIEW [dbo].[vUsersGeneral]
- AS
- SELECT * FROM [dbo].[_ftUSysUsers](0, NULL)
- GO
- --------------------------------------------------
- --- 视图:vUsersGeneralCurrent
- --- 用途:当前一般用户
- --- 依赖:dbo.USysUsers/dbo.USysUserExtender/dbo._ftUSysUsers
- --------------------------------------------------
- CREATE VIEW [dbo].[vUsersGeneralCurrent]
- AS
- SELECT * FROM [dbo].[_ftUSysUsers](0, 1)
- GO
- --------------------------------------------------
- --- 视图:vUsersGeneralDisabled
- --- 用途:当前禁用的一般用户
- --- 依赖:dbo.USysUsers/dbo.USysUserExtender/dbo._ftUSysUsers
- --------------------------------------------------
- CREATE VIEW [dbo].[vUsersGeneralDisabled]
- AS
- SELECT * FROM [dbo].[_ftUSysUsers](0, 0)
- GO
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|