CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  应用实例

高分求一个简单存储过程,在线等,立节

楼主huangyan229()2005-01-18 11:49:07 在 MS-SQL Server / 应用实例 提问

非常简单,由于我没写过存储过程,特求教于大家  
   
  select   distinct   mt.update_time,mt.pnc,mt.mt_code,i.subgroup,i.[supplier   code]  
    from   mt   LEFT   JOIN   iniqessubg   as   i   ON   i.pnc=mt.pnc   where      
  DATEPART(yy,mt.update_time)=?    
  and   DATEPART(mm,mt.update_time)=?    
  ?为参数  
  将上面的选择结果插入到Model_File表中,插入过程中,如果ModelFile表中存在相应纪录则更新,  
  否则插入新纪录,判断条件是在Model_File中PNC是否已存在  
   
  在线等,立节 问题点数:100、回复次数:9Top

1 楼daijingjie2002(艰苦创业)回复于 2005-01-18 11:53:51 得分 5

insert   into   Model_File  
  select   distinct   mt.update_time,mt.pnc,mt.mt_code,i.subgroup,i.[supplier   code]  
    from   mt   LEFT   JOIN   iniqessubg   as   i   ON   i.pnc=mt.pnc   where      
  DATEPART(yy,mt.update_time)=?    
  and   DATEPART(mm,mt.update_time)=?    
                                      and   pnc   not   in(select   pnc   from   Model_File)Top

2 楼huangyan229()回复于 2005-01-18 11:55:23 得分 0

您这个只有插入没有更新阿Top

3 楼pbsql(风云)回复于 2005-01-18 12:00:51 得分 30

update   Model_File  
      set   update_time=t.update_time,mt_code=t.mt_code,subgroup=t.subgroup,supplier   code=t.supplier   code  
      from  
  (  
  select   distinct   mt.update_time,mt.pnc,mt.mt_code,i.subgroup,i.[supplier   code]  
    from   mt   LEFT   JOIN   iniqessubg   as   i   ON   i.pnc=mt.pnc   where      
  DATEPART(yy,mt.update_time)=?    
  and   DATEPART(mm,mt.update_time)=?  
  )   t  
      where   Model_File.PNC=t.PNC  
   
  insert   into   Model_File(update_time,pnc,mt_code,subgroup,supplier   code)  
  select   distinct   mt.update_time,mt.pnc,mt.mt_code,i.subgroup,i.[supplier   code]  
    from   mt   LEFT   JOIN   iniqessubg   as   i   ON   i.pnc=mt.pnc   where      
  DATEPART(yy,mt.update_time)=?    
  and   DATEPART(mm,mt.update_time)=?    
      and   not   exists(select   *   from   Model_File   where   Model_File.PNC=mt.PNC)  
  Top

4 楼huangyan229()回复于 2005-01-18 12:22:27 得分 0

忘了写,希望又返回值,成功为1失败为0Top

5 楼xluzhong(Ralph)回复于 2005-01-18 12:37:27 得分 10

create   procedure   test  
                @test1     nvarchar(100),  
                @test2     nvarchar(100)  
  as  
      if   exists(select   *   from   ModelFile   where   条件   )  
          begin  
                update   ModelFile  
                set   update_time=mt.update_time,  
                        pnc=mt.pnc,  
                        mt_code=mt.mt_code,  
                        subgroup=i.subgroup,  
                        [supplier   code]=i.[supplier   code]  
                from   ModelFile  
                LEFT   JOIN   iniqessubg   as   i   ON   i.pnc=mt.pnc   where      
  DATEPART(yy,mt.update_time)=@test1    
  and   DATEPART(mm,mt.update_time)=@test2  
                 
          end    
      else  
          begin  
                insert   into   ModelFile(update_time,pnc,mt_code,subgroup,[supplier   code])  
                select   distinct   mt.update_time,mt.pnc,mt.mt_code,i.subgroup,i.[supplier   code]  
    from   mt   LEFT   JOIN   iniqessubg   as   i   ON   i.pnc=mt.pnc   where      
  DATEPART(yy,mt.update_time)=@test1    
  and   DATEPART(mm,mt.update_time)=@test2    
          end  
  goTop

6 楼camel_yang(骆驼)回复于 2005-01-18 12:37:39 得分 30

