简体   繁体   English

为什么查询没有返回结果

[英]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.然而我的查询没有返回任何东西。

  • Check if the SpecializationCode exists in the Specialization table.检查SpecializationCode是否存在于Specialization表中。 If it does not exist, insert the SpecializationCode and SpecializationName into the Specialization table如果不存在,则将SpecializationCodeSpecializationName插入到Specialization表中
  • Insert doctor details into the Doctor table将医生详细信息插入Doctor
  • Insert the newly generated DoctorID and the given SpecializationCode into DoctorSpecialization table with SpecializationDate as current date将新生成的DoctorID和给定的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 BlockCATCH块中

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM