早晨起来问一个数据库方面的(按某字段排序)

ZOU_SEAFARER 2007-07-06 08:50:47
MSSQL 2000 + VB6.0

现在有一个表(没有KEY)
ROW DESC fValue
1 p 15
2 p 20
3 p 23
4 v 15
5 16
6 v 21
7 m 22
8 m 23

需要按指定规则排序 比如 V , M ,P 排序后改变行号继续保存在这个表中
可以增加临时表,方便导动数据等,
结果应该是
ROW DESC fValue
1 V 15 (其中排序关键字段相同的确时候,
2 V 21 行做第二排序关键字)
3 M 22
4 M 23
5 P 15
6 P 20
7 P 23
8 16 (不在指定范围的空格放最后)
...全文
587 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
洋溢2020 2007-07-07
  • 打赏
  • 举报
回复
看看~
ZOU_SEAFARER 2007-07-06
  • 打赏
  • 举报
回复
Sub Sp_Sort(S_Table As String, D_Table, F_Key As String, Optional S_Key As String)
Dim SQL As String
Dim strOtherCol As String
strOtherCol = "[fEwcID],"
strOtherCol = strOtherCol & "[fEwcDatMasID] ,"
strOtherCol = strOtherCol & "[fEwcDatNo] ,"
' strOtherCol = strOtherCol & "[fEwcRow] ,"
'+++++++++++++++++++++++++++++++++++++++20070709+++++++++++++++++++++++++++++++++
strOtherCol = strOtherCol & "[fEwcDIA] ,"
strOtherCol = strOtherCol & "[fEwcPage] ,"
strOtherCol = strOtherCol & "[fEwcFR] ,"
'+++++++++++++++++++++++++++++++++++++++20070709+++++++++++++++++++++++++++++++++
strOtherCol = strOtherCol & "[fEwcGaiMasID] ,"
strOtherCol = strOtherCol & "[fEwcCircuit] ,"
strOtherCol = strOtherCol & "[fEwcDivision] ,"
strOtherCol = strOtherCol & "[fEwcDivNo] ,"
strOtherCol = strOtherCol & "[fEwcDivCheck] ,"
strOtherCol = strOtherCol & "[fEwcDekID1],"
strOtherCol = strOtherCol & "[fEwcDekID2],"
strOtherCol = strOtherCol & "[fEwcDestination1] ,"
strOtherCol = strOtherCol & "[fEwcDestination2] ,"
strOtherCol = strOtherCol & "[fEwcClrMasID] ,"
strOtherCol = strOtherCol & "[fEwcCutPrint],"
strOtherCol = strOtherCol & "[fEwcMark] ,"
strOtherCol = strOtherCol & "[fEwcLength] ,"
strOtherCol = strOtherCol & "[fEwcForcePrint] ,"
strOtherCol = strOtherCol & "[fEwcSetFlag1] ,"
strOtherCol = strOtherCol & "[fEwcForeRGB1] ,"
strOtherCol = strOtherCol & "[fEwcBackRGB1] ,"
strOtherCol = strOtherCol & "[fEwcSetFlag2] ,"
strOtherCol = strOtherCol & "[fEwcForeRGB2] ,"
strOtherCol = strOtherCol & "[fEwcBackRGB2] ,"
strOtherCol = strOtherCol & "[fEwcSetFlag3] ,"
strOtherCol = strOtherCol & "[fEwcForeRGB3] ,"
strOtherCol = strOtherCol & "[fEwcBackRGB3] ,"
strOtherCol = strOtherCol & "[fEwcSetFlag4] ,"
strOtherCol = strOtherCol & "[fEwcForeRGB4] ,"
strOtherCol = strOtherCol & "[fEwcBackRGB4] ,"
strOtherCol = strOtherCol & "[fEwcSetFlag5] ,"
strOtherCol = strOtherCol & "[fEwcForeRGB5] ,"
strOtherCol = strOtherCol & "[fEwcBackRGB5] ,"
strOtherCol = strOtherCol & "[fEwcSetFlag6] ,"
strOtherCol = strOtherCol & "[fEwcForeRGB6] ,"
strOtherCol = strOtherCol & "[fEwcBackRGB6] ,"
strOtherCol = strOtherCol & "[fEwcSetFlag7] ,"
strOtherCol = strOtherCol & "[fEwcForeRGB7] ,"
strOtherCol = strOtherCol & "[fEwcBackRGB7] ,"
strOtherCol = strOtherCol & "[fEwcSetFlag8] ,"
strOtherCol = strOtherCol & "[fEwcForeRGB8] ,"
strOtherCol = strOtherCol & "[fEwcBackRGB8] ,"
strOtherCol = strOtherCol & "[fEwcSetFlag9] ,"
strOtherCol = strOtherCol & "[fEwcForeRGB9] ,"
strOtherCol = strOtherCol & "[fEwcBackRGB9] ,"
strOtherCol = strOtherCol & "[fEwcSetFlag10] ,"
strOtherCol = strOtherCol & "[fEwcForeRGB10] ,"
strOtherCol = strOtherCol & "[fEwcBackRGB10] ,"
strOtherCol = strOtherCol & "[fEwcSetFlag11] ,"
strOtherCol = strOtherCol & "[fEwcForeRGB11] ,"
strOtherCol = strOtherCol & "[fEwcBackRGB11] ,"
strOtherCol = strOtherCol & "[fEwcSetFlag12] ,"
strOtherCol = strOtherCol & "[fEwcForeRGB12] ,"
strOtherCol = strOtherCol & "[fEwcBackRGB12] ,"
strOtherCol = strOtherCol & "[fEwcPrint],"
strOtherCol = strOtherCol & "[fEwcPrintOver],"
strOtherCol = strOtherCol & "[fEwcCutPrintOver],"
strOtherCol = strOtherCol & "[fEwcDemPrintOver],"
strOtherCol = strOtherCol & "[fEwcUpDivision] ,"
strOtherCol = strOtherCol & "[fEwcUpDivPosition] ,"
strOtherCol = strOtherCol & "[fEwcDownDivision] ,"
strOtherCol = strOtherCol & "[fEwcDownDivPosition],"
strOtherCol = strOtherCol & "[fEwc01Division] ,"
strOtherCol = strOtherCol & "[fEwc01DivPosition],"
strOtherCol = strOtherCol & "[fEwc02Division] ,"
strOtherCol = strOtherCol & "[fEwc02DivPosition],"
strOtherCol = strOtherCol & "[fEwc03Division] ,"
strOtherCol = strOtherCol & "[fEwc03DivPosition],"
strOtherCol = strOtherCol & "[fEwc04Division] ,"
strOtherCol = strOtherCol & "[fEwc04DivPosition],"
strOtherCol = strOtherCol & "[fEwc05Division] ,"
strOtherCol = strOtherCol & "[fEwc05DivPosition],"
strOtherCol = strOtherCol & "[fEwc06Division] ,"
strOtherCol = strOtherCol & "[fEwc06DivPosition],"
strOtherCol = strOtherCol & "[fEwc07Division] ,"
strOtherCol = strOtherCol & "[fEwc07DivPosition],"
strOtherCol = strOtherCol & "[fEwc08Division] ,"
strOtherCol = strOtherCol & "[fEwc08DivPosition],"
strOtherCol = strOtherCol & "[fEwc09Division] ,"
strOtherCol = strOtherCol & "[fEwc09DivPosition],"
strOtherCol = strOtherCol & "[fEwc10Division] ,"
strOtherCol = strOtherCol & "[fEwc10DivPosition],"
strOtherCol = strOtherCol & "[fEwc11Division] ,"
strOtherCol = strOtherCol & "[fEwc11DivPosition],"
strOtherCol = strOtherCol & "[fEwc12Division] ,"
strOtherCol = strOtherCol & "[fEwc12DivPosition],"
strOtherCol = strOtherCol & "[fEwc13Division] ,"
strOtherCol = strOtherCol & "[fEwc13DivPosition],"
strOtherCol = strOtherCol & "[fEwc14Division] ,"
strOtherCol = strOtherCol & "[fEwc14DivPosition],"
strOtherCol = strOtherCol & "[fEwc15Division] ,"
strOtherCol = strOtherCol & "[fEwc15DivPosition],"
strOtherCol = strOtherCol & "[fEwc16Division] ,"
strOtherCol = strOtherCol & "[fEwc16DivPosition],"
strOtherCol = strOtherCol & "[fEwc17Division] ,"
strOtherCol = strOtherCol & "[fEwc17DivPosition],"
strOtherCol = strOtherCol & "[fEwc18Division] ,"
strOtherCol = strOtherCol & "[fEwc18DivPosition],"
strOtherCol = strOtherCol & "[fEwc19Division] ,"
strOtherCol = strOtherCol & "[fEwc19DivPosition],"
strOtherCol = strOtherCol & "[fEwc20Division] ,"
strOtherCol = strOtherCol & "[fEwc20DivPosition],"
strOtherCol = strOtherCol & "[fEwc21Division] ,"
strOtherCol = strOtherCol & "[fEwc21DivPosition],"
strOtherCol = strOtherCol & "[fEwc22Division] ,"
strOtherCol = strOtherCol & "[fEwc22DivPosition],"
strOtherCol = strOtherCol & "[fEwc23Division] ,"
strOtherCol = strOtherCol & "[fEwc23DivPosition],"
strOtherCol = strOtherCol & "[fEwc24Division] ,"
strOtherCol = strOtherCol & "[fEwc24DivPosition],"
strOtherCol = strOtherCol & "[fEwc25Division] ,"
strOtherCol = strOtherCol & "[fEwc25DivPosition],"
strOtherCol = strOtherCol & "[fEwc26Division] ,"
strOtherCol = strOtherCol & "[fEwc26DivPosition],"
strOtherCol = strOtherCol & "[fEwc27Division] ,"
strOtherCol = strOtherCol & "[fEwc27DivPosition],"
strOtherCol = strOtherCol & "[fEwc28Division] ,"
strOtherCol = strOtherCol & "[fEwc28DivPosition],"
strOtherCol = strOtherCol & "[fEwc29Division] ,"
strOtherCol = strOtherCol & "[fEwc29DivPosition],"
strOtherCol = strOtherCol & "[fEwc30Division] ,"
strOtherCol = strOtherCol & "[fEwc30DivPosition],"
strOtherCol = strOtherCol & "[fEwcDeleteKbn] ,"
strOtherCol = strOtherCol & "[fEwcMikakunin] ,"
strOtherCol = strOtherCol & "[fEwcCreateDate] ,"
strOtherCol = strOtherCol & "[fEwcCreateName] ,"
strOtherCol = strOtherCol & "[fEwcUpdateDate] ,"
strOtherCol = strOtherCol & "[fEwcUpdateName] "





