Why we need refresh view after change table

本文介绍了当基础表结构发生变化时,如何更新SQL视图以反映这些变化。通过使用`sp_refreshview`等方法,可以避免因视图元数据未同步而导致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.mssqltips.com/tip.asp?tip=1427

 

Problem
I recently added a column to one of my core system tables referenced by a reporting view. When I run the view, the added column is not appearing in my result set! What can I do?

Solution
When a view is created in SQL Server, metadata for the referenced table columns (column name and ordinal position) is persisted in the databas e . Any change to the referenced base table(s) (column re-ordering, new column addition, etc) will not be reflected in the view until the view is either:

  • Altered with an ALTER VIEW statement
  • Recreated with DROP VIEW/CREATE VIEW statements
  • Refreshed using system stored procedure sp_refreshview

    The following example creates a table and an associated view

    create table dbo.Customer
    (
    customer_id int identity(1,1) not null primary key,
    firstname nvarchar(40) not null,
    lastname nvarchar(40) not null,
    birthdate datetime null
    )
    go

    insert into dbo.Customer (firstname, lastname, birthdate)
    select 'George', 'Washington', '1950-07-01'

    insert into dbo.Customer (firstname, lastname, birthdate)
    select 'James', 'Madison', '1948-11-09'

    insert into dbo.Customer (firstname, lastname, birthdate)
    select 'Alexander', 'Hamilton', '1970-03-02'
    go

    create view dbo.v_Customer
    as
    select * from dbo.Customer
    go



    Running the view using select * from dbo.v_Customer, we see the following output:



    A new business requirement has now been identified; we need to add a last order date to keep track of the last time this customer ordered stock from our warehouse:

    alter table dbo.Customer
    add last_order_date datetime null
    go
    exec sp_help [dbo.Customer]
    go


    As we can see, the last_order_date has been added to the table.  However, running our view again we get the same output as before the column change!



    This occurs because the view's metadata information about the table needs to be updated; it's not maintained automatically. This can be corrected using one of the methods mentioned at the beginning of this tip. My preference is to use sp_refreshview since I don't have to hunt down the view and issue a DROP VIEW/CREATE VIEW and re-grant permissions or issue an ALTER VIEW statement:

    exec sp_refreshview [dbo.v_customer]
    go
    select * from dbo.v_customer
    go

    As you can see, the view now returns all columns including the newly added one.



    I find that this can always be avoided by not issuing "SELECT *" in your view definitions. By explicitly defining a column list, you're forced to change any views that reference tables that may require a change. Another approach is to issue your CREATE VIEW statements with a SCHEMABINDING option. Views defined with this option do not allow SELECT * syntax (you'll receive an error if you try) and forces you to enter an explicit column list. This way, you can prevent a less experienced developer from changing a view to use this syntax

    If you're curious about how your view metadata is being stored, you can take a peek at the INFORMATION_SCHEMA.COLUMNS view (view column information is kept there as well as table column information).

    Next Steps

    • Examine your views and consider changing SELECT * syntax to explicit column lists, if possible
    • Read more about sp_refreshview in greater detail in the SQL Server 2000 and 2005 Books Online
    • Read more about SCHEMABINDING option in greater detail in the SQL Server 2000 and 2005 Books Online
    • Read more about INFORMATION_SCHEMA.COLUMNS view in greater detail in the SQL Server 2000 and 2005 Books Online
