求一存储过程。对刚才的话说sorry!

Mark杨 2010-03-27 10:54:27
表1
金额 车间 时间
1 25 化验 2010-3-24
2 32 电雕 2010-3-24
3 13 仓库 2010-3-24
4 2 电镀 2010-3-24
5 8 雕刻 2010-3-24
6 0 化验 2010-3-25
7 23 电雕 2010-3-25
8 15 仓库 2010-3-25
9 22 电镀 2010-3-25

表2
部门 车间
1 财务部 '仓库'
2 电雕部 '电雕','雕刻'
3 电镀部 '化验'

表3
部门 金额


要查出某个时间段内 所有部门的总金额,并把数据插入表3中。写在存储过程中、

不知道这次有没有表达清楚。

在这里对我刚才说的话表示道歉。SORRY!
...全文
104 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
htl258_Tony 2010-03-27
  • 打赏
  • 举报
回复
--------------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-03-27 11:01:01
-- Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
--------------------------------------------------------------------------
--> 生成测试数据表:t1

IF NOT OBJECT_ID('[t1]') IS NULL
DROP TABLE [t1]
GO
CREATE TABLE [t1]([ID] INT,[金额] INT,[车间] NVARCHAR(10),[时间] DATETIME)
INSERT [t1]
SELECT 1,25,N'化验',N'2010-3-24' UNION ALL
SELECT 2,32,N'电雕',N'2010-3-24' UNION ALL
SELECT 3,13,N'仓库',N'2010-3-24' UNION ALL
SELECT 4,2,N'电镀',N'2010-3-24' UNION ALL
SELECT 5,8,N'雕刻',N'2010-3-24' UNION ALL
SELECT 6,0,N'化验',N'2010-3-25' UNION ALL
SELECT 7,23,N'电雕',N'2010-3-25' UNION ALL
SELECT 8,15,N'仓库',N'2010-3-25' UNION ALL
SELECT 9,22,N'电镀',N'2010-3-25'
GO
--SELECT * FROM [t1]

--> 生成测试数据表:t2

IF NOT OBJECT_ID('[t2]') IS NULL
DROP TABLE [t2]
GO
CREATE TABLE [t2]([ID] INT,[部门] NVARCHAR(10),[车间] NVARCHAR(10))
INSERT [t2]
SELECT 1,N'财务部',N'''仓库''' UNION ALL
SELECT 2,N'电雕部',N'''电雕'',''雕刻''' UNION ALL
SELECT 3,N'电镀部',N'''化验'',''电镀'''
GO
--SELECT * FROM [t2]

--> 生成测试数据表:t3

IF NOT OBJECT_ID('[t3]') IS NULL
DROP TABLE [t3]
GO
CREATE TABLE [t3]([部门] NVARCHAR(10),[金额] NVARCHAR(10))

-->SQL查询如下:
--创建存储过程
IF NOT OBJECT_ID('[sp_test]') IS NULL
DROP PROC [sp_test]
GO
CREATE PROC sp_test
@bt datetime='1900',
@et datetime='9999'
AS
insert t3
select b.部门,SUM(a.金额) 金额
from t1 a
join t2 b
on CHARINDEX(''''+a.[车间]+'''',b.车间)>0
where a.时间 between @bt and @et
group by b.部门
GO
--调用过程查询
exec sp_test

select * from t3

/*
部门 金额
---------- ----------
财务部 28
电雕部 63
电镀部 49

(3 行受影响)

*/

truncate table t3

exec sp_test '2010-3-24','2010-3-24'
select * from t3
/*
部门 金额
---------- ----------
财务部 13
电雕部 40
电镀部 27

(3 行受影响)
*/
Mark杨 2010-03-27
  • 打赏
  • 举报
回复
加上时间断也不会出错吗
fengxiaohan211 2010-03-27
  • 打赏
  • 举报
回复
学习 小F厉害
东那个升 2010-03-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ldslove 的回复:]
SQL code

