sql where 拼接

sql where 拼接

ALTER procedure  [dbo].[procUser_Role_AllList]
  @session varchar(30),
  @ip varchar(30),
  @u_id int=0,
  @login_code varchar(30),
  @rescode int  output,
  @resdesc varchar(200) output
as
  begin try
	declare @operate_userid bigint=0
	exec spCheck @session,@ip,'p_user_role_alllist',@rescode output,@resdesc output,@operate_userid output
	if @rescode <> 0 raiserror ('验证失败,原因:%s',11,1,@resdesc)

	declare @Sql nvarchar(max)
	declare @SqlWhere nvarchar(max)
	set @SqlWhere=''
	if @u_id <> 0 
	begin
		declare @db_count int
		select @db_count=count(*) from t_user where deleted = 0 and u_id = @u_id 
		if @db_count<=0 raiserror ('该用户不存在或已被删除!u_id=%d',11,3,@u_id);
		set @SqlWhere =' u_id ='+str(@u_id) 
	end
	if @login_code<>''
	begin
		if (@SqlWhere <> '') set @SqlWhere=@SqlWhere+' and '
		set @SqlWhere =' login_code like ''%'+ @login_code +'%'''
	end 
	set @sql = ' select u_id,login_code,r_id,name from v_user_role  where'+@SqlWhere+'' 
	exec sp_executesql @sql

	set @rescode = 0;
	set @resdesc = 'success';
end try	
begin catch
	set @rescode = ERROR_NUMBER();
	if @rescode=50000
		set @rescode=error_state()
	set @resdesc ='获取用户角色列表失败,'+ERROR_MESSAGE();
	select @rescode as rescode,@resdesc as resdesc
end catch 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值