这个TSQL查询的相应HQL查询是什么?

时间:2014-07-25 15:06:30

标签: sql sql-server hibernate tsql hql

我希望在HQL中重写TSQL查询。这是我想要转换为HQL查询的T-SQL查询。以下T-SQL查询工作正常。

DECLARE @D_Id INT = 120
DECLARE @DR_Id INT = 1100

IF @D_Id <> 120 
BEGIN 
    SELECT 
        d.D_Name 
    FROM 
        Department AS d 
    WHERE 
        d.D_Id = @D_Id
END 
ELSE 
BEGIN 
    SELECT 
        DR.DR_DepartmentOther 
    FROM 
        DataRequest AS DR 
    WHERE 
        DR.DR_Id = @DR_Id
END

我想要相应的HQL查询。我编写了以下HQL查询:

CASE
    WHEN D_Id <> 120
        SELECT 
            D.D_Name 
        FROM 
            Department AS D 
        WHERE 
            D.D_Id = D_Id
    WHEN D_Id = 120
        SELECT 
            DR.DR_DepartmentOther 
        FROM 
            DataRequest AS DR 
        WHERE 
            DR.DR_Id = DR_Id
END

当我执行上面的HQL查询时,我收到以下错误:

  

执行Hibernate查询时出错。

     

java.sql.SQLException:[Macromedia] [SQLServer JDBC   驱动程序] [SQLServer]关键字&#39; SELECT&#39;。

附近的语法不正确

1 个答案:

答案 0 :(得分:1)

试试这个:

CASE
    WHEN D_Id <> 120 THEN 
        (SELECT 
            D.D_Name 
        FROM 
            Department AS D 
        WHERE 
            D.D_Id = D_Id)
    WHEN D_Id = 120 THEN 
        (SELECT 
            DR.DR_DepartmentOther 
        FROM 
            DataRequest AS DR 
        WHERE 
            DR.DR_Id = DR_Id)
END