内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
<think>我们正在讨论的是BootstrapTablerefresh方法。根据引用,我们知道refresh方法用于异步刷新表格数据,并且在刷新完成后可能需要执行回调函数。同时,引用中提到,在onPostBody事件中可能无法访问到某些对象,因此需要使用其他方式(如通过变量)来判断刷新完成并执行代码。问题:如何在BootstrapTable中使用refresh函数更新表格数据?步骤:1.初始化BootstrapTable。2.使用refresh方法更新数据,并了解如何传递新的数据源。3.如何在刷新完成后执行回调函数(即刷新完成后的操作)?根据BootstrapTable的文档,refresh方法有两个可选参数:-params:一个对象,包含要发送到服务器的额外参数(当使用ajax方式时)。-options:一个对象,可以设置silent为true来静默刷新(不重新加载数据,仅刷新视图),或者设置url来改变数据源地址。但是,如果我们只是要更新数据(比如从本地获取了新数据),我们可以使用:$('#table').bootstrapTable('load',data);//直接加载新数据//或者使用refresh来重新从服务器获取数据(如果配置了url)$('#table').bootstrapTable('refresh');然而,refresh方法通常用于重新从服务器加载数据(当表格配置了url时)。如果我们已经获得了新数据(比如通过ajax请求),我们可以直接使用load方法加载。但是,问题中提到了“refresh函数”,所以我们重点讲解refresh。另外,refresh方法还支持回调。但是,在官方文档中,refresh方法本身并不直接提供回调参数。那么如何知道刷新完成呢?我们可以通过监听BootstrapTable的事件来实现,例如:-使用'on-post-body'事件(但引用[1]提到可能调用不到需要的对象,所以需要谨慎)-使用'on-load-success'事件,该事件在数据加载成功时触发。但是,注意:refresh方法会触发ajax请求,然后触发'on-load-success'事件。因此,我们可以这样:示例:1.初始化表格:```javascript$('#table').bootstrapTable({url:'data.json',columns:[...],onLoadSuccess:function(data){//数据加载成功,包括refresh触发的加载也会触发这里console.log('数据加载完成');}});```2.调用refresh刷新数据:```javascript$('#table').bootstrapTable('refresh');```但是,如果我们只想在某个特定的refresh操作后执行回调,而不是每次加载都执行,我们可以使用以下方法:方法一:在refresh之前解绑事件,然后再绑定(不推荐,因为可能影响其他事件处理)方法二:使用jQuery的one方法绑定一次性的onLoadSuccess事件(推荐):```javascript//先解绑,避免重复绑定(如果之前已经绑定过)//然后使用one绑定一次$('#table').one('load-success.bs.table',function(e,data){//这里写刷新完成后的回调console.log('本次刷新完成');});$('#table').bootstrapTable('refresh');```注意:事件名称是'load-success.bs.table',这是BootstrapTable的事件命名空间。另外,引用[1]中提到使用`postBodyResult`变量再判断执行一些代码,这是因为在`onPostBody`事件中可能访问不到某些对象。所以,我们通常使用`onLoadSuccess`事件来处理刷新完成后的操作。如果我们没有使用ajax,而是使用本地数据,那么我们可以使用`refresh`方法并设置`silent`为true来刷新视图(不重新请求数据):```javascript$('#table').bootstrapTable('refresh',{silent:true});```但是,如果我们更新了本地数据,通常使用`load`方法重新加载数据即可。总结:使用`refresh`方法更新表格数据(从服务器重新加载):```javascript$('#table').bootstrapTable('refresh');```如果需要在刷新完成后执行操作,可以监听`load-success.bs.table`事件(一次性监听可以使用`one`方法)。示例代码:HTML结构(参考引用[2]):```html<tableid="table"data-toggle="table"data-url="data.json"data-height="460"><thead><tr><thdata-field="id">ID</th><thdata-field="name">Name</th><thdata-field="price">Price</th></tr></thead></table>```JavaScript:```javascript//初始化表格(通过HTML属性已经初始化了)//刷新按钮点击事件$('#refreshButton').click(function(){//刷新前绑定一次性事件$('#table').one('load-success.bs.table',function(e,data){alert('刷新完成!数据条数:'+data.length);});$('#table').bootstrapTable('refresh');});```注意:如果刷新过程中出现错误,可以监听`onLoadError`事件来处理。另外,如果我们需要传递额外的参数到服务器,可以:```javascript$('#table').bootstrapTable('refresh',{query:{date:'2018-06-13',type:'month'}});```服务器端将接收到date和type参数。以上就是在BootstrapTable中使用refresh函数更新表格数据的方法。引用说明:[^1]:参考了BootstrapTable的事件文档,特别是onPostBody事件和onLoadSuccess事件。[^2]:参考了提供的HTML结构,用于表格的初始化。相关问题:1.如何给BootstrapTable传递自定义参数进行刷新?2.BootstrapTable的load方法和refresh方法有什么区别?3.如何处理BootstrapTable刷新时的错误?</think>### Bootstrap Table 的 `refresh` 函数使用指南 在 Bootstrap Table 中,`refresh` 函数用于动态更新表格数据,支持**异步刷新**和**回调执行**。以下是具体实现方法和示例: --- #### 一、核心用法 ##### 1. 基本刷新(重新加载数据) ```javascript $('#dataTable').bootstrapTable('refresh'); ``` - 重新请求配置的 `url` 数据源 - 自动更新表格视图 ##### 2. 带参数的刷新 ```javascript $('#dataTable').bootstrapTable('refresh', { query: { date: $('#calendar').val(), // 获取表单值 type: $('#types').val() // 获取下拉框值 } }); ``` - 通过 `query` 传递新参数到后端 - 适合动态过滤数据(如日期筛选)[^2] --- #### 二、异步刷新后的回调执行 当需要**在数据加载完成后执行操作**时(如更新统计信息),使用事件监听: ```javascript $('#dataTable').bootstrapTable('refresh', { query: { type: 'month' } }); // 监听数据加载完成事件 $('#dataTable').on('post-body.bs.table', function(e, data) { console.log("新数据已加载:", data); // 在此添加回调逻辑 updateSummary(data); // 自定义回调函数 }); ``` - `post-body.bs.table` 是官方推荐的回调事件[^1] - 参数 `data` 包含新加载的数据集 --- #### 三、完整示例(结合 AJAX) ```html <div class="tableDisplay" id="dataTable"> <table id="myTable"></table> </div> <script> // 初始化表格 $('#myTable').bootstrapTable({ url: '/api/getData', columns: [{ field: 'id', title: 'ID' }, { field: 'name', title: '名称' }] }); // 按钮触发刷新 $('#refreshBtn').click(function() { $('#myTable').bootstrapTable('refresh', { query: { date: $('#calendar').val(), type: $('#types').val() }, silent: true // 静默刷新不显示加载动画 }); }); // 刷新完成回调 $('#myTable').on('load-success.bs.table', function(e, data) { alert(`已加载 ${data.length} 条新数据`); }); </script> ``` --- #### 关键注意事项 1. **事件选择** - `load-success.bs.table`:数据加载成功时触发 - `post-body.bs.table`:DOM渲染完成后触发(操作DOM元素时用)[^1] - 避免使用 `onPostBody`(旧版可能有作用域问题) 2. **性能优化** - 添加 `silent: true` 参数避免过度渲染动画 - 使用 `cache: false` 禁用缓存确保获取最新数据 3. **与 `load` 的区别** ```javascript // refresh:基于配置的url重新请求 $('#table').bootstrapTable('refresh'); // load:直接加载本地数据 $('#table').bootstrapTable('load', localDataArray); ``` > 参考官方文档:[Bootstrap Table 事件文档](https://bootstrap-table.com/docs/api/events/) ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值