--参考pbsql(风云)的,写成存储过程,加上判断,加上年/月参数  
   
  CREATE   PROCEDURE   proc_name  
  @intYear   int   =   2005,  
  @intMonth   int=1  
  AS  
   
  if   not   Exist(select   distinct   mt.update_time,mt.pnc,mt.mt_code,i.subgroup,i.[supplier   code]  
    from   mt   LEFT   JOIN   iniqessubg   as   i   ON   i.pnc=mt.pnc   where      
  DATEPART(yy,mt.update_time)=@intYear    
  and   DATEPART(mm,mt.update_time)=@intMonth   )  
  begin    
    select     '没有满足条件的记录'  
    return   0  
  end  
  Else  
   
  update   Model_File  
      set   update_time=t.update_time,mt_code=t.mt_code,subgroup=t.subgroup,supplier   code=t.supplier   code  
      from  
  (  
  select   distinct   mt.update_time,mt.pnc,mt.mt_code,i.subgroup,i.[supplier   code]  
    from   mt   LEFT   JOIN   iniqessubg   as   i   ON   i.pnc=mt.pnc   where      
  DATEPART(yy,mt.update_time)=@intYear    
  and   DATEPART(mm,mt.update_time)=@intMonth  
  )   t  
      where   Model_File.PNC=t.PNC  
   
  if   @@ErrorCode<>0  
  begin    
          select   '不能更新model_File',   @ErrorCode  
          return   0  
  end    
   
  insert   into   Model_File(update_time,pnc,mt_code,subgroup,supplier   code)  
  select   distinct   mt.update_time,mt.pnc,mt.mt_code,i.subgroup,i.[supplier   code]  
    from   mt   LEFT   JOIN   iniqessubg   as   i   ON   i.pnc=mt.pnc   where      
  DATEPART(yy,mt.update_time)=@intYear    
  and   DATEPART(mm,mt.update_time)=@intMonth    
      and   not   exists(select   *   from   Model_File   where   Model_File.PNC=mt.PNC)  
   
  if   @@ErrorCode<>0  
  begin    
          select   '不能插入model_File',   @ErrorCode  
          return   0  
  endTop

7 楼gsh945(太平洋底)回复于 2005-01-18 12:58:17 得分 5

治标不治本::看下面的  
  语法  
  CREATE   PROC   [   EDURE   ]   procedure_name   [   ;   number   ]  
          [   {   @parameter   data_type   }  
                  [   VARYING   ]   [   =   default   ]   [   OUTPUT   ]  
          ]   [   ,...n   ]    
   
  [   WITH  
          {   RECOMPILE   |   ENCRYPTION   |   RECOMPILE   ,   ENCRYPTION   }   ]    
   
  [   FOR   REPLICATION   ]    
   
  AS   sql_statement   [   ...n   ]    
   
   
  例子:  
  CREATE   PROCEDURE   sp_showindexes  
        @@TABLE   varchar(30)   =   'sys%'  
  AS    
  SELECT   o.name   AS   TABLE_NAME,  
        i.name   AS   INDEX_NAME,    
        indid   AS   INDEX_ID  
  FROM   sysindexes   i   INNER   JOIN   sysobjects   o  
        ON   o.id   =   i.id    
  WHERE   o.name   LIKE   @@TABLE  
  GO                    
  USE   pubs  
  EXEC   sp_showindexes   'emp%'  
  GO  
  Top

8 楼Littleming(小明)回复于 2005-01-18 13:22:56 得分 20

--   上面的做法效率不太高,只能分两步  
  --   1.插入:你要先mt对照Model_File,再插入在Model_File没有的记录  
  --   2.更新:满足条件就更新  
  declare   proc   pro_insert_or_update_records  
  (@year   int,@month   int)  
  as  
  begin  
  --   insert  
  insert   into   Model_File  
  select   distinct   mt.update_time   update_time,mt.pnc,mt.mt_code,i.subgroup,i.[supplier   code]  
  from   (select   *   from   mt   where   PNC   not   in(select   PNC   from   Model_File))   mt   LEFT   JOIN   iniqessubg   as   i   ON   i.pnc=mt.pnc   where      
  DATEPART(yy,mt.update_time)=@year  
  and   DATEPART(mm,mt.update_time)=@year  
  --   update  
  update   Model_File  
  set   update_time=aaa.update_time,mt_code=aaa.mt_code,subgroup=aaa.subgroup,  
  [supplier   code]=aaa.[supplier   code]   from  
  (select   distinct   mt.update_time   update_time,mt.pnc,mt.mt_code,i.subgroup,i.[supplier   code]  
  from   (select   *   from   mt   where   PNC   in(select   PNC   from   Model_File))   mt   LEFT   JOIN   iniqessubg   as   i   ON   i.pnc=mt.pnc   where      
  DATEPART(yy,mt.update_time)=@year  
  and   DATEPART(mm,mt.update_time)=@year)   aaa    
  where   Model_File.PNC=aaa.PNC  
  end  
  Top

9 楼huangyan229()回复于 2005-01-18 14:48:43 得分 0

看来只能分两步走了Top

相关问题

  • 一个简单的存储过程的问题,在线等待!
  • 求一简单存储过程.在线等,马上结帖!!!!
  • 存储过程的的简单问题,在线等待!
  • 存储过程简单问题:在线等
  • 简单的存储过程!
  • 一简单存储过程
  • 简单存储过程
  • 存储过程,在线等!!!!!!!!!
  • 在线求存储过程!?
  • 问一个存储过程的简单问题,在线等,立即揭帖

关键词

  • code
  • pnc
  • mt
  • subgroup
  • supplier
  • 插入
  • model
  • datepart
  • update
  • distinct

得分解答快速导航

  • 帖主:huangyan229
  • daijingjie2002
  • pbsql
  • xluzhong
  • camel_yang
  • gsh945
  • Littleming

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo