+ Stored procedures database integrity Exercises

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
For the final exam, written on paper is a standard SQL:
Here Insert Picture Description
Here Insert Picture Description
here did not see the whole problem, do not know to ask to be converted to the third level, starting from the variable name do not know what, you think only the most simple A, B, C, D, E, so it is a coincidence that the variable name in line with the third question. .

T-SQL:
In order to experiment on SQL Server, here playing a direct code.

CREATE TABLE MathCount
(Level CHAR(10) PRIMARY KEY,
Countnum INT
);

INSERT INTO MathCount VALUES('90~100',0);
INSERT INTO MathCount VALUES('80~90',0);
INSERT INTO MathCount VALUES('70~80',0);
INSERT INTO MathCount VALUES('60~70',0);
INSERT INTO MathCount VALUES('<60',0);

IF(EXISTS(SELECT * FROM sys.objects WHERE NAME = 'SMath_Grade'))
  DROP PROCEDURE SMath_Grade;
GO
--以上语句,为删除同名的存储过程,刚开始,我以为没有同名的不需要删除,但是删除后,后面的CREATE会受影响,会有错误,说是CREATE语句为批处理语句。。什么什么的,于是后来又加上了。
CREATE PROCEDURE SMath_Grade
--和上述标准SQL一样,该存储过程没有参数
AS
BEGIN
DECLARE
 @levelA INT,
 @levelB INT,
 @levelC INT,
 @levelD INT,
 @levelE INT;

SELECT @levelA = count(*) --标准SQL这里用的INTO
FROM SC,Course
WHERE SC.Cno = Course.Cno AND Cname = '离散数学'
      AND (Grade >= 90 AND Grade <= 100);
SELECT @levelB = count(*) --标准SQL这里用的INTO
FROM SC,Course
WHERE SC.Cno = Course.Cno AND Cname = '离散数学'
      AND (Grade >= 80 AND Grade < 90);
SELECT @levelC = count(*) --标准SQL这里用的INTO
FROM SC,Course
WHERE SC.Cno = Course.Cno AND Cname = '离散数学'
      AND (Grade >= 70 AND Grade < 80);
SELECT @levelD = count(*) --标准SQL这里用的INTO
FROM SC,Course
WHERE SC.Cno = Course.Cno AND Cname = '离散数学'
      AND (Grade >= 60 AND Grade < 70);
SELECT @levelE = count(*) --标准SQL这里用的INTO
FROM SC,Course
WHERE SC.Cno = Course.Cno AND Cname = '离散数学'
      AND (Grade >= 0 AND Grade <  60);

UPDATE MathCount SET Countnum = @levelA WHERE Level = '90~100';
UPDATE MathCount SET Countnum = @levelB WHERE Level = '80~90';
UPDATE MathCount SET Countnum = @levelC WHERE Level = '70~80';
UPDATE MathCount SET Countnum = @levelD WHERE Level = '60~70';
UPDATE MathCount SET Countnum = @levelE WHERE Level = '<60';
--如果前面没有事先在表中插入数据,这里可以将更新改为插入
END;

EXEC SMath_Grade; --没有参数,后面不需要跟变量

SELECT * FROM MathCount;
SELECT * FROM SC;

Here Insert Picture Description
Here write performance range when, for the first time using BETWEEN ... AND ..., but this will overlap at the boundary, an error will occur, so it should be with comparison operators, above standard SQL where I do not bother changed. . .
Here Insert Picture Description
(2)
Here Insert Picture Description
course number to space some big
T-SQL:

CREATE TABLE Avg_Grade
(Cno CHAR(2),
Avggrade FLOAT
);

IF(EXISTS(SELECT * FROM sys.objects WHERE name = 'Pro_Avg_grade'))
  DROP PROCEDURE Pro_Avg_grade;
GO
CREATE PROCEDURE Pro_Avg_grade(@Cno CHAR(2))
--带参数,用于指定哪一门课程
AS
BEGIN
DECLARE
  @Agrade FLOAT;
SELECT Agrade = AVG(Grade) FROM SC
WHERE SC.Cno = @Cno;

INSERT INTO Avg_Grade VALUES(@Cno,@Agrade);
END;

EXEC Pro_Avg_grade
     @Cno = 5;

EXEC Pro_Avg_grade
     @Cno = 2;

SELECT * FROM Avg_Grade;

Here Insert Picture Description
(3)
Here Insert Picture Description
because the stored procedure, neither variables nor parameters, so the T-SQL and standard SQL is not much difference, run different

Here Insert Picture Description
The beginning of the time of writing, the name of the stored procedure have added '', so he has been given. .

[Experience]
in the last job to write a stored procedure that a bit difficult, few examples, there is no particularly deep impression, this time finished, clearly feeling a lot, but on a question to do is simply scratching their heads, reference is made to the students and did not know some, it is relatively easy to write back. Is to create a stored procedure can not remember the routine, always we need to move forward to double finishing notes.

Published 15 original articles · won praise 17 · views 5072

Guess you like

Origin blog.csdn.net/fu_GAGA/article/details/105312984