SQL 错误:由于绑定错误,无法使用视图或函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39599794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:52:53  来源:igfitidea点击:

Error: Could not use view or function because of binding errors

sqlsql-server-2008sql-view

提问by swathi

I got read only access to Views and when i am trying to query the View i got this error message. Can anyone help me understand what is the actual problem and how to fix it. FYI.. this is the 1st time i am viewing this table . Here is the error message.

我获得了对视图的只读访问权限,当我尝试查询视图时,我收到了此错误消息。任何人都可以帮助我了解实际问题是什么以及如何解决它。仅供参考.. 这是我第一次查看此表。这是错误消息。

Msg 207, Level 16, State 1, Line 1
Invalid column name 'ProductCategoryL2Name'.
Could not use view or function 'DB.Product' because of binding errors.

采纳答案by Russell Fox

It sounds like the view was created and then one of the underlying tables was changed. I.e., ProductCategoryL2Name no longer exists or was renamed. You can try this to get the view's definition, but the sys tables might be locked down. Your best bet is to go talk to whoever owns the database and ask them to fix it (which can be quite a rabbit hole in large organizations or on consulting gigs).

听起来像是创建了视图,然后更改了其中一个基础表。即,ProductCategoryL2Name 不再存在或已重命名。您可以尝试此操作来获取视图的定义,但 sys 表可能已被锁定。最好的办法是与拥有数据库的人交谈并要求他们修复它(这在大型组织或咨询工作中可能是一个相当大的漏洞)。

SELECT sm.definition
FROM [YourDB].sys.sql_modules AS sm  
    JOIN [YourDB].sys.objects AS o 
        ON sm.object_id = o.object_id  
WHERE sm.object_id = OBJECT_ID('YourDB.dbo.ViewName')

回答by Marcello Miorelli

when deploying changes to our systems it is not uncommon to get this error message Could not use view or function because of binding errors.

将更改部署到我们的系统时,收到此错误消息的情况并不少见,由于绑定错误,无法使用视图或函数

This happens because a view can become outdated because of changes to the underlying objects upon which the view depends.

发生这种情况是因为视图可能会因为视图所依赖的底层对象的更改而变得过时。

you can use sp_refreshviewto for correction - or better - prevention - if you do a sp_refreshview @viewname='the_view_I_have_just_changed'every time you change a view or function.

您可以使用sp_refreshview进行更正 - 或更好地 - 预防 - 如果您sp_refreshview @viewname='the_view_I_have_just_changed'每次更改视图或功能时都执行一次。

example:

例子:

use my_database

    exec sp_refreshview @viewname='dbo.vw_select_applicant'

    exec sp_refreshview @viewname='dbo.vw_search'



    execute as login='cola'
--to check the permissions
    select top 10 * from dbo.vw_select_applicant

    select top 10 * from dbo.vw_search