SQL 存储过程 【大神来】

zhangluotian 2013-07-02 07:05:12
需求是这样:客户端传入“姓名”,
比如“张三”然后 一点提交按钮,就调用sql的存储过程。

存储过程需求:把张三 到Name表,做对比,看是否有,如果没有重复,往表Name里新增一条记录,

如果有,有几条,找出一个table(有可能是:张三1、张三2、张三3),

如果只有一条,则在原来的名字 加1,

如果有多条,找出最后一个“张三3”,

然后改成“张三4”往表Name里新增一条记录。

大神们给个思路的存储过程,
1怎么判断取出查到的记录的最后一条,里面的name列的值?
2怎么新增到表里?
或者给一些参考资料。谢谢!
...全文
222 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangluotian 2013-07-03
  • 打赏
  • 举报
回复
感谢!!!!!!
everglow 2013-07-03
  • 打赏
  • 举报
回复
create table dbo.t (name varchar(50)) go create procedure t @name varchar(50) as begin if not exists(select 1 from t where name=@name or ISNUMERIC(REPLACE(name,@name,''))=1) begin insert into t values(@name) end else begin if exists(select 1 from t where name=@name) begin update t set name=name+'1' where name=@name insert into t values(@name+'2') end if exists(select 1 from t where ISNUMERIC(REPLACE(name,@name,''))=1) begin declare @a int select @a=max(CAST(REPLACE(name,@name,'') as int)) from t where ISNUMERIC(REPLACE(name,@name,''))=1 set @a=@a+1 insert into t values(@name+CAST(@a as varchar)) end end end
lzw_0736 2013-07-03
  • 打赏
  • 举报
回复
ALTER procedure [dbo].[usercheckee] (@infullname varchar(50), @outcheck char(3) OUTPUT) as begin if object_id('tempdb..#tabletest') is not null --提示列名tempdb..#tabletest无效 drop table #tabletest --如果临时表存在,删除临时表 if exists(select * from usra01 where A0101=@infullname) begin--找出最大的张三,并分离出数字 并转化类型 再加1, 为了程序清晰,暂且放入临时表; select cast(replace(max(A0101) ,@infullname,'') as decimal(18,0))+1 as num into #tabletest from usra01 where A0101 like @infullname --提示where附近语法错误 group by A0101 insert into usra01 (name) select 'num'+@infullname from #tabletest --values附近语法错误 select @outcheck='yes' end else select @outcheck='no' end
zhangluotian 2013-07-03
  • 打赏
  • 举报
回复
我又写了一个,但是有语法错误,帮忙看一下!


ALTER procedure [dbo].[usercheckee]
@infullname varchar(50), 
@outcheck char(3) OUTPUT 
as
if object_id("tempdb..#tabletest") in not null --提示列名tempdb..#tabletest无效
drop table #tabletest  --如果临时表存在,删除临时表
if exists(select * from usra01  where A0101=@infullname)
begin--找出最大的张三,并分离出数字 并转化类型 再加1, 为了程序清晰,暂且放入临时表;
select cast(replace(max(A0101) ,@infullname,'')  as decimal(18,0))+1 as  num  into #tabletest  from usra01 group by A0101 where A0101 like @infullname --提示where附近语法错误
insert into usra01  name  values (select 'num'+@infullname    from #tabletest)
--values附近语法错误
select @outcheck='yes'
end
else 
select @outcheck='no'

zhangluotian 2013-07-02
  • 打赏
  • 举报
回复
现在只实现了 查看表里是否有重复姓名

ALTER procedure [dbo].[usercheckee] 
@infullname varchar(50), 
@outcheck char(3) OUTPUT 
as 
if exists(select * from usra01 where A0101=@infullname) 
select @outcheck='yes' 
else 
select @outcheck='no'

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