要求是这么样的,我有一张综合表,要从别的五张表上取统计数据往这张表里写,同时还要求判断这个统计的数据在这个综合表中是否已存大,存在就更新,不存在就写入
该如何写
14 个解决方案
#1
update...
if @@rowcount=0
insert ...
if @@rowcount=0
insert ...
#2
create procedure test
as
if exists(select * from 综合表 where ......)
update 综合表 set col=值 where .......
else
insert into 综合表 select * from ... where ....
go
as
if exists(select * from 综合表 where ......)
update 综合表 set col=值 where .......
else
insert into 综合表 select * from ... where ....
go
#3
同意楼上。
update table set column='XXX' where ...
if @@rowcount=0
insert table (...) values(...)
update table set column='XXX' where ...
if @@rowcount=0
insert table (...) values(...)
#4
但要求是统半这五张表的SQL也在存储过程中,这五张表每张表统计出来都有N条记录,每个都要循环的啊
#5
你加上统计语句就可以了,不用循环,不过最好加上index
#6
create procedure test
as
if exists(select * from 综合表 where ......)
update 综合表 set col=值 where .......
else
insert into 综合表 select * from ... where ....
go
如果要求更详细的清贴出数据结构和条件
as
if exists(select * from 综合表 where ......)
update 综合表 set col=值 where .......
else
insert into 综合表 select * from ... where ....
go
如果要求更详细的清贴出数据结构和条件
#7
create procedure test
as
if exists(select * from 综合表 where ......)
update 综合表 set col=值 where .......
else
insert into 综合表 select * from ... where ....
go
as
if exists(select * from 综合表 where ......)
update 综合表 set col=值 where .......
else
insert into 综合表 select * from ... where ....
go
#8
各们老大,谢了,我贴上数据结构
#9
这是综合表的结构,
Cust_no Nvarchar(10) 客户编号
Brand Nvarchar(10) 品牌编号
Materielno Nvarcahr(10) 物料编号
ShipmentQuantity Int 出货数
ExitQuantity Int 退货数
WithQuantity Int 货损数
Fact_sell Int 实销数
Begindate date 开始时间
Enddate date 结束时间
下面是我要插入综合表的统计的SQL数句,回关连太多表不能贴表结构,不好意思
SELECT SUM(ExitList.Amount) AS amount, ExitList.Materielno, ExitMain.cust_no,B530.NAME, B530.PID
FROM ExitList INNER JOIN ExitMain ON ExitList.Bill = ExitMain.Bill INNER JOIN
B530 ON ExitMain.cust_no = B530.CUST_NO
WHERE (YEAR(ExitMain.make_date) = '2005') AND (MONTH(ExitMain.make_date) = '1') AND
(ExitMain.state = '2') GROUP BY ExitMain.cust_no, ExitList.Materielno, ExitMain.cust_no, B530.NAME, B530.PID
上面为取得退货以客户与物料为基础的统计数量
SELECT B530.CUST_NO, WithdrawGoodsList.MaterielID, SUM(WithdrawGoodsList.Amount) AS amount, B530.PID, B530.NAME FROM
WithdrawGoodsMain INNER JOIN WithdrawGoodsList ON WithdrawGoodsMain.WithdrawGoodsID = WithdrawGoodsList.WithdrawGoodsID
INNER JOIN OrderMain ON WithdrawGoodsMain.orderID = OrderMain.Po INNER JOIN B530 ON OrderMain.Cust_No = B530.CUST_NO WHERE
(WithdrawGoodsMain.state = '2') AND (YEAR(WithdrawGoodsMain.WithdrawGoodsDate) = '2005')
AND (MONTH(WithdrawGoodsMain.WithdrawGoodsDate) = '1')
GROUP BY B530.CUST_NO, WithdrawGoodsList.MaterielID, B530.PID, B530.NAME
上面为取得货损以客户与物料为基础的统计数量
SELECT acceptPayP.customer, acceptPayF.stype, acceptPayF.po, B530.PID,
SUM(CONVERT(money, CAST(acceptPayF.addmoney AS money))) AS addmoney
FROM acceptPayF INNER JOIN
acceptPayP ON acceptPayF.po = acceptPayP.po INNER JOIN
B530 ON acceptPayP.customer = B530.CUST_NO INNER JOIN
grade_b130 ON acceptPayF.stype = grade_b130.grade_no
WHERE (YEAR(acceptPayP.setdatex) = '2005') AND (MONTH(acceptPayP.setdatex) = '01')
GROUP BY acceptPayP.customer, acceptPayF.stype, acceptPayF.po, B530.PID
上面为取得收款单以客户与品牌为基础的统计数量
SELECT SUM(outdepotlist.outdepotsum) AS Outdepostsum, B530.CUST_NO,
outdepotlist.materielid, outdepotlist.outdepotid, OrderList.Po
FROM outdepotlist INNER JOIN
outdepotmain ON outdepotlist.outdepotid = outdepotmain.outdepotid INNER JOIN
OrderList ON outdepotmain.orderID = OrderList.Po INNER JOIN
OrderMain ON OrderList.Po = OrderMain.Po INNER JOIN
B530 ON OrderMain.Cust_No = B530.CUST_NO
WHERE (YEAR(outdepotmain.outdepotdate) = '2005') AND
(MONTH(outdepotmain.outdepotdate) = '01')
GROUP BY B530.CUST_NO, outdepotlist.materielid, outdepotlist.outdepotid, OrderList.Po
还有几张表的关连的没写上,主要是我统计后的结果在综合表中插入的字段都是不一样的
如第一条SQL统计后插入综合表客户编号,品牌编号,物料编号,退货数,开始时,结束时间,而第二句插入综合表客户编号,品牌编号,物料编号,货损数,开始时,结束时间,谢谢高手们了
Cust_no Nvarchar(10) 客户编号
Brand Nvarchar(10) 品牌编号
Materielno Nvarcahr(10) 物料编号
ShipmentQuantity Int 出货数
ExitQuantity Int 退货数
WithQuantity Int 货损数
Fact_sell Int 实销数
Begindate date 开始时间
Enddate date 结束时间
下面是我要插入综合表的统计的SQL数句,回关连太多表不能贴表结构,不好意思
SELECT SUM(ExitList.Amount) AS amount, ExitList.Materielno, ExitMain.cust_no,B530.NAME, B530.PID
FROM ExitList INNER JOIN ExitMain ON ExitList.Bill = ExitMain.Bill INNER JOIN
B530 ON ExitMain.cust_no = B530.CUST_NO
WHERE (YEAR(ExitMain.make_date) = '2005') AND (MONTH(ExitMain.make_date) = '1') AND
(ExitMain.state = '2') GROUP BY ExitMain.cust_no, ExitList.Materielno, ExitMain.cust_no, B530.NAME, B530.PID
上面为取得退货以客户与物料为基础的统计数量
SELECT B530.CUST_NO, WithdrawGoodsList.MaterielID, SUM(WithdrawGoodsList.Amount) AS amount, B530.PID, B530.NAME FROM
WithdrawGoodsMain INNER JOIN WithdrawGoodsList ON WithdrawGoodsMain.WithdrawGoodsID = WithdrawGoodsList.WithdrawGoodsID
INNER JOIN OrderMain ON WithdrawGoodsMain.orderID = OrderMain.Po INNER JOIN B530 ON OrderMain.Cust_No = B530.CUST_NO WHERE
(WithdrawGoodsMain.state = '2') AND (YEAR(WithdrawGoodsMain.WithdrawGoodsDate) = '2005')
AND (MONTH(WithdrawGoodsMain.WithdrawGoodsDate) = '1')
GROUP BY B530.CUST_NO, WithdrawGoodsList.MaterielID, B530.PID, B530.NAME
上面为取得货损以客户与物料为基础的统计数量
SELECT acceptPayP.customer, acceptPayF.stype, acceptPayF.po, B530.PID,
SUM(CONVERT(money, CAST(acceptPayF.addmoney AS money))) AS addmoney
FROM acceptPayF INNER JOIN
acceptPayP ON acceptPayF.po = acceptPayP.po INNER JOIN
B530 ON acceptPayP.customer = B530.CUST_NO INNER JOIN
grade_b130 ON acceptPayF.stype = grade_b130.grade_no
WHERE (YEAR(acceptPayP.setdatex) = '2005') AND (MONTH(acceptPayP.setdatex) = '01')
GROUP BY acceptPayP.customer, acceptPayF.stype, acceptPayF.po, B530.PID
上面为取得收款单以客户与品牌为基础的统计数量
SELECT SUM(outdepotlist.outdepotsum) AS Outdepostsum, B530.CUST_NO,
outdepotlist.materielid, outdepotlist.outdepotid, OrderList.Po
FROM outdepotlist INNER JOIN
outdepotmain ON outdepotlist.outdepotid = outdepotmain.outdepotid INNER JOIN
OrderList ON outdepotmain.orderID = OrderList.Po INNER JOIN
OrderMain ON OrderList.Po = OrderMain.Po INNER JOIN
B530 ON OrderMain.Cust_No = B530.CUST_NO
WHERE (YEAR(outdepotmain.outdepotdate) = '2005') AND
(MONTH(outdepotmain.outdepotdate) = '01')
GROUP BY B530.CUST_NO, outdepotlist.materielid, outdepotlist.outdepotid, OrderList.Po
还有几张表的关连的没写上,主要是我统计后的结果在综合表中插入的字段都是不一样的
如第一条SQL统计后插入综合表客户编号,品牌编号,物料编号,退货数,开始时,结束时间,而第二句插入综合表客户编号,品牌编号,物料编号,货损数,开始时,结束时间,谢谢高手们了
#10
在大表里创建所有要用到的字段,
然后在 每条 insert 的时候写上字段名就可以了撒.
#11
什么意思啊,还没明白呢,高手说明白点啊,谢了
#12
强烈同意
xluzhong(打麻将一缺三,咋办?)
应该没错
xluzhong(打麻将一缺三,咋办?)
应该没错
#13
没有人提别的意见了吗?,来点例子也好嘛,谢了,不行就结贴吧,
#14
update 综合表 set col=值 where .......
insert into 综合表 select * from ... where not exists......
insert into 综合表 select * from ... where not exists......
#1
update...
if @@rowcount=0
insert ...
if @@rowcount=0
insert ...
#2
create procedure test
as
if exists(select * from 综合表 where ......)
update 综合表 set col=值 where .......
else
insert into 综合表 select * from ... where ....
go
as
if exists(select * from 综合表 where ......)
update 综合表 set col=值 where .......
else
insert into 综合表 select * from ... where ....
go
#3
同意楼上。
update table set column='XXX' where ...
if @@rowcount=0
insert table (...) values(...)
update table set column='XXX' where ...
if @@rowcount=0
insert table (...) values(...)
#4
但要求是统半这五张表的SQL也在存储过程中,这五张表每张表统计出来都有N条记录,每个都要循环的啊
#5
你加上统计语句就可以了,不用循环,不过最好加上index
#6
create procedure test
as
if exists(select * from 综合表 where ......)
update 综合表 set col=值 where .......
else
insert into 综合表 select * from ... where ....
go
如果要求更详细的清贴出数据结构和条件
as
if exists(select * from 综合表 where ......)
update 综合表 set col=值 where .......
else
insert into 综合表 select * from ... where ....
go
如果要求更详细的清贴出数据结构和条件
#7
create procedure test
as
if exists(select * from 综合表 where ......)
update 综合表 set col=值 where .......
else
insert into 综合表 select * from ... where ....
go
as
if exists(select * from 综合表 where ......)
update 综合表 set col=值 where .......
else
insert into 综合表 select * from ... where ....
go
#8
各们老大,谢了,我贴上数据结构
#9
这是综合表的结构,
Cust_no Nvarchar(10) 客户编号
Brand Nvarchar(10) 品牌编号
Materielno Nvarcahr(10) 物料编号
ShipmentQuantity Int 出货数
ExitQuantity Int 退货数
WithQuantity Int 货损数
Fact_sell Int 实销数
Begindate date 开始时间
Enddate date 结束时间
下面是我要插入综合表的统计的SQL数句,回关连太多表不能贴表结构,不好意思
SELECT SUM(ExitList.Amount) AS amount, ExitList.Materielno, ExitMain.cust_no,B530.NAME, B530.PID
FROM ExitList INNER JOIN ExitMain ON ExitList.Bill = ExitMain.Bill INNER JOIN
B530 ON ExitMain.cust_no = B530.CUST_NO
WHERE (YEAR(ExitMain.make_date) = '2005') AND (MONTH(ExitMain.make_date) = '1') AND
(ExitMain.state = '2') GROUP BY ExitMain.cust_no, ExitList.Materielno, ExitMain.cust_no, B530.NAME, B530.PID
上面为取得退货以客户与物料为基础的统计数量
SELECT B530.CUST_NO, WithdrawGoodsList.MaterielID, SUM(WithdrawGoodsList.Amount) AS amount, B530.PID, B530.NAME FROM
WithdrawGoodsMain INNER JOIN WithdrawGoodsList ON WithdrawGoodsMain.WithdrawGoodsID = WithdrawGoodsList.WithdrawGoodsID
INNER JOIN OrderMain ON WithdrawGoodsMain.orderID = OrderMain.Po INNER JOIN B530 ON OrderMain.Cust_No = B530.CUST_NO WHERE
(WithdrawGoodsMain.state = '2') AND (YEAR(WithdrawGoodsMain.WithdrawGoodsDate) = '2005')
AND (MONTH(WithdrawGoodsMain.WithdrawGoodsDate) = '1')
GROUP BY B530.CUST_NO, WithdrawGoodsList.MaterielID, B530.PID, B530.NAME
上面为取得货损以客户与物料为基础的统计数量
SELECT acceptPayP.customer, acceptPayF.stype, acceptPayF.po, B530.PID,
SUM(CONVERT(money, CAST(acceptPayF.addmoney AS money))) AS addmoney
FROM acceptPayF INNER JOIN
acceptPayP ON acceptPayF.po = acceptPayP.po INNER JOIN
B530 ON acceptPayP.customer = B530.CUST_NO INNER JOIN
grade_b130 ON acceptPayF.stype = grade_b130.grade_no
WHERE (YEAR(acceptPayP.setdatex) = '2005') AND (MONTH(acceptPayP.setdatex) = '01')
GROUP BY acceptPayP.customer, acceptPayF.stype, acceptPayF.po, B530.PID
上面为取得收款单以客户与品牌为基础的统计数量
SELECT SUM(outdepotlist.outdepotsum) AS Outdepostsum, B530.CUST_NO,
outdepotlist.materielid, outdepotlist.outdepotid, OrderList.Po
FROM outdepotlist INNER JOIN
outdepotmain ON outdepotlist.outdepotid = outdepotmain.outdepotid INNER JOIN
OrderList ON outdepotmain.orderID = OrderList.Po INNER JOIN
OrderMain ON OrderList.Po = OrderMain.Po INNER JOIN
B530 ON OrderMain.Cust_No = B530.CUST_NO
WHERE (YEAR(outdepotmain.outdepotdate) = '2005') AND
(MONTH(outdepotmain.outdepotdate) = '01')
GROUP BY B530.CUST_NO, outdepotlist.materielid, outdepotlist.outdepotid, OrderList.Po
还有几张表的关连的没写上,主要是我统计后的结果在综合表中插入的字段都是不一样的
如第一条SQL统计后插入综合表客户编号,品牌编号,物料编号,退货数,开始时,结束时间,而第二句插入综合表客户编号,品牌编号,物料编号,货损数,开始时,结束时间,谢谢高手们了
Cust_no Nvarchar(10) 客户编号
Brand Nvarchar(10) 品牌编号
Materielno Nvarcahr(10) 物料编号
ShipmentQuantity Int 出货数
ExitQuantity Int 退货数
WithQuantity Int 货损数
Fact_sell Int 实销数
Begindate date 开始时间
Enddate date 结束时间
下面是我要插入综合表的统计的SQL数句,回关连太多表不能贴表结构,不好意思
SELECT SUM(ExitList.Amount) AS amount, ExitList.Materielno, ExitMain.cust_no,B530.NAME, B530.PID
FROM ExitList INNER JOIN ExitMain ON ExitList.Bill = ExitMain.Bill INNER JOIN
B530 ON ExitMain.cust_no = B530.CUST_NO
WHERE (YEAR(ExitMain.make_date) = '2005') AND (MONTH(ExitMain.make_date) = '1') AND
(ExitMain.state = '2') GROUP BY ExitMain.cust_no, ExitList.Materielno, ExitMain.cust_no, B530.NAME, B530.PID
上面为取得退货以客户与物料为基础的统计数量
SELECT B530.CUST_NO, WithdrawGoodsList.MaterielID, SUM(WithdrawGoodsList.Amount) AS amount, B530.PID, B530.NAME FROM
WithdrawGoodsMain INNER JOIN WithdrawGoodsList ON WithdrawGoodsMain.WithdrawGoodsID = WithdrawGoodsList.WithdrawGoodsID
INNER JOIN OrderMain ON WithdrawGoodsMain.orderID = OrderMain.Po INNER JOIN B530 ON OrderMain.Cust_No = B530.CUST_NO WHERE
(WithdrawGoodsMain.state = '2') AND (YEAR(WithdrawGoodsMain.WithdrawGoodsDate) = '2005')
AND (MONTH(WithdrawGoodsMain.WithdrawGoodsDate) = '1')
GROUP BY B530.CUST_NO, WithdrawGoodsList.MaterielID, B530.PID, B530.NAME
上面为取得货损以客户与物料为基础的统计数量
SELECT acceptPayP.customer, acceptPayF.stype, acceptPayF.po, B530.PID,
SUM(CONVERT(money, CAST(acceptPayF.addmoney AS money))) AS addmoney
FROM acceptPayF INNER JOIN
acceptPayP ON acceptPayF.po = acceptPayP.po INNER JOIN
B530 ON acceptPayP.customer = B530.CUST_NO INNER JOIN
grade_b130 ON acceptPayF.stype = grade_b130.grade_no
WHERE (YEAR(acceptPayP.setdatex) = '2005') AND (MONTH(acceptPayP.setdatex) = '01')
GROUP BY acceptPayP.customer, acceptPayF.stype, acceptPayF.po, B530.PID
上面为取得收款单以客户与品牌为基础的统计数量
SELECT SUM(outdepotlist.outdepotsum) AS Outdepostsum, B530.CUST_NO,
outdepotlist.materielid, outdepotlist.outdepotid, OrderList.Po
FROM outdepotlist INNER JOIN
outdepotmain ON outdepotlist.outdepotid = outdepotmain.outdepotid INNER JOIN
OrderList ON outdepotmain.orderID = OrderList.Po INNER JOIN
OrderMain ON OrderList.Po = OrderMain.Po INNER JOIN
B530 ON OrderMain.Cust_No = B530.CUST_NO
WHERE (YEAR(outdepotmain.outdepotdate) = '2005') AND
(MONTH(outdepotmain.outdepotdate) = '01')
GROUP BY B530.CUST_NO, outdepotlist.materielid, outdepotlist.outdepotid, OrderList.Po
还有几张表的关连的没写上,主要是我统计后的结果在综合表中插入的字段都是不一样的
如第一条SQL统计后插入综合表客户编号,品牌编号,物料编号,退货数,开始时,结束时间,而第二句插入综合表客户编号,品牌编号,物料编号,货损数,开始时,结束时间,谢谢高手们了
#10
在大表里创建所有要用到的字段,
然后在 每条 insert 的时候写上字段名就可以了撒.
#11
什么意思啊,还没明白呢,高手说明白点啊,谢了
#12
强烈同意
xluzhong(打麻将一缺三,咋办?)
应该没错
xluzhong(打麻将一缺三,咋办?)
应该没错
#13
没有人提别的意见了吗?,来点例子也好嘛,谢了,不行就结贴吧,
#14
update 综合表 set col=值 where .......
insert into 综合表 select * from ... where not exists......
insert into 综合表 select * from ... where not exists......