设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1106|回复: 4
打印 上一主题 下一主题

SQL 储存过程通过查询时时更新数据到另一张表中

[复制链接]
跳转到指定楼层
1#
发表于 2022-3-30 10:44:10 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:                <Author,,Name>
-- Create date: <Create Date,,>
-- Description:        <Description,,>
-- =============================================
CREATE PROCEDURE sp_insert_sc_xd_wltgb
@ID int,
@kf varchar(15),
@cpdm varchar(40),
@cplh varchar(40),
@mh  varchar(15),
@scxd int,
@gdbh varchar(40),
@gdzq datetime,
@xdfl varchar(6),
@wldm varchar(45),
@wlmc varchar(50),
@wlgg varchar(50),
@lb varchar(10),
@xdyll int,
@rtn int output       
AS
declare
@tID int,
@tkf varchar(15),
@tcpdm varchar(40),
@tcplh varchar(40),
@tmh  varchar(15),
@tscxd int,
@tgdbh varchar(40),
@tgdzq datetime,
@txdfl varchar(6),
@twldm varchar(45),
@twlmc varchar(50),
@twlgg varchar(50),
@tlb varchar(10),
@txdyll int
SELECT * INTO #TT
FROM
(SELECT   a.ID, a.kf, a.cpdm, a.cplh, a.mh,a.scxd,   
           a.gdbh, a.gdzq, a.xdfl,d.wldm, d.wlmc,            
                     d.wlgg, d.lb, CEILING(ISNULL(a.scxd + 0.00, 1) * ISNULL(d.dwylb, 0)      
                                    ) AS xdyll
FROM       dbo.sc_xd_tgmxb as a INNER JOIN
                dbo.BOM_xxb as d ON a.cplh = d.cplh AND a.mh = d.mh WHERE   a.ly ='YES')as aa

  if exists(SELECT  ID, kf,cpdm, cplh, mh,scxd,
               gdbh, gdzq, xdfl,wldm, wlmc,
                wlgg, lb,  xdyll
FROM       #TT
where ID=@ID )
BEGIN
        SELECT  @TID=ID,@tkf=kf,@tcpdm= cpdm,@tcplh= cplh,@tmh=mh,@tscxd=scxd,
               @tgdbh=gdbh, @tgdzq=gdzq, @txdfl=xdfl,@twldm=wldm, @twlmc=wlmc,
                @twlgg=wlgg, @tlb=lb,@txdyll= xdyll
                FROM  #TT
                                where ID=@ID
         if ((@TID=@ID) and (@tkf=@kf) and (@tcpdm= @cpdm) and (@tcplh= @cplh) and (@tmh=@mh) and (@tscxd=@scxd)
          
              and (@tgdbh=@gdbh) and (@tgdzq=@gdzq) and (@txdfl=@xdfl) and (@twldm=@wldm) and (@twlmc=@wlmc) and
               ( @twlgg=@wlgg) and (@tlb=@lb) and (@txdyll= @xdyll))
                        begin
                               set @rtn = 0
                                       end
                                         else
                        begin
                                update sc_xd_wltgb set ID=@ID,kf=@kf,cpdm= @cpdm,cplh= @cplh,mh=@mh,scxd=@scxd,
               gdbh=@gdbh, gdzq=@gdzq, xdfl=@xdfl,wldm=@wldm, wlmc=@wlmc,
                wlgg=@wlgg, lb=@lb,xdyll=@xdyll where ID=@ID
                                 set @rtn = 2
                                       end
                                           end
                                         else
                        begin
                        insert into sc_xd_wltgb  values (@ID, @kf,@cpdm, @cplh, @mh,@scxd, @gdbh, @gdzq, @xdfl,@wldm, @wlmc, @wlgg, @lb,  @xdyll)
                        set @rtn = 1

END
GO
高手们帮助看看,储存过程+临时表,未能更新数据,新手别喷!
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2022-3-30 10:45:52 | 显示全部楼层
本帖最后由 天涯沦落20131 于 2022-3-30 17:57 编辑

说明一下 :                           1、有3张表:表1(a.ID, a.kf, a.cpdm, a.cplh, a.mh,a.scxd,a.gdbh, a.gdzq, a.xdfl  from dbo.sc_xd_tgmxb as a)---下单
                                                 表2(d.wldm, d.wlmc, d.wlgg, d.lb,d.dwyl       from dbo.BOM_xxb as d )---BOM
                                                  表3(ID, kf,cpdm, cplh, mh,scxd, gdbh, gdzq, xdfl,wldm, wlmc,wlgg, lb,  xdyll from sc_xd_wltgb)物料表
                            2、通过查询表1,表2 更新数据到表3
  遇到问题:
                           1、表2 BOM 是多款物料对一个表1定单,通过查询会产生多条相同的数据
                           2、查询无主键,插入新表会产生重得数据
                           3、做到订单下单时时更新到表3
以上敬请高手指点!非常感谢!                        


回复 支持 0 反对 1

使用道具 举报

3#
 楼主| 发表于 2022-4-15 12:16:25 | 显示全部楼层
roych 发表于 2022-4-3 12:13
答复如下(见红褐色字体):
1、表2 BOM 是多款物料对一个表1定单,通过查询会产生多条相同的数据
BOM表 ...

感谢你的回复!access+SQL server 做的内部小系统,已能实现自已想要的数据统计功能,感谢这个平台,也感谢你一如既往的支持;小白自搞一波三折,遇到了很多问题!不在网上救助衬步难行!我也想了一些办法,防重得插入增加了一些条件!
insert into by_sc_data.dbo.sc_xd_wltgb( kf, cpdm, cplh, mh,scxd,
               gdbh, gdzq, xdfl,wldm, wlmc,
                wlgg, lb, xdyll)
select * from by_sc_data.dbo.sc_xd_wlcx  a  
where not exists(select * from by_sc_data.dbo.sc_xd_wltgb b where b.cplh=a.cplh and b.gdbh=a.gdbh and b.scxd=a.scxd and
b.mh=a.mh and b.wldm=a.wldm and b.xdyll=a.xdyll and b.gdzq<=a.gdzq)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-6-1 21:45 , Processed in 0.100676 second(s), 26 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表