雄辩与声明的表在查询中使用 [英] Eloquent with declared table to use in the query

查看:94
本文介绍了雄辩与声明的表在查询中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定Larvel 5.2和SQL Server 2005数据库。我有这样的查询:

  declare @mapping_table表(id int,name varchar(255),mapping_value varchar(255) )

插入到@mapping_table值(1,'foo','A1')
插入到@mapping_table值(2,'bar','A1')
insert (@,baz','A2')

选择distinct(lookup_field)
from production_table
其中lookup_field不在(
中选择名称
from @mapping_table

当我运行这个使用如此的口才: code> DB :: connection('sqlsrv') - > select($ query)我收到错误查询的活动结果不包含字段。 / code>



背景:我正在创建一个应用程序,确保某些查询产生一个有效的结果集用于报告。大多数这些ara数据映射为自由文本输入到定义的选项列表。如果在生产中找到一个新的自由文本条目,查询应该提到未映射的值,以便我可以将它们添加到定义的表中。



我没有写访问生产数据库,只读。由于此限制,创建存储过程不是一个选项。当我将查询复制到SQL Server Management Studio时,它的作用就像魅力一样。我猜这是因为SSMS在内部分离结果,而且没有说出来。



如何获得像给定的查询一样使用Eloquent?

解决方案

纯粹的猜测,但雄辩需要列名?



尝试

  select distinct(lookup_field)as lookup_field 


Given Larvel 5.2 and a SQL Server 2005 database. I have query something like this:

declare @mapping_table table(id int, name varchar(255), mapping_value varchar(255))

insert into @mapping_table values (1, 'foo', 'A1')
insert into @mapping_table values (2, 'bar', 'A1')
insert into @mapping_table values (3, 'baz', 'A2')

select distinct(lookup_field) 
from production_table
where lookup_field not in (
  select name
  from @mapping_table 
)

When I run this using Eloquent like so: DB::connection('sqlsrv')->select($query) I get the error The active result for the query contains no fields.

Background: I'm creating an app that ensures certain queries result in a valid result set for reporting purposes. Mostly these ara data mappings for free text input to a defined list of options. If a new free text entry is found in the production a query should mention the unmapped value(s) so I can add them to the defined table.

I have no writing access on the production database, only read. Creating stored procedures is not an option due to this limitation. When I copy the query to SQL Server Management Studio it works like charm. I'm guessing this is because SSMS internally seperates the results and Eloquent doesn't.

How can I get a query like the given one get to work with Eloquent?

解决方案

Pure guesswork, but does eloquent require column names ?

Try

Select distinct(lookup_field) as lookup_field

这篇关于雄辩与声明的表在查询中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