paoluo 2007-07-06
  • 打赏
  • 举报
回复
沒看明白,什麼意思?

固定幾行之後要有幾行空格?
ZOU_SEAFARER 2007-07-06
  • 打赏
  • 举报
回复
也许是表有问题,应用的结果是


按照制定规律排列,不过每16条后面就有3条空格数据,和我以前做的另外方法一样,
A
A
A
A
A
A
A
A
.
.
.
B(16条)
3条空格
B
B
B
paoluo 2007-07-06
  • 打赏
  • 举报
回复
不是這麼寫的,你直接在數據庫中建立存儲過程,不需要用VB代碼去建立這個存儲過程。

你在VB中只需要調用SP_TEST這個存儲過程,並得到存儲過程的返回集即可。



你那樣改列為自增列是有問題的。

另外,存儲過程需要做小部分修改,將建表的語句改為和你實際表一樣即可,不要去管原表中的ROW是否是自增列了。

ZOU_SEAFARER 2007-07-06
  • 打赏
  • 举报
回复
SQL = "SELECT " 'Creat Empty Table 复制表结构
SQL = SQL & "* INTO "
SQL = SQL & Temp_Table
SQL = SQL & " FROM " & TEST
SQL = SQL & " WHERE "
SQL = SQL & " 1=2 "
If RunSQL(SQL) <> R_OK Then 'SQL実行
Call ErrMsg(1) 'エラーメッセージ
Exit Function 'サブルーチンの終了
End If



Call CreatTabSql("#T") '建立一个临时表,和原表一模一样,包括默认值等
SQL = "ALTER TABLE #T ALTER COLUMN fEwcRow INT IDENTITY(1,1)" 修改临时表的行字段为自增字段,原表是普通的INT字段
If RunSQL(SQL) <> R_OK Then 'SQL実行
Call ErrMsg(1) '
Exit Function '
End If

SQL = "Create ProceDure SP_TEST(@OrderBy Varchar(100))"
SQL = SQL & " As"
SQL = SQL & " Begin"
'SQL = SQL & " Create Table #T(ROW Int Identity(1, 1), [DESC] Varchar(10), fValue Int)"
SQL = SQL & " Insert #T (*) Select * From TEST Where Rtrim([fEwcDIA]) != '' Order By CharIndex([fEwcDIA], @OrderBy), fEwcRow"
SQL = SQL & " Insert #T (*) Select * From TEST Where Rtrim([fEwcDIA]) = '' Order By fEwcRow"
SQL = SQL & " Select * INTO " & Temp_Table & " FROM #T" '把结果导入一个表中
SQL = SQL & " Drop Table #T"
SQL = SQL & " End"
If RunSQL(SQL) <> R_OK Then 'SQL実行
Call ErrMsg(1) '
Exit Function '
End If

