1、#tmptable是存储过程中的一个内存表。这种不是内存表,应该叫临时表。
2、贴出你的代码

解决方案 »

  1.   

    提示对象名“#tmptable”无效 这种要看代码,另外@也不叫内存表,叫表变量
      

  2.   

    alter procedure sms_behind_sendsms
    @belong smallint
    as--思路:定时任务每天调度此任务,判断当天是否有需要发送的数据,有的话就写入发送表中
    --为了防止手动启动本任务,造成多次发送某类数据,因此发送后需记录日期,发送前需进行日期判断
    if exists(
      select top 1 sms_count from s_auto_send_log 
      where convert(char(10),real_time,121) = convert(char(10),getdate(),121)
              and belong = @belong
       )
    return
    /*----------重要的表结构-------------------
    select * from s_disturb       --免打扰名单
    select * from s_smsset        --短信表
    select * from s_keyword       --关键字表
    select * from s_sendtime      --时间段设置表
    select * from s_set_sendtime  --各种短信发送时间段设置表
    select * from s_para          --发送短信的参数设置表
    ------------------------------------------*/
    ----------------------------------------------------------------------------------------------------------
    declare @is_gjj_remind char(1),       --是否进行“公积金余额变动提醒” 1是0否
    @is_gjj_month_remind char(1), --是否进行“公积金【每月】【15】日定时提醒” 1是0否
    @gjj_monthselect char(1),     --【每月】选择参数 1每月2每两月3每季度4每半年5每年 这些值是事先约定好的
    @gjj_day int,                 --【15】的输入参数 输入范围:1-28(一个月最少的天数)
    @is_dk_remind char(1),        --是否进行“贷款余额变动提醒” 1是0否
    @is_repay_mind char(1),       --是否进行“应于还款【10】日前提醒” 1是0否
    @repay_day int,               --【10】的输入参数 输入范围:无限制
    @is_overdue_remind char(1),   --是否进行“逾期【7】日以上【每日】催收”
    @overdue_day int,             --【7】的输入参数 输入范围:无限制
    @overdue_dayselect char(1),   --【每日】的选择参数 1每日2每周3每两周4每月
    @sendmode char(1),       --发送模式0中心统一发送1各管理部分别发送
    @is_gjj_day_remind char(1),   --是否进行“公积金【3】日以上余额无变动人员,给【单位】【职工】【单位及职工】,发送催收短信”
    @gjj_day2 int,       --【3】的输入参数
    @sel_obj char(1),       --【单位】【职工】【单位及职工】选择参数,0单位1职工2单位及职工
    @is_overdue_remind_long char(1),--是否进行“长期逾期:逾期【30】日以上【每日 每周 每两周 每月】催收,向贷款人及【配偶】【担保人】【所在单位】,发送催收短信”
    @overdue_day1 int, --【30】的输入参数
    @overdue_dayselect1 char(1), --【每日】的选择参数 1每日2每周3每两周4每月
    @overdue_sel_obj char(1), --【配偶】的选择参数
    @overdue_sel_obj1 char(1), --【担保人】的选择参数
    @overdue_sel_obj2 char(1) --【所在单位】的选择参数declare @sendtimebegin datetime,     --发送时间的开始
    @handset char(11),           --手机号码
    @smscontent varchar(8000),   --发送内容,包含关键字
    @content varchar(8000)       --发送内容,不包含关键字declare @unit_account char(13),
    @person_account char(8),
    @sendtype char(1)--发送类型【1单位经办人 2缴存职工 3贷款职工】--查询相关的参数值
    select  @is_gjj_remind = is_gjj_remind,
    @is_gjj_month_remind = is_gjj_month_remind,
            @gjj_monthselect = gjj_monthselect,
            @gjj_day = gjj_day,
    @is_dk_remind = is_dk_remind,
    @is_repay_mind = is_repay_remind,
            @repay_day = repay_day,
    @is_overdue_remind = is_overdue_remind,
            @overdue_day = overdue_day,
            @overdue_dayselect = overdue_dayselect, 
    @sendmode = sendmode,      
    @is_gjj_day_remind = is_gjj_day_remind,
    @gjj_day2 = gjj_day2,
    @sel_obj = sel_obj,
    @is_overdue_remind_long = is_overdue_remind_long,
    @overdue_day1 = overdue_day1,
    @overdue_dayselect1 = overdue_dayselect1,
    @overdue_sel_obj = overdue_sel_obj,
    @overdue_sel_obj1 = overdue_sel_obj2,
    @overdue_sel_obj2 = overdue_sel_obj3
    from s_para 
    where belong = @belong--准备临时表
    create table #tmptable
    (
            t_id int,               --ID列,保证不重复
            smstype varchar(10),    --短信类型
            sms_send_time datetime, --短信预备发送时间,
            send_to varchar(10),    --接受对象
    handset varchar(11),
    content varchar(7900)
    )
    --把前一天没有发送成功的短信,从草稿箱获取再重新发送。重新发送后删除草稿箱内容
    insert into #tmptable
    select t_id,smstype,dateadd(day,1,sms_send_time),send_to,handset,content
    from s_draft
    where belong=@belongdelete from s_draft where belong=@belongdeclare @tmp_max_id int
    select @tmp_max_id=isnull(max(SISMSID),1)+1 from sms_outbox
    ----------------------------------进行公积金余额变动提醒:同时给单位及个人--------------------------
    --思路:第二天给前一天余额有变动的单位及人员发送短信提醒
    if @is_gjj_remind = '1'
    begin
    --1.给发送时间段设置值
    select  @sendtimebegin = convert(char(11),getdate(),121) + substring(b.sendtimeset,1,5) + ':00.000'
    from s_set_sendtime a,s_sendtime b 
    where a.sendtimeid = b.ID 
    and a.belong = b.belong
    and a.belong = @belong
    and smstype = '1' if convert(char(23),@sendtimebegin,121) = '' goto come_here_1

    --2.查询待发送的内容,包含关键字 经办人的短信
    select @sendtype = sendto, @smscontent = smscontent 
            from s_smsset 
            where smstype = '1' 
    and sendto = '1' 
    and status = '7' --终审通过的短信 
    and belong = @belong if isnull(@smscontent,'') = '' goto come_here_101 --无单位短信,跳转到给职工发短信

    --3.查询前一天有业务的 单位帐号,手机号码
    declare handset_cursor1 cursor for
    select  a.unit_account,'',a.handset 
            from m_unitinfo a
    where a.belong = @belong 
            and a.status < '2'
    and dbo.f_sms_checkphone(a.handset) = 1 --验证手机号是否合法的函数
    and a.unit_account not in ( select unit_account from s_disturb where type = '1') --1.经办人短信 2.缴存职工短信 3.贷款职工短信
            and a.unit_account in 
            (   select unit_account from m_unitbusi 
                where real_date >= convert(char(10),dateadd(day,-1,getdate()),121)
                and real_date < convert(char(10),getdate(),121)
                and belong = @belong            union             select unit_account from m_personbusi
                where real_date >= convert(char(10),dateadd(day,-1,getdate()),121)
                and real_date < convert(char(10),getdate(),121) 
                and belong = @belong
            ) open handset_cursor1
    fetch next from handset_cursor1 into @unit_account,@person_account,@handset
    while @@fetch_status = 0
    begin
    --发送内容中的关键字替换
    exec sms_get_content_by_keyword @sendtype,@smscontent,@belong,@unit_account,@person_account,@content output --考虑频繁访问MAS服务器数据库,造成速度无比慢,先把待发送的手机号和内容插入到临时表中,同一数据库中插入数据,速度快多了,然后在下面一次性访问MAS服务器数据库即可,能节省点时间。
    insert into #tmptable(t_id,smstype,sms_send_time,send_to,handset,content) 
                    values (@tmp_max_id,'1',@sendtimebegin,'1',@handset,@content)                set @tmp_max_id = @tmp_max_id + 1    fetch next from handset_cursor1 into @unit_account,@person_account,@handset
    end
    close handset_cursor1
    deallocate handset_cursor1 come_here_101: --2.查询待发送的内容,包含关键字 缴存职工的短信
    select @sendtype = sendto, @smscontent = smscontent 
            from s_smsset 
            where smstype = '1' 
    and sendto = '2' 
    and status = '7'
    and belong = @belong  if isnull(@smscontent,'') = '' goto come_here_1

    --3.查询前一日有余额变动【有缴存的或有(2支取 3销户 4冲支取 7年度利息)】的公积金缴存职工的单位帐号,个人帐号,手机号码
    declare handset_cursor2 cursor for select distinct a.unit_account,a.person_account,c.handset 
    from m_unitbusidetail a, m_unitbusi b, m_personinfo c
    where a.belong = b.belong and a.unit_account = b.unit_account and a.busi_id = b.busi_id
            and a.belong = c.belong and a.unit_account = c.unit_account and a.person_account = c.person_account
            and a.belong = @belong
            and dbo.f_sms_checkphone(c.handset) = 1
    and b.real_date >= convert(char(10),dateadd(day,-1,getdate()),121)
    and b.real_date < convert(char(10),getdate(),121)
    and a.unit_account+a.person_account not in (select unit_account+person_account from s_disturb where belong = @belong and type = '2')--1.经办人短信2.缴存职工短信3.贷款职工短信

            union select distinct a.unit_account,a.person_account,b.handset 
    from m_personbusi a, m_personinfo b 
    where a.belong = b.belong and a.unit_account = b.unit_account and a.person_account = b.person_account
            and dbo.f_sms_checkphone(b.handset) = 1
    and a.busi_type in ('2','3','4','7')--2支取 3销户 4冲支取 7年度利息
    and a.belong = @belong
    and a.real_date >= convert(char(10),dateadd(day,-1,getdate()),121)
    and a.real_date < convert(char(10),getdate(),121)
    and a.unit_account + a.person_account
     not in (select unit_account+person_account from s_disturb where belong = @belong and type = '2')--1.经办人短信2.缴存职工短信3.贷款职工短信 open handset_cursor2
    fetch next from handset_cursor2 into @unit_account,@person_account,@handset
    while @@fetch_status = 0
    begin
    --发送内容中的关键字替换
    exec sms_get_content_by_keyword @sendtype,@smscontent,@belong,@unit_account,@person_account,@content output --考虑频繁访问MAS服务器数据库,造成速度无比慢,先把待发送的手机号和内容插入到临时表中,同一数据库中插入数据,速度快多了,然后在下面一次性访问MAS服务器数据库即可,能节省点时间。
    insert into #tmptable(t_id,smstype,sms_send_time,send_to,handset,content) 
                    values (@tmp_max_id,'1',@sendtimebegin,'2',@handset,@content)                set @tmp_max_id = @tmp_max_id + 1    fetch next from handset_cursor2 into @unit_account,@person_account,@handset
    end
    close handset_cursor2
    deallocate handset_cursor2end
      

  3.   


    ------------------------------进行公积金定时提醒-------------------------------------------------------
    --目前的思路:每月/两月 n日给上月/上两月余额有变动的人员发送提醒信息come_here_1:  --上面参数如果不设置的话,跳转到这里来if @is_gjj_month_remind = '1'
    begin
    if isnull(@gjj_day,0) = 0 or isnull(@gjj_monthselect,'') = '' goto come_here_2

    declare @send_date varchar(10)--发送的日期 --1.给发送时间段设置值
    if @gjj_monthselect = '1'--每月(每月发送一次)
    begin
    set @send_date = convert(char(8),getdate(),121) 
    + case  when len(@gjj_day) = 1 then '0' + cast(@gjj_day as varchar) 
    else cast(@gjj_day as varchar) 
    end
    end
    else if @gjj_monthselect = '2'--每两月(2,4,6,8,10,12月发送一次)
    begin
    set @send_date = case 
                                     when month(getdate()) in (2,4,6,8,10,12) 
     then convert(char(8),getdate(),121) 
     + case when len(@gjj_day) = 1 then '0' + cast(@gjj_day as varchar) 
    else cast(@gjj_day as varchar) 
    end
     else ''
     end
    end
    else if @gjj_monthselect = '3'--每季度(1,4,7,10月发送一次)
    begin
    set @send_date = case when month(getdate()) in (1,4,7,10) 
    then convert(char(8),getdate(),121) 
    + case  when len(@gjj_day) = 1 then '0' + cast(@gjj_day as varchar) 
    else cast(@gjj_day as varchar) 
    end
    else ''
    end
    end
    else if @gjj_monthselect = '4'--每半年(每年的1月和7月份分别发送一次)
    begin
    set @send_date = case when month(getdate()) in (1,7) 
    then convert(char(8),getdate(),121) 
    + case  when len(@gjj_day) = 1 then '0' + cast(@gjj_day as varchar) 
    else cast(@gjj_day as varchar) 
    end
     else ''
     end
    end
    else if @gjj_monthselect = '5'--每年(每年的7月份发送一次)
    begin
    set @send_date = '2012-07-'
    + case  when len(@gjj_day) = 1 then '0' + cast(@gjj_day as varchar) 
    else cast(@gjj_day as varchar) 
    end
    end        --如果发送日不是今天,则跳转到下一项
            if @send_date <> convert(char(10),getdate(),121) goto come_here_2  --用上面求和的日期+发送时间段,得到具体的发送时间
    select  @sendtimebegin = @send_date + ' ' + substring(b.sendtimeset,1,5) + ':00.000'
    from s_set_sendtime a,s_sendtime b 
            where a.sendtimeid = b.ID
    and a.belong = b.belong
    and a.belong = @belong
    and smstype = '2' if convert(char(23),@sendtimebegin,121) = '' goto come_here_2

    --2.查询待发送的内容,包含关键字  经办人
    select @sendtype = sendto, @smscontent = smscontent 
            from s_smsset 
            where smstype = '2' 
    and sendto = '1' 
    and status = '7' 
    and belong = @belong

    if isnull(@smscontent,'') = '' goto come_here_201

    --3.查询发送周期内有发生业务的单位帐号,手机号码
    declare handset_cursor3 cursor for
    select  unit_account,'',handset from m_unitinfo a
    where a.belong = @belong 
            and a.status < '2'
    and dbo.f_sms_checkphone(a.handset) = 1 --验证手机号是否合法的函数
    and a.unit_account not in (select unit_account from s_disturb where belong = @belong and type = '1')--1.经办人短信2.缴存职工短信3.贷款职工短信
            and a.unit_account in 
            (   select unit_account from m_unitbusi 
                where real_date >= case @gjj_monthselect 
                                   when '1' then convert(char(7),dateadd(month,-1,getdate()),121) + '-01' --每月
                                   when '2' then convert(char(7),dateadd(month,-2,getdate()),121) + '-01' --每两月                                when '3' then convert(char(7),dateadd(month,-3,getdate()),121) + '-01' --每季
                                   when '4' then convert(char(7),dateadd(month,-6,getdate()),121) + '-01' --每半年
                                   when '5' then convert(char(7),dateadd(month,-12,getdate()),121) + '-01' --每年
                                   else convert(char(7),getdate(),121) + '-01' end
                and real_date < convert(char(7),getdate(),121) + '-01' --上月底之前
                and belong = @belong            union             select unit_account from m_personbusi
                where real_date >= case @gjj_monthselect 
                                   when '1' then convert(char(7),dateadd(month,-1,getdate()),121) + '-01' --每月
                                   when '2' then convert(char(7),dateadd(month,-2,getdate()),121) + '-01' --每两月
                                   when '3' then convert(char(7),dateadd(month,-3,getdate()),121) + '-01' --每季
                                   when '4' then convert(char(7),dateadd(month,-6,getdate()),121) + '-01' --每半年
                                   when '5' then convert(char(7),dateadd(month,-12,getdate()),121) + '-01' --每年
                                   else convert(char(7),getdate(),121) + '-01' end
                and real_date < convert(char(7),getdate(),121) + '-01' --上月底之前
                and belong = @belong
            ) open handset_cursor3
    fetch next from handset_cursor3 into @unit_account,@person_account,@handset
    while @@fetch_status = 0
    begin
    --发送内容中的关键字替换
    exec sms_get_content_by_keyword @sendtype,@smscontent,@belong,@unit_account,@person_account,@content output --考虑频繁访问MAS服务器数据库,造成速度无比慢,先把待发送的手机号和内容插入到临时表中,同一数据库中插入数据,速度快多了,然后在下面一次性访问MAS服务器数据库即可,能节省点时间。
    insert into #tmptable(t_id,smstype,sms_send_time,send_to,handset,content) 
                    values (@tmp_max_id,'2',@sendtimebegin,'1',@handset,@content)                set @tmp_max_id = @tmp_max_id + 1    fetch next from handset_cursor3 into @unit_account,@person_account,@handset
    end
    close handset_cursor3
    deallocate handset_cursor3come_here_201: --2.查询待发送的内容,包含关键字
    select @sendtype = sendto, @smscontent = smscontent 
            from s_smsset 
            where smstype = '2' 
    and sendto = '2' 
    and status = '7'
    and belong = @belong

    if isnull(@smscontent,'') = '' goto come_here_2

    --3.查询本周期内有公积金业务的职工的单位帐号,个人帐号,手机号码
    declare handset_cursor4 cursor for
    select  a.unit_account,a.person_account,a.handset 
            from m_personinfo a
    where a.belong = @belong 
    and dbo.f_sms_checkphone(a.handset) = 1 --验证手机号是否合法的函数
    and a.unit_account + a.person_account not in (select unit_account+person_account from s_disturb where belong = @belong and type = '2')--1.经办人短信2.缴存职工短信3.贷款职工短信
            and a.unit_account + a.person_account in 
            (   select a.unit_account + a.person_account 
                from m_unitbusidetail a, m_unitbusi b 
                where a.unit_account = b.unit_account and a.busi_id = b.busi_id
                and b.real_date >= case @gjj_monthselect 
                                   when '1' then convert(char(7),dateadd(month,-1,getdate()),121) + '-01' --每月
                                   when '2' then convert(char(7),dateadd(month,-2,getdate()),121) + '-01' --每两月
                                   when '3' then convert(char(7),dateadd(month,-3,getdate()),121) + '-01' --每季
                                   when '4' then convert(char(7),dateadd(month,-6,getdate()),121) + '-01' --每半年
                                   when '5' then convert(char(7),dateadd(month,-12,getdate()),121) + '-01' --每年
                                   else convert(char(7),getdate(),121) + '-01' end
                and b.real_date < convert(char(7),getdate(),121) + '-01' --上月底之前
                and b.belong = @belong            union             select unit_account + person_account from m_personbusi
                where 
    busi_type in ( '2','3','4','7' ) --2支取 3销户 4冲支取 7年度利息 
                and real_date >= case @gjj_monthselect 
                                   when '1' then convert(char(7),dateadd(month,-1,getdate()),121) + '-01' --每月
                                   when '2' then convert(char(7),dateadd(month,-2,getdate()),121) + '-01' --每两月
                                   when '3' then convert(char(7),dateadd(month,-3,getdate()),121) + '-01' --每季
                                   when '4' then convert(char(7),dateadd(month,-6,getdate()),121) + '-01' --每半年
                                   when '5' then convert(char(7),dateadd(month,-12,getdate()),121) + '-01' --每年
                                   else convert(char(7),getdate(),121) + '-01' end
                and real_date < convert(char(7),getdate(),121) + '-01' --上月底之前
                and belong = @belong
            ) open handset_cursor4
    fetch next from handset_cursor4 into @unit_account,@person_account,@handset
    while @@fetch_status = 0
    begin
    --发送内容中的关键字替换
    exec sms_get_content_by_keyword @sendtype,@smscontent,@belong,@unit_account,@person_account,@content output --考虑频繁访问MAS服务器数据库,造成速度无比慢,先把待发送的手机号和内容插入到临时表中,同一数据库中插入数据,速度快多了,然后在下面一次性访问MAS服务器数据库即可,能节省点时间。
    insert into #tmptable(t_id,smstype,sms_send_time,send_to,handset,content) 
                    values (@tmp_max_id,'2',@sendtimebegin,'2',@handset,@content)                set @tmp_max_id = @tmp_max_id + 1    fetch next from handset_cursor4 into @unit_account,@person_account,@handset
    end
    close handset_cursor4
    deallocate handset_cursor4
    end
      

  4.   

    ALTER PROCEDURE sms_behind_sendsms @belong SMALLINT
    AS --思路:定时任务每天调度此任务,判断当天是否有需要发送的数据,有的话就写入发送表中
    --为了防止手动启动本任务,造成多次发送某类数据,因此发送后需记录日期,发送前需进行日期判断
        IF EXISTS ( SELECT TOP 1
                            sms_count
                    FROM    s_auto_send_log
                    WHERE   CONVERT(CHAR(10), real_time, 121) = CONVERT(CHAR(10), GETDATE(), 121)
                            AND belong = @belong )
            RETURN
    /*----------重要的表结构-------------------
    select * from s_disturb       --免打扰名单
    select * from s_smsset        --短信表
    select * from s_keyword       --关键字表
    select * from s_sendtime      --时间段设置表
    select * from s_set_sendtime  --各种短信发送时间段设置表
    select * from s_para          --发送短信的参数设置表
    ------------------------------------------*/
    ----------------------------------------------------------------------------------------------------------
        DECLARE @is_gjj_remind CHAR(1) ,       --是否进行“公积金余额变动提醒” 1是0否
            @is_gjj_month_remind CHAR(1) , --是否进行“公积金【每月】【15】日定时提醒” 1是0否
            @gjj_monthselect CHAR(1) ,     --【每月】选择参数 1每月2每两月3每季度4每半年5每年 这些值是事先约定好的
            @gjj_day INT ,                 --【15】的输入参数 输入范围:1-28(一个月最少的天数)
            @is_dk_remind CHAR(1) ,        --是否进行“贷款余额变动提醒” 1是0否
            @is_repay_mind CHAR(1) ,       --是否进行“应于还款【10】日前提醒” 1是0否
            @repay_day INT ,               --【10】的输入参数 输入范围:无限制
            @is_overdue_remind CHAR(1) ,   --是否进行“逾期【7】日以上【每日】催收”
            @overdue_day INT ,             --【7】的输入参数 输入范围:无限制
            @overdue_dayselect CHAR(1) ,   --【每日】的选择参数 1每日2每周3每两周4每月
            @sendmode CHAR(1) ,       --发送模式0中心统一发送1各管理部分别发送
            @is_gjj_day_remind CHAR(1) ,   --是否进行“公积金【3】日以上余额无变动人员,给【单位】【职工】【单位及职工】,发送催收短信”
            @gjj_day2 INT ,       --【3】的输入参数
            @sel_obj CHAR(1) ,       --【单位】【职工】【单位及职工】选择参数,0单位1职工2单位及职工
            @is_overdue_remind_long CHAR(1) ,--是否进行“长期逾期:逾期【30】日以上【每日 每周 每两周 每月】催收,向贷款人及【配偶】【担保人】【所在单位】,发送催收短信”
            @overdue_day1 INT , --【30】的输入参数
            @overdue_dayselect1 CHAR(1) , --【每日】的选择参数 1每日2每周3每两周4每月
            @overdue_sel_obj CHAR(1) , --【配偶】的选择参数
            @overdue_sel_obj1 CHAR(1) , --【担保人】的选择参数
            @overdue_sel_obj2 CHAR(1) --【所在单位】的选择参数    DECLARE @sendtimebegin DATETIME ,     --发送时间的开始
            @handset CHAR(11) ,           --手机号码
            @smscontent VARCHAR(8000) ,   --发送内容,包含关键字
            @content VARCHAR(8000)       --发送内容,不包含关键字    DECLARE @unit_account CHAR(13) ,
            @person_account CHAR(8) ,
            @sendtype CHAR(1)--发送类型【1单位经办人 2缴存职工 3贷款职工】--查询相关的参数值
        SELECT  @is_gjj_remind = is_gjj_remind ,
                @is_gjj_month_remind = is_gjj_month_remind ,
                @gjj_monthselect = gjj_monthselect ,
                @gjj_day = gjj_day ,
                @is_dk_remind = is_dk_remind ,
                @is_repay_mind = is_repay_remind ,
                @repay_day = repay_day ,
                @is_overdue_remind = is_overdue_remind ,
                @overdue_day = overdue_day ,
                @overdue_dayselect = overdue_dayselect ,
                @sendmode = sendmode ,
                @is_gjj_day_remind = is_gjj_day_remind ,
                @gjj_day2 = gjj_day2 ,
                @sel_obj = sel_obj ,
                @is_overdue_remind_long = is_overdue_remind_long ,
                @overdue_day1 = overdue_day1 ,
                @overdue_dayselect1 = overdue_dayselect1 ,
                @overdue_sel_obj = overdue_sel_obj ,
                @overdue_sel_obj1 = overdue_sel_obj2 ,
                @overdue_sel_obj2 = overdue_sel_obj3
        FROM    s_para
        WHERE   belong = @belong--准备临时表
    IF OBJECT_ID('tempdb..#tmptable','u')IS NULL 加一段这个
    BEGIN
        CREATE TABLE #tmptable
            (
              t_id INT ,               --ID列,保证不重复
              smstype VARCHAR(10) ,    --短信类型
              sms_send_time DATETIME , --短信预备发送时间,
              send_to VARCHAR(10) ,    --接受对象
              handset VARCHAR(11) ,
              content VARCHAR(7900)
            )
    END 
    --把前一天没有发送成功的短信,从草稿箱获取再重新发送。重新发送后删除草稿箱内容
        INSERT  INTO #tmptable
                SELECT  t_id ,
                        smstype ,
                        DATEADD(day, 1, sms_send_time) ,
                        send_to ,
                        handset ,
                        content
                FROM    s_draft
                WHERE   belong = @belong    DELETE  FROM s_draft
        WHERE   belong = @belong    DECLARE @tmp_max_id INT
        SELECT  @tmp_max_id = ISNULL(MAX(SISMSID), 1) + 1
        FROM    sms_outbox
    ----------------------------------进行公积金余额变动提醒:同时给单位及个人--------------------------
    --思路:第二天给前一天余额有变动的单位及人员发送短信提醒
        IF @is_gjj_remind = '1'
            BEGIN
    --1.给发送时间段设置值
                SELECT  @sendtimebegin = CONVERT(CHAR(11), GETDATE(), 121)
                        + SUBSTRING(b.sendtimeset, 1, 5) + ':00.000'
                FROM    s_set_sendtime a ,
                        s_sendtime b
                WHERE   a.sendtimeid = b.ID
                        AND a.belong = b.belong
                        AND a.belong = @belong
                        AND smstype = '1'            IF CONVERT(CHAR(23), @sendtimebegin, 121) = ''
                    GOTO come_here_1--2.查询待发送的内容,包含关键字 经办人的短信
                SELECT  @sendtype = sendto ,
                        @smscontent = smscontent
                FROM    s_smsset
                WHERE   smstype = '1'
                        AND sendto = '1'
                        AND status = '7' --终审通过的短信 
                        AND belong = @belong            IF ISNULL(@smscontent, '') = ''
                    GOTO come_here_101 --无单位短信,跳转到给职工发短信 --3.查询前一天有业务的 单位帐号,手机号码
                DECLARE handset_cursor1 CURSOR
                FOR
                    SELECT  a.unit_account ,
                            '' ,
                            a.handset
                    FROM    m_unitinfo a
                    WHERE   a.belong = @belong
                            AND a.status < '2'
                            AND dbo.f_sms_checkphone(a.handset) = 1 --验证手机号是否合法的函数
                            AND a.unit_account NOT IN ( SELECT  unit_account
                                                        FROM    s_disturb
                                                        WHERE   type = '1' ) --1.经办人短信 2.缴存职工短信 3.贷款职工短信
                            AND a.unit_account IN (
                            SELECT  unit_account
                            FROM    m_unitbusi
                            WHERE   real_date >= CONVERT(CHAR(10), DATEADD(day, -1,
                                                                  GETDATE()), 121)
                                    AND real_date < CONVERT(CHAR(10), GETDATE(), 121)
                                    AND belong = @belong
                            UNION
                            SELECT  unit_account
                            FROM    m_personbusi
                            WHERE   real_date >= CONVERT(CHAR(10), DATEADD(day, -1,
                                                                  GETDATE()), 121)
                                    AND real_date < CONVERT(CHAR(10), GETDATE(), 121)
                                    AND belong = @belong )            OPEN handset_cursor1
                FETCH NEXT FROM handset_cursor1 INTO @unit_account,
                    @person_account, @handset
                WHILE @@fetch_status = 0
                    BEGIN
    --发送内容中的关键字替换
                        EXEC sms_get_content_by_keyword @sendtype, @smscontent,
                            @belong, @unit_account, @person_account,
                            @content OUTPUT
    代码过长我后面没贴
      

  5.   

    还有你的那些if/else,while的,都加上begin end包住,特别是if /else,如果不加begin/end,这个对接下来的第一句有效,对第二句开始就无效
      

  6.   

    begin/end这个我知道,我只是在只执行if else下面一句的情况才不加begin end的。
      

  7.   

    我来猜猜看:
    你的存储过程会创建一个临时表 #tmptable,插入数据。
    然后调用这会向 s_auto_send_log 插入日志,标记存储过程被执行过了,并且会读取 #tmptable 表的数据做某些处理。那么好玩的来了。
    如果上层调用者还没有处理完 #tmptable 表的数据,连接意外断开了,整个 #tmptable 表就没了。
    然后试图再调用存储过程,日志表已经有记录了,直接跳出,那么  #tmptable 表没有被创建,上层调用者试图从 #tmptable 表取数就会出提示对象名“#tmptable”无效的错误了。
      

  8.   

    最后是会向s_auto_send_log插入日志,但是s_auto_send_log表在建立定时任务的时候是没有数据的。
      

  9.   

    对象名“#tmptable”无效 应该不是sp内而是上层调用者的错误吧。
    那么很显然在某种条件下sp没有创建#tmptable。
      

  10.   

    存储过程我把开始的判断删除了还是不行。
    现在存储过程内除了开始定义的变量以及变量赋值,紧接着就是创建#tmptable这个临时表了。
    不过还是不能新建包,依旧提示“对象名‘#tmptable’无效”
      

  11.   

    你会不会上层调用跳过了sp?
    加调试 PRINT 吧。
      

  12.   

    个人不建议把复杂的sql搞成job, 难以维护高度, 最好是写代码做成Windows服务如果你一定要做job, 可以把中间结果放在一个调试专用的表中保存……