• Sql Server 存储过程


    用户的增删改查

    新增

    --新增用户
    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
    
  • 相关阅读:
    NX 8.5 License Server Firewall Setting
    Cisco ASA intra-interface routing
    How to configure windows machine to allow file sharing with dns alias (CNAME)
    Install unifi controller on CentOS
    Android图片加载框架Glide用法
    判断app是否已启动
    SharedPreferences的一个工具类适合的数据类型包括String、Integer、Boolean、Float、Long
    android获取缓存大小和清除缓存
    对字符串进行MD5加密工具类
    android代码设置RelativeLayout的高度
  • 原文地址:https://www.cnblogs.com/yanjiez/p/10141610.html
Copyright © 2020-2023  润新知