SQL = "EXEC SP_TEST 'V,M,P'"
If RunSQL(SQL) <> R_OK Then 'SQL実行
Call ErrMsg(1) '
Exit Function '
End If

这样我想Temp_Table表中就是我们的结果了!不知道是否可行?
paoluo 2007-07-06
  • 打赏
  • 举报
回复
现在就是转化到VB中了,还不晓得如何转化进去呢?
-----------------
我覺得直接修改原表的數據不是很好,所以這個存儲過程返回的是一個結果集,你直接在VB中調用存儲過程就可以。




还有 的问题就是我的确表中字段30多个
Create Table #T(ROW Int Identity(1, 1), [DESC] Varchar(10), fValue Int)
--------------

你原表中的第一列是否是自增列?還是普通Int列?

如果你的原表的第一列不是自增列,就需要在建表的時候將列一一寫出來,就和你建造你的原表一樣,也不算太麻煩。



ZOU_SEAFARER 2007-07-06
  • 打赏
  • 举报
回复
现在就是转化到VB中了,还不晓得如何转化进去呢?

还有 的问题就是我的确表中字段30多个
Create Table #T(ROW Int Identity(1, 1), [DESC] Varchar(10), fValue Int)


是不是要写很多了??
ZOU_SEAFARER 2007-07-06
  • 打赏
  • 举报
回复
paoluo(一天到晚游泳的鱼)



一天到晚游泳的鱼 是鲨鱼吗??? 鲨鱼不休息
vbman2003 2007-07-06
  • 打赏
  • 举报
回复
呵呵,鱼儿来了
我就学习吧
vbman2003 2007-07-06
  • 打赏
  • 举报
回复
比较笨的办法就是在order by语句中通过case语句给对应的字段值前加上一个序号,但这样一来语句会很庞杂,可读性不好。access有个swicth函数写起来还好看一点。SQL中不知道有没相应的东东。还有pgSQL可以在SQL语句中用数组,SQL应该也可以吧,这样的话可以将规则放在数组里,通过数组序号排序。手边没SQL数据库,你去SQL版问一下比较好,那里SQL语句牛人多多
paoluo 2007-07-06
  • 打赏
  • 举报
