时间:2021-07-01 10:21:17 帮助过:45人阅读
CREATE TABLE "LUO"."LOGIN_RECORD" ( "USER_NAME" VARCHAR2(10) NOT NULL, "USER_PASSWD" VARCHAR2(10) NOT NULL, "RECORD_TIME" DATE DEFAULT sysdate NOT NULL, "SUCCESS" VARCHAR2(10) NOT NULL ) ; CREATE TABLE "LUO"."LOGIN" ( "USER_NAME" VARCHAR2(
CREATE TABLE "LUO"."LOGIN_RECORD" (
"USER_NAME" VARCHAR2(10) NOT NULL,
"USER_PASSWD" VARCHAR2(10) NOT NULL,
"RECORD_TIME" DATE DEFAULT sysdate NOT NULL,
"SUCCESS" VARCHAR2(10) NOT NULL
) ;
CREATE TABLE "LUO"."LOGIN" (
"USER_NAME" VARCHAR2(10) NOT NULL,
"USER_PASSWD" VARCHAR2(10) NOT NULL
) ;
CREATE OR REPLACE TRIGGER "LUO"."LOGIN_RECORD_TRIGER"
BEFORE INSERT ON "LUO"."LOGIN" FOR EACH ROW
BEGIN
INSERT INTO LOGIN_RECORD VALUES(
:new.USER_NAME,
:new.USER_PASSWD,
sysdate,
'HI'
);
END;
--创建存储过程
CREATE OR REPLACE PROCEDURE LOGIN_PROCEDURE(
usern IN LUO.LOGIN.USER_NAME%TYPE, passwd IN LUO.LOGIN.USER_PASSWD%TYPE
)AS
count_num INTEGER ;
BEGIN
SELECT COUNT(*)
INTO count_num
FROM LOGIN
WHERE LOGIN.USER_NAME = usern
AND LOGIN.USER_PASSWD = passwd;
IF count_num = 0 THEN
INSERT INTO "LUO"."LOGIN_RECORD" VALUES(usern,passwd,sysdate,'failure');
DBMS_OUTPUT.PUT_LINE('LOGIN FAILD!');
ELSE
INSERT INTO "LUO"."LOGIN_RECORD" VALUES(usern,passwd,sysdate,'success');
DBMS_OUTPUT.PUT_LINE('LOGIN SUCCESS!');
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('EXCEPTION OCCURED!');
ROLLBACK;
END LOGIN_PROCEDURE;
/
下面是sql server的语法:
--RecordLoginWithTrigerUseSql.SQL - Creates the LabTest database
--Author: Luo Weifeng
--Time : 2010-6-23
--All Rights Reserved.
--切换到master数据库
USE master
-- 查询有没有以我们期望的数据名为名的数据库,存在则删除
if exists (select * from sysdatabases where name='LabTest')
begin
raiserror('Dropping existing LabTest database ....',0,1)
DROP database LabTest
end
GO
-- 创建数据库(大小等用默认值)
CREATE DATABASE LabTest
GO
-- 进入新建数据库创建表
USE LabTest
GO
-- 检查是否正确
if db_name() <> 'LabTest'
raiserror('Error in RecordLoginWithTrigerUseSql.SQL, ''USE LabTest'' failed! Killing the SPID now.'
,22,127) with log
GO
--创建历史记录表
CREATE TABLE LOGIN_RECORD (
"USER_NAME" VARCHAR(10) NOT NULL,
"USER_PASSWD" VARCHAR(10) NOT NULL,
"RECORD_TIME" DATE NOT NULL DEFAULT( getdate() ),
"SUCCESS" VARCHAR(10) NOT NULL
)
Go
--创建用户名/密码表
CREATE TABLE "LOGIN" (
"USER_NAME" VARCHAR(10) NOT NULL,
"USER_PASSWD" VARCHAR(10) NOT NULL
)
GO
-- 检查出错
raiserror('Now at the create trigger div ...',0,1)
GO
-- 创建触发器
CREATE TRIGGER LOGIN_RECORD_TRIGER
ON LOGIN
FOR INSERT
AS
BEGIN
DECLARE @new_name VARCHAR(10);
DECLARE @new_passwd VARCHAR(10);
SET @new_name = (SELECT INSERTED.USER_NAME FROM INSERTED)
SET @new_passwd = (SELECT INSERTED.USER_PASSWD FROM INSERTED)
INSERT INTO LOGIN_RECORD VALUES(
@new_name,
@new_passwd,
getdate(),
'HI'
);
END
写的不好,拿出来见笑了,只为那些需要的人提供一些东东吧。