简体   繁体   English

如何使用Perl和DBD :: ODBC从SQL Server查询视图?

[英]How do I query a view from SQL server with Perl and DBD::ODBC?

I can query the SQL server DB fine. 我可以查询SQL Server数据库很好。 The problem happens when I try and query a view. 当我尝试查询视图时会发生问题。

I'm not trying to do anything crazy: 我不是想做任何疯狂的事情:

 $sql = 'select * from location_v';
 $stj = $db_destination->prepare($sql);

It keeps dying on the prepare line. 它一直死在准备线上。 Here's what I'm getting back (which isn't all that useful): 这就是我要返回的内容(不是那么有用):

DBD::ODBC::db prepare failed: (DBD: st_prepare/SQLPrepare err=-1)

Shouldn't views be handled exactly the same as a table? 视图是否应该与表完全一样地处理? Thanks in advance. 提前致谢。

Did you try using the dbo. 您是否尝试过使用dbo。 prefix on the view name (SELECT * FROM dbo.location_v)? 视图名称的前缀(SELECT * FROM dbo.location_v)? Did you check that the view is actually in the dbo. 您是否检查过视图实际上在dbo中。 schema? 架构? Did you check permissions on the view and/or the base table(s) the view is selecting from? 您是否检查了视图和/或从中选择视图的基表的权限? Sadly ODBC does not give a very meaningful error message here, so it could be any number of things, but it is most likely either object not found (because of the prefix, or you are in the wrong database) or permissions. 遗憾的是,ODBC在这里没有给出非常有意义的错误消息,因此它可能有很多东西,但是很可能找不到对象(由于前缀,或者您使用的数据库错误)或权限。

Yes, views should be indistinguishable from tables for that query. 是的,该查询的视图应该与表没有区别。

Can you actually query the view (rather than just the database in general)? 您是否可以实际查询视图(而不是一般的数据库)?

An error code of -1 is not strongly indicative of anything specific as a problem. 错误代码-1不能强烈表示特定于问题的任何内容。 It can sometimes be 'no permission', but it could be almost anything. 有时可以是“未经许可”,但几乎可以是任何东西。

I suggest looking to see where the problem is by using DBI_TRACE=9 (or maybe smaller numbers) set in the environment (or use $dbh->trace(9) ). 我建议通过使用在环境中设置的DBI_TRACE = 9(或可能是较小的数字)来查看问题出在哪里(或使用$dbh->trace(9) )。 This should give you lots of information (not all of it comprehensible) about what is going on; 这应该为您提供有关正在发生的事情的大量信息(并非所有信息都是可理解的)。 it may show you where the trouble actually is. 它可能会告诉您问题出在哪里。

Arg. 精氨酸。 I found the problem. 我发现了问题。 I had a few queries before I query the view and I wasn't closing my handle before opening a new one. 在查询视图之前,我进行了一些查询,但在打开新视图之前没有关闭句柄。 All I needed to do was this before I ran the query: 我需要做的就是在运行查询之前执行以下操作:

undef $stj;

Hopefully this helps someone else. 希望这可以帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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