[英]Why does query return no results
I am trying to write a SQL query to fulfil the below requirements.我正在尝试编写 SQL 查询来满足以下要求。 All the naming conventions are correct for the tables, columns, and values.
表、列和值的所有命名约定都是正确的。 Yet my query does not return anything.
然而我的查询没有返回任何东西。
SpecializationCode
exists in the Specialization
table.SpecializationCode
是否存在于Specialization
表中。 If it does not exist, insert the SpecializationCode
and SpecializationName
into the Specialization
tableSpecializationCode
和SpecializationName
插入到Specialization
表中Doctor
tableDoctor
表DoctorID
and the given SpecializationCode
into DoctorSpecialization
table with SpecializationDate
as current dateDoctorID
和给定的SpecializationCode
插入到DoctorSpecialization
表中,并将SpecializationDate
作为当前日期CREATE PROCEDURE usp_AddDoctorDetails
(@DoctorName VARCHAR(25),
@SpecializationCode CHAR(3),
@SpecializationName VARCHAR(20))
AS
BEGIN
DECLARE @DoctorID INT
SELECT @DoctorID = DoctorID
FROM Doctor
WHERE DoctorName = @DoctorName
BEGIN TRY
IF NOT EXISTS (SELECT SpecializationCode FROM Specialization
WHERE @SpecializationCode = SpecializationCode)
BEGIN
INSERT INTO Specialization (SpecializationCode, SpecializationName)
VALUES (@SpecializationCode, @SpecializationName)
INSERT INTO Doctor (DoctorID, DoctorName)
VALUES (@DoctorID, @DoctorName)
INSERT INTO DoctorSpecialization (DoctorID, SpecializationCode, SpecializationDate)
VALUES (@DoctorID, @SpecializationCode, GETDATE())
RETURN -1
END
END TRY
BEGIN CATCH
RETURN -99
END CATCH
END
GO
Replace Return -1
with SELECT 1 / 0 AS Error;
将
Return -1
替换为SELECT 1 / 0 AS Error;
in TRY
Block Replace Return -99
with ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
在
TRY
块中将Return -99
替换为ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
in CATCH
Block在
CATCH
块中
Your procedure look like this after changes with only TRY CATCH block仅使用 TRY CATCH 块进行更改后,您的过程如下所示
BEGIN try
IF NOT EXISTS (SELECT specializationcode
FROM specialization
WHERE @SpecializationCode = specializationcode)
BEGIN
INSERT INTO specialization
(specializationcode,
specializationname)
VALUES (@SpecializationCode,
@SpecializationName)
INSERT INTO doctor
(doctorid,
doctorname)
VALUES (@DoctorID,
@DoctorName)
INSERT INTO doctorspecialization
(doctorid,
specializationcode,
specializationdate)
VALUES (@DoctorID,
@SpecializationCode,
Getdate())
SELECT
1 / 0 AS Error;
END
END try
BEGIN catch
SELECT
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END catch
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.