你是把上面的002为该业务内的流水号!try: ----建表 if exists(select * from sysobjects where name='send') drop table sendcreate table send ( smid varchar(20) PRIMARY KEY, message varchar(8000), beactive varchar(10) goif exists(select * from sysobjects where name='maxcode') drop table maxcode create table maxcode (
maxcode int) insert into maxcode VALUES (001) go--------------------------------------------------------- SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO create proc proc_send @tshcode varchar(10), ----短信特符号 @ywcode varchar(10), ------业务代码 @textcode varchar(8000) -----短信内容 as
declare @errcode integer set @errcode=10000 declare @return integer /*返回结果的初始化*/ set @return=0--启动事务处理 declare @tran_point int --控制事务嵌套 set @tran_point=@@trancount --保存事务点 if @tran_point=0 begin tran tran_send else save tran tran_send declare @lshcode int select @lshcode=(maxcode+1) from maxcode update maxcode set maxcode = maxcode+1 if @@error<>0 begin set @return=1 goto err_lab end insert into send (smid,message,beactive) values ('@tshcode'+'@ywcode'+convert(varchar(20),@lshcode),@textcode,'否')
if @@error<>0 begin set @return=2 goto err_lab end --事务处理 if @tran_point=0 commit tran tran_send goto return_laberr_lab: if @return<100 set @return=@errcode +@return rollback tran tran_sendreturn_lab: return @return GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
----建表
if exists(select * from sysobjects where name='send')
drop table sendcreate table send (
smid varchar(20) PRIMARY KEY,
message varchar(8000),
beactive varchar(10) goif exists(select * from sysobjects where name='maxcode')
drop table maxcode create table maxcode (
maxcode int)
insert into maxcode VALUES (001) go---------------------------------------------------------
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
create proc proc_send @tshcode varchar(10), ----短信特符号
@ywcode varchar(10), ------业务代码
@textcode varchar(8000) -----短信内容
as
declare @errcode integer
set @errcode=10000
declare @return integer /*返回结果的初始化*/
set @return=0--启动事务处理
declare @tran_point int --控制事务嵌套
set @tran_point=@@trancount --保存事务点
if @tran_point=0
begin tran tran_send
else
save tran tran_send
declare @lshcode int select @lshcode=(maxcode+1) from maxcode update maxcode set maxcode = maxcode+1 if @@error<>0
begin
set @return=1
goto err_lab
end
insert into send (smid,message,beactive) values ('@tshcode'+'@ywcode'+convert(varchar(20),@lshcode),@textcode,'否')
if @@error<>0
begin
set @return=2
goto err_lab
end
--事务处理
if @tran_point=0
commit tran tran_send
goto return_laberr_lab:
if @return<100 set @return=@errcode +@return
rollback tran tran_sendreturn_lab:
return @return
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
要写存储过程还需要建一个表quencent,字段包括,first(初始值)比如08857400101001,nowid(当前smid值),buchang(步长,设为一,每次增加1),maxsmid(最大值08857400101999),mixsmid(最小值08857400101001)上述例子是以业务代码01举例的;业务代码是区分各个应用程序的因为有不同的应用程序调用短信功能;业务代码用2位区分;前面088574001是短信特符号,固定的;就是说根据不同的业务代码,就要不通的最大值最小值当前值等等;
本来对于发送表格来说每发送成功后数据删掉了,就不会存在重复情况(因为每个业务代码后有3位10000个号码肯定不会产生重复),但是对于接收的部分就有可能产生重复;接收表格位smid(主键),content,addtel(手机号);因为对于回复的数据有可能产生重复。所以现在的存储过程就是一个业务需要发送短信产生smid的时候需要把业务代码传送给存储过程,然后存储过程产生一个smid给该业务,产生的这个smid需要到接收表格里面测试产生的这个smid是否存在(比如已经用了08857400101008,现在就需要分配给他们smid位08857400101009但是如果上次循环这个记录还没有处理,就寻找08857400101010给他同样检查该记录处理没有,如果没有处理寻找下一个,没有这条记录就把这个号码分配给他),如果是其他应用程序,比如业务代码位21什么的就在08857400102×××××里面执行相应的操作。