回复
--創建測試環境
Create Table TEST
(ROW Int,
[DESC] Varchar(10),
fValue Int)
Insert TEST Select 1, 'p', 15
Union All Select 2, 'p', 20
Union All Select 3, 'p', 23
Union All Select 4, 'v', 15
Union All Select 5, '', 16
Union All Select 6, 'v', 21
Union All Select 7, 'm', 22
Union All Select 8, 'm', 23
GO
--創建存儲過程
Create ProceDure SP_TEST(@OrderBy Varchar(100))
As
Begin
Create Table #T(ROW Int Identity(1, 1), [DESC] Varchar(10), fValue Int)
Insert #T ([DESC], fValue) Select [DESC], fValue From TEST Where Rtrim([DESC]) != '' Order By CharIndex([DESC], @OrderBy), ROW
Insert #T ([DESC], fValue) Select [DESC], fValue From TEST Where Rtrim([DESC]) = ''
Select * From #T
Drop Table #T
End
GO
--測試
EXEC SP_TEST 'V,M,P'
GO
--刪除測試環境
Drop Table TEST
Drop ProceDure SP_TEST
--結果
/*
ROW DESC fValue
1 v 15
2 v 21
3 m 22
4 m 23
5 p 15
6 p 20
7 p 23
8 16
*/
paoluo 2007-07-06
  • 打赏
  • 举报
回复
利用CharIndex函數來做,不是很複雜。
ZOU_SEAFARER 2007-07-06
  • 打赏
  • 举报
回复
2005 实现比较容易点,2000下我把 表里所有的V写入一个临时表
然后所有的M,在是所有的P,最后所有的空格数据写入临时表
然后在临时表里面增加一个自加一的自段,再把自加一字段的值付给行号,这样一来就就可以删除自加一字段,再把原表清空,把临时表数据导入,!!

看起来好象这样可以,实际上不能操作,因为

(表里所有的V写入一个临时表,然后所有的M,在是所有的P,最后所有的空格数据写入临时表)

这样写入临时表的数据在表里面的物理存储没有规律,最后写入的空格数据在表中任意出现,并不是追加到表的末尾,这个又比较不好做了!!

vbman2003 2007-07-06
  • 打赏
  • 举报
回复
这个弄起来比较烦,应该可以实现吧?
手边没MSSQL,想想先
ZOU_SEAFARER 2007-07-06
  • 打赏
  • 举报
回复
XIEXIE
clear_zero 2007-07-06
  • 打赏
  • 举报
回复
特别复杂的我就写存储过程了
paoluo 2007-07-06
  • 打赏
  • 举报
回复
算了,如果你實現了,就OK了。

其實這種東西,我習慣直接用數據庫來做。

PS:

我不是鯊魚,是魚 :)
ZOU_SEAFARER 2007-07-06
  • 打赏
  • 举报
回复
Insert #T ([DESC], fValue) Select [DESC], fValue From TEST Where Rtrim([DESC]) != '' Order By CharIndex([DESC], @OrderBy), ROW

我用循环了!!现在循环解决问题了,谢谢鲨鱼

for i = 0 to ubound(Sort)
Insert #T ([DESC], fValue) Select [DESC], fValue From TEST Where Rtrim([DESC]) = 'Sort(i)' Order By ROW
Runsql()................
next
ZOU_SEAFARER 2007-07-06
  • 打赏
  • 举报
回复
呵呵,数据库不归我做哈]

数据库别人设置好了,我只管写代码!
加载更多回复(3)
实验一 SQL*PLUS练习 【实验目的】 (1) 了解Oracle的工作环境和基本使用方法。 (2) 练习标准SQL的数据操作,查询命令及其查询优化。 (3) 学会使用高级SQL命令,排序、分组、自连接查询等。 (4) 学会使用SQL*PLUS命令显示报表,存储到文件等。 【实验内容】 一、 准备使用SQL*PLUS 1. 进入SQL*PLUS 2. 退出SQL*PLUS 3. 显示表结构命令DESCRIBE SQL>DESCRIBE emp 使用DESCRIBE(缩写DESC)可以列出指定表的基本结构,包括各字段字段名以及类型、长度、是否非空等信息。 4. 使用SQL*PLUS显示数据库中EMP表的内容 输入下面的查询语句: SQL>SELECT * FROM emp; 按下回车键执行查询 5. 执行命令文件 START或@命令将指定文件调入SQL缓冲区中,并执行文件内容。 SQL>@ 文件名(文件后缀缺省为.SQL)或 SQL>START 文件名 文件中每条SQL语句顺序装入缓冲区并执行。 二、 数据库命令——有关表、视图等的操作 1. 创建表employee 例1 定义一个人事信息管理系统中存放职工基本信息的一张表。可输入如下命令: SQL>CREATE TABLE employee (empno number(6) PRIMARY KEY, /* 职工编号 name varchar2(10) NOT NULL, /* 姓名 deptno number(2) DEFAULT 10, /* 部门号 salary number(7,2) CHECK(salarycreate table emp2 as select * from emp where 1=2; 在命令的where子句中给出1=2,表示条件不可能成立,因而只能复制表结构,而不能复制任何数据到新表中去。另外,还可以复制一个表的部分列定义或部分列定义及其数据。 三、 Oracle数据库数据查询 1、单表查询 2、多表查询 四、 SQL*PLUS常用命令 表1 常用报表格式化名命令 命令 定义 Btitle 为报表的每一页设置底端标题 Column 设置列的标题和格式 Compute 让SQL*PLUS计算各种值 Remark 将某些字标记为注释 Set linesize 设置报表的行宽字符数 Set newpage 设置报表各页之间的行数 Spool 使SQL*PLUS将输出写入文件中 Start 使SQL*PLUS执行一个sql文件 Ttitle 设置报表每页的头标题 Break 让SQL*PLUS进行分组操作 例3 建立一个批命令文件对查询到的数据以报表的形式输出并将其保存到指定的文件中。 处理方法:利用SQL*PLUS语言工具(也可以使用其他文本编辑器)建立批命令的.SQL文件。在“SQL>”提示符下,使用EDIT命令在”E:\”中建立SCGB.SQL文件。 SCGB.SQL文件中的命令组如下: SQL>EDIT E:\ SCGB.SQL SET echo off SET pagesize 30 SET linesize 75 TTITLE’2008年4月10号’CE’公司职员基本情况登记表’R’Page:’ FORMAT 99- >SQL.PNO SKIP 1 CE’===========================’ BTITLE COL 60 ’制标单位’ TAB 3 ‘人事部’ COLUMN empno heading ‘职工|编号’ COLUMN ename format a10 heading ‘姓 名’ COLUMN job heading ‘工 种’ COLUMN sal format $99,990 heading 工 资’ COLUMN comm Like sal heading ‘奖 金’ COLUMN deptno format 9999 heading ‘部门|编号’ COLUMN hiredate heading ‘参加工作时间’ SPOOL e:\sjbb /*在E盘中建立格式报表输出文件,默认属性为LST BREAK on deptno skip 1 COMPUTE sum of sal comm on deptno SELECT empno,ename,job,hiredate,sal,comm,deptno from emp ORDER BY deptno,sal; SPOOL off /*终止SPOOL功能,关闭其文件。注意,此命令不可省,否则将建立空文件。 五、 实验内容 1、以cs+学号为用户名创建用户,并授予用户创建数据对象的权限。 2、复制emp表,复制表名为emp_学号,然后将emp表中工资低于$2000 的职工插入到复制的表中。 3、对复制的emp表插入一行只包含有职工号,职工名,工资与部门号四个数据 项值的记录。 4、在复制的emp表中将雇员ALLEN提升为经理,工资增至$2500, 奖(佣 )金增加40%。 5、删除复制的emp表中工资低于500的记录行。 6、列出10号部门中既不是经理,也不是秘书的职工的所有信息。 7、查找出部门所在地是CHICAGO的部门的职工姓名、工资和工种。 8、统计各部门中各工种的人数、工资总和及奖金总和。 9、查找出工资比其所在部门平均工资高的职工姓名、工种与工资情况。 实验3 Oracle数据库开发环境下PL/SQL编程 【实验目的】 (1)掌握 PL/SQL 的基本使用方法。 (2)在SQL*PLUS环境下运行PL/SQL的简单程序。 (3)应用 PL/SQL 解决实际题 【实验内容与步骤】 PL/SQL块中的可执行部分是由一系列语句组成的(包括对数据库进行操作的SQL语句,PL/SQL语言的各种流程控制语句等)。在块中对数据库查询,增、删、改等对数据的操作是由SQL命令完成的。在PL/SQL块中,可以使用SQL的数据查询命令,数据操纵命令和事务控制命令。可使用全部SQL函数。PL/SQL中的SQL语句,可使用SQL的比较操作等运算符。但不能使用数据定义语句。 在PL/SQL块中使用SELECT语句时注意几点: (1)SELECT语句必须含有INTO子句。 (2)INTO子句后的变量个数和位置及数据类型必须和SELECT命令后的字段名表相同。 (3)INTO子句后可以是简单类型变量或组合类型变量。 (4)SELECT语句中的WHERE条件可以包含PL/SQL块中定义的变量及表达式,但变量名不要同数据库表列名相同。 (5)在未使用显式游标的情况下,使用SELECT语句必须保证只有一条记录返回,否则会产生异常情况。 [例3-1] 题:编写一个过程,求和运算。 SET SERVEROUTPUT ON; DECLARE a number:=1; BEGIN a:=a+5; DBMS_OUTPUT.PUT_LINE('和为:'||TO_CHAR(a)); END; / 【例3-2】:使用%TYPE声明变量,输出制定表中的相关信息。 DECLARE my_name student.sname%TYPE; BEGIN SELECT sname INTO my_name FROM student WHERE no=’01203001’; DBMS_OUTPUT.PUT_LINE(my_name); END; / 【例3-3】题:编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%。 declare v_sal emp.sal%type; begin select sal into v_sal from emp where ename=spName; if v_sal :NEW.sal THEN DBMS_OUTPUT.PUT_LINE('工资减少'); ELSIF :OLD.sal < :NEW.sal THEN DBMS_OUTPUT.PUT_LINE('工资增加'); ELSE DBMS_OUTPUT.PUT_LINE('工资未作任何变动'); END IF; DBMS_OUTPUT.PUT_LINE('更新前工资 :' || :OLD.sal); DBMS_OUTPUT.PUT_LINE('更新后工资 :' || :NEW.sal); END; / --执行UPDATE查看效果 UPDATE emp SET sal = 3000 WHERE empno = '7788'; 6、需要对在表上进行DML操作的用户进行安全检查,看是否具有合适的特权。 Create table foo(a number); Create trigger biud_foo Before insert or update or delete On foo Begin If user not in (‘DONNY’) then Raise_application_error(-20001, ‘You don’t have access to modify this table.’); End if; End; / 即使SYS,SYSTEM用户也不能修改foo表。 2、 利用PL/SQL编写程序实现下列触发器 1)、编写一个数据库触发器,当任何时候某个部门从dept表中删除时,该触发器将从emp表中删除该部门的所有雇员。(要求:emp表、dept表均为复制后的表) 2)、创建一个触发器,当客户下完订单后,自动统计该订单的所有图书的价格总额。 3)、创建一个触发器,禁止客户在非工作时间(早上8:00前,晚上17:00后)下订单。 五、实验心得

1,217

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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