declare begindate datetime,@enddate datetime
insert 表3
select a.部门,SUM(金额) as 金额
from 表2 a,表1 b
where charindex(','''+b.车间+''',',','+replace(a.车间,'''','''''')+',')>0
and b.时间 betwee……
[/Quote]

...replace(a.车间,'''','''''')换成a.车间
--小F-- 2010-03-27
  • 打赏
  • 举报
回复
对了 插入表3用insert into 表3(部门,金额) select * from 前面的结果
--小F-- 2010-03-27
  • 打赏
  • 举报
回复
楼上的大牛 给出语句了 但是我还是建议在保存的时候不要有单引号 这样效率不高 呵呵
-狙击手- 2010-03-27
  • 打赏
  • 举报
回复
--Start
------------------------------------
-- Author: flystone
-- Version:V1.001
-- Date:2010-03-27 10:57:21
------------------------------------

-- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(id int,金额 int,车间 nvarchar(2),时间 smalldatetime)
Go
Insert into ta
select 1,25,'化验','2010-3-24' union all
select 2,32,'电雕','2010-3-24' union all
select 3,13,'仓库','2010-3-24' union all
select 4,2,'电镀','2010-3-24' union all
select 5,8,'雕刻','2010-3-24' union all
select 6,0,'化验','2010-3-25' union all
select 7,23,'电雕','2010-3-25' union all
select 8,15,'仓库','2010-3-25' union all
select 9,22,'电镀','2010-3-25'
Go
-- Test Data: tb
If object_id('tb') is not null
Drop table tb
Go
Create table tb(id int,部门 nvarchar(3),车间 nvarchar(10))
Go
Insert into tb
select 1,'财务部','''仓库''' union all
select 2,'电雕部','''电雕'',''雕刻''' union all
select 3,'电镀部','''化验'''
Go
create proc pr_test
as
Select b.部门 ,sum(金额)
from ta a
join tb b
on charindex(','''+a.车间+''',',','+b.车间+',')>0
group by b.部门
go
exec pr_test

--Result:
/*

部门
---- -----------
财务部 28
电雕部 63
电镀部 25

(所影响的行数为 3 行)



*/
--End
东那个升 2010-03-27
  • 打赏
  • 举报
回复

declare begindate datetime,@enddate datetime
insert 表3
select a.部门,SUM(金额) as 金额
from 表2 a,表1 b
where charindex(','''+b.车间+''',',','+replace(a.车间,'''','''''')+',')>0
and b.时间 between @begindate and @enddate
group by a.部门

?????????
-狙击手- 2010-03-27
  • 打赏
  • 举报
回复
------------------------------------
-- Author: flystone
-- Version:V1.001
-- Date:2010-03-27 10:57:21
------------------------------------

-- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(id int,金额 int,车间 nvarchar(2),时间 smalldatetime)
Go
Insert into ta
select 1,25,'化验','2010-3-24' union all
select 2,32,'电雕','2010-3-24' union all
select 3,13,'仓库','2010-3-24' union all
select 4,2,'电镀','2010-3-24' union all
select 5,8,'雕刻','2010-3-24' union all
select 6,0,'化验','2010-3-25' union all
select 7,23,'电雕','2010-3-25' union all
select 8,15,'仓库','2010-3-25' union all
select 9,22,'电镀','2010-3-25'
Go
-- Test Data: tb
If object_id('tb') is not null
Drop table tb
Go
Create table tb(id int,部门 nvarchar(3),车间 nvarchar(5))
Go
Insert into tb
select 1,'财务部','仓库' union all
select 2,'电雕部','电雕,雕刻' union all
select 3,'电镀部','化验'
Go
--Start
create proc pr_test
as
Select b.部门 ,sum(金额)
from ta a
join tb b
on charindex(','+a.车间+',',','+b.车间+',')>0
group by b.部门
go
exec pr_test

--Result:
/*

部门
---- -----------
财务部 28
电雕部 63
电镀部 25

(所影响的行数为 3 行)



*/
--End
guguda2008 2010-03-27
  • 打赏
  • 举报
回复
这就很明了了,交给小F
Mark杨 2010-03-27
  • 打赏
  • 举报
回复
这只是我自己的想法,如果有更好的你可以用你自己的方法。只要能实现功能就可以了
Mark杨 2010-03-27
  • 打赏
  • 举报
回复
是的
都加了单引号
--小F-- 2010-03-27
  • 打赏
  • 举报
回复
表2中的车间都加了引号吗?

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