首页 > 解决方案 > 在 Laravel 中执行自定义查询

问题描述

我尝试执行这样的查询

@type = 'hobby'

IF @type = 'hobby'

    BEGIN

        SELECT m.id, m.name, m.address, p.name as parentName into #tempPeople from man m join parent p = m.parentId = p.id

        SELECT h.id, h.manId, h.hobbyName, h.timeSchedule, tp.name, tp.parentName from hobby h 
        join #tempPeople tp on h.manId = tp.id

        drop table #tempPeople

    END

ELSE 

  BEGIN  

        SELECT m.id, m.name, m.address, p.name as parentName into #tempPeople from man m join parent p = m.parentId = p.id

        SELECT gf.id, gf.manId, gf.name, gf.address, tp.name, tp.parentName from girlFriend gf
        join #tempPeople tp on gf.manId = tp.id

    END

我的laravel代码是这样的

 public static function getManControl($type)
    {
        $sql = "
        declare @type varchar(100)

        @type = '".$type."'

        IF @type = 'hobby'

            BEGIN

                SELECT m.id, m.name, m.address, p.name as parentName into #tempPeople from man m join parent p = m.parentId = p.id

                SELECT h.id, h.manId, h.hobbyName, h.timeSchedule, tp.name, tp.parentName from hobby h 
                join #tempPeople tp on h.manId = tp.id

                drop table #tempPeople

            END

        ELSE 

          BEGIN  

                SELECT m.id, m.name, m.address, p.name as parentName into #tempPeople from man m join parent p = m.parentId = p.id

                SELECT gf.id, gf.manId, gf.name, gf.address, tp.name, tp.parentName from girlFriend gf
                join #tempPeople tp on gf.manId = tp.id

            END
        ";

        $result = DB::select($sql);
        return $result;
    }

上面代码的结果,得到这样的异常

PDOException: SQLSTATE[IMSSP]: 查询的活动结果不包含任何字段。

如何在 laravel 中运行该查询?我应该用什么?

标签: sqlsql-serverlaravellaravel-query-builder

解决方案


推荐阅读