码迷,mamicode.com
首页 > 数据库 > 详细

Sql Server 存储过程 - 用户操作实例

时间:2018-12-19 11:02:52      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:ice   test   uid   roc   update   let   arc   one   set   

用户的增删改查

新增

--新增用户
create procedure addDeviceUser
    @name nvarchar(36) ,
    @phone nvarchar(36) ,
    @password nvarchar(255)
as
begin
    if exists(select 1 from DeviceUser where phone=@phone) --判断用户是否存在   
        select 1 from DeviceUser where 1!=1
    else
        begin
            insert into DeviceUser (id,name,phone,password) values(newid(),@name,@phone,@password)
            select id,name,phone from DeviceUser where phone=@phone
        end
end

--执行
exec addDeviceUser @name='testname',@phone='18800000000',@password='123'

查询

--获取用户
alter procedure getDeviceUser
    @id nvarchar(36),
    @phone nvarchar(36)
as
begin
    if(@id=''and @phone='')
        select id,name,phone,isAdmin,createDate from DeviceUser where isAdmin=0 order by createDate desc
    else if(@id!='')
        select id,name,phone,isAdmin,createDate from DeviceUser where id=@id
    else
        select id,name,phone,isAdmin,createDate from DeviceUser where phone=@phone
end

--执行
exec getDeviceUser @id='', @phone=''
exec getDeviceUser @id='test guid', @phone=''
exec getDeviceUser @id='', @phone='18800000000'


--登录
create procedure getDeviceUserForLogin
    @phone nvarchar(36),
    @password nvarchar(255)
as
begin
    select id,name,phone,isAdmin,createDate from DeviceUser where phone=@phone and password=@password 
end

exec getDeviceUserForLogin @phone='18800000000',@password='123'
exec getDeviceUserForLogin @phone='18800000000',@password='123456'
exec getDeviceUserForLogin @phone='18811111111',@password='123456'

更新

create procedure updateDeviceUser
    @id nvarchar(36) ,
    @name nvarchar(36) ,
    @phone nvarchar(36) ,
    @password nvarchar(255)
as
begin
    if not exists(select 1 from DeviceUser where id=@id) --判断用户是否存在 
        select 1 from DeviceUser where 1!=1
    else
        begin
            update DeviceUser set name=@name,phone=@phone,password=@password  where id=@id
            select id,name,phone from DeviceUser where id=@id
        end
end
 
--执行
exec updateDeviceUser @id='guid 1',@name='test name 1',@phone='18800000000',@password='123'
exec updateDeviceUser @id='guid 2',@name='test name 2',@phone='18811111111',@password='12345678'

删除

create procedure deleteDeviceUser
    @id nvarchar(36)
as
delete from DeviceUser  where id=@id

Sql Server 存储过程 - 用户操作实例

标签:ice   test   uid   roc   update   let   arc   one   set   

原文地址:https://www.cnblogs.com/yanjiez/p/10141610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!