标题:将一个表两个列相加插入到另一个表上 SQL语句
只看楼主
czq0904
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-5-19
结帖率:0
已结贴  问题点数:5 回复次数:18 
将一个表两个列相加插入到另一个表上 SQL语句
如今有两个表,一个是成绩表,字段如下:姓名varchar(20),语文成绩number,数学成绩number;另一个表是评语表,字段如下:姓名varchar(2),评语varchar(10),现在需要根据成绩表的成绩插入评语到评语表,根据语文成绩和数学成绩相加,如果总成绩>=90即插入"优秀",如果90>总成绩>=80即插入"良",如果80>总成绩>=60即插入"普通",如果总成绩<60即插入"差",如何写SQL语句
搜索更多相关主题的帖子: number 数学 成绩表 总成绩 
2013-05-19 10:35
Aviva_Wang
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:苏州
等 级:贵宾
威 望:26
帖 子:701
专家分:2382
注 册:2013-3-12
得分:2 
这个不是一条sql就能解决的问题吧,你一个函数吧

asp
2013-05-19 20:48
华丽之词
Rank: 1
等 级:新手上路
帖 子:2
专家分:2
注 册:2013-5-7
得分:2 
--创建存储过程
create procedure ping_proc
as
    declare @name varchar(20)
    select @name = name from score
    declare @ping int
    select @ping = yuwen+shuxu from score
    if(@ping >= 90)
        insert into pingyu(name,pingyu) values(@name,'优秀')
    if(@ping >= 80)
        insert into pingyu(name,pingyu) values(@name,'良')
    if(@ping >= 60)
        insert into pingyu(name,pingyu) values(@name,'普通')
    else
        insert into pingyu(name,pingyu) values (@name,'差')
go
--执行存储过程
exec ping_proc

你用这个存储过程试试看
2013-05-20 11:52
czq0904
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-5-19
得分:0 
回复 3楼 华丽之词
用了这个怎么就只能插入一条呢?
2013-05-21 15:32
czq0904
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-5-19
得分:0 
回复 2楼 Aviva_Wang
那怎么写!?
2013-05-21 15:32
何时是月明
Rank: 2
等 级:论坛游民
帖 子:30
专家分:76
注 册:2011-9-27
得分:2 
用 case语句!
2013-05-23 14:19
chenpengfei1
Rank: 2
等 级:论坛游民
威 望:1
帖 子:15
专家分:64
注 册:2013-3-30
得分:0 
可以架一个循环嘛,用变量的形式来增加
create procedure ping_proc
as
begin
    declare @iint,
    set@i=0,
    while i<10
     @i=@i+1,
    declare @name varchar(20)
    select @name = name from score
    declare @ping int
    select @ping = yuwen+shuxu from score
    if(@ping >= 90)
        insert into pingyu(name,pingyu) values(@name,'优秀')
    if(@ping >= 80)
        insert into pingyu(name,pingyu) values(@name,'良')
    if(@ping >= 60)
        insert into pingyu(name,pingyu) values(@name,'普通')
    else
        insert into pingyu(name,pingyu) values (@name,'差')
end
go
--执行存储过程
exec ping_proc
2013-06-18 16:59
303770957
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:838
专家分:2125
注 册:2005-9-10
得分:0 
*****************************************************
The following is the complete code that can be
directly executed to get results what you wanted.
If you have any questions please send me a message.
Here is an exeample for this question,Good Luck!
*****************************************************


程序代码:
---------------------Create table named YourSourceTable-------------------------------------------------------
IF EXISTS (SELECT * FROM sys.tables WHERE object_id = OBJECT_ID(N'[dbo].[YourSourceTable]') AND type in (N'U'))
DROP TABLE [dbo].[YourSourceTable]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Table YourSourceTable
(
  Name varchar(50),
  English int,
  Chinese int
)
-----------------Insert data into table named YourSourceTable---------
delete from YourSourceTable
Insert into YourSourceTable(Name,English,Chinese) values('赵钱',98,89)
Insert into YourSourceTable(Name,English,Chinese) values('孙李',78,98)
Insert into YourSourceTable(Name,English,Chinese) values('张三',56,58)
Insert into YourSourceTable(Name,English,Chinese) values('李四',75,68)
Insert into YourSourceTable(Name,English,Chinese) values('周五',86,79)
Insert into YourSourceTable(Name,English,Chinese) values('郑王',89,76)
-----------------Create table and Insert data into YourSourceTable ---
IF EXISTS (SELECT * FROM sys.tables WHERE object_id = OBJECT_ID(N'[dbo].[YourAnotherSourceTable]') AND type in (N'U'))
DROP TABLE [dbo].[YourAnotherSourceTable]
GO
select Name,English+Chinese as Totals into YourAnotherSourceTable
from YourSourceTable
Go
------------------To see data from YourSourceTable and YourAnotherSourceTable--------
select * from YourSourceTable
select * from YourAnotherSourceTable
---------------------------------The  End--------------------------------------------

Result:



[ 本帖最后由 303770957 于 2013-6-26 16:05 编辑 ]

♂ 死后定当长眠,生前何须久睡。♀
2013-06-26 15:57
303770957
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:838
专家分:2125
注 册:2005-9-10
得分:0 
程序代码:
[本代码可以直接执行]
---------------------Create table named [成绩表]------------------------------------------------------
IF EXISTS (SELECT * FROM sys.tables WHERE object_id = OBJECT_ID(N'[dbo].[成绩表]') AND type in (N'U'))
DROP TABLE [dbo].[成绩表]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Table [成绩表]
(
  [姓名] varchar(20),
  [语文成绩] int,
  [数学成绩] int
)
-----------------Insert data into table named [成绩表]---------
delete from [成绩表]
Insert into [成绩表]([姓名],[语文成绩],[数学成绩]) values('赵钱',38,69)
Insert into [成绩表]([姓名],[语文成绩],[数学成绩]) values('孙李',29,46)
Insert into [成绩表]([姓名],[语文成绩],[数学成绩]) values('张三',36,48)
Insert into [成绩表]([姓名],[语文成绩],[数学成绩]) values('李四',35,28)
Insert into [成绩表]([姓名],[语文成绩],[数学成绩]) values('周五',56,39)
Insert into [成绩表]([姓名],[语文成绩],[数学成绩]) values('郑王',48,40)
-----------------Create table and Insert data into [评语表]---
IF EXISTS (SELECT * FROM sys.tables WHERE object_id = OBJECT_ID(N'[dbo].[评语表]') AND type in (N'U'))
DROP TABLE [dbo].[评语表]
GO
select [姓名],[语文成绩]+[数学成绩] as [总成绩],
Case
When [语文成绩]+[数学成绩] >=90 then '优秀'
when 90>[语文成绩]+[数学成绩] and [语文成绩]+[数学成绩]>=80 then ''
when 80>[语文成绩]+[数学成绩] and [语文成绩]+[数学成绩]>=60 then '普通'
else ''
end as [评语]
into [评语表] from [成绩表]
Go
------------------To see data from [成绩表] and [评语表]--------
select * from [成绩表]
select * from [评语表]
---------------------------------The  End--------------------------------------------

Result:
[attach]71729[/attach]


[ 本帖最后由 303770957 于 2013-9-10 10:20 编辑 ]

♂ 死后定当长眠,生前何须久睡。♀
2013-06-26 16:24
yaner003
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-8-27
得分:0 
好难啊! 十六浦娱|乐|城 同乐城
2013-08-27 21:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-407468-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.239849 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved