从我的表视图刷新我的查询结果

时间:2012-07-24 02:45:18

标签: sql sql-server-2008

SET ANSI_NULLS ON
 GO

SET QUOTED_IDENTIFIER ON
 GO


ALTER VIEW [dbo].[pais2k1view]
AS
SELECT DISTINCT 
                  RTRIM(dbo.Gen_info.last) AS LastName, RTRIM(dbo.Gen_info.first) AS         Firstname, RTRIM(dbo.Gen_info.middle) AS Middlename, RTRIM(dbo.Gen_info.rank) AS rank, 
                  RTRIM(dbo.Gen_info.badge_no) AS badge_no, RTRIM(dbo.Gen_info.tin) AS TIN, CONVERT(char(10), dbo.Gen_info.birthdate, 110) AS birthdate, dbo.Gen_info.hair, 
                  dbo.Gen_info.eyes, RTRIM(dbo.Gen_info.blood) AS Blood, dbo.Gen_info.height, dbo.Gen_info.weight, dbo.Gen_info.marks, dbo.dependent.name, 
                  dbo.dependent.address, dbo.dependent.phone_no, dbo.dependent.dep_contact, dbo.dependent.dep_phone, dbo.Gen_info.acct_no, dbo.Gen_info.unit AS RDO_Code, 
                  dbo.Gen_info.fstatus, dbo.dependent.tag1, RTRIM(dbo.Gen_info.first) + ' ' + RTRIM(dbo.Gen_info.middle) + ' ' + RTRIM(dbo.Gen_info.last) AS Fullname, 
                  RTRIM(dbo.Gen_info.last) + RTRIM(dbo.Gen_info.first) AS Photolink, dbo.Unit.abbvr AS unit
FROM         dbo.Gen_info WITH (NOLOCK) LEFT OUTER JOIN
                  dbo.dependent WITH (NOLOCK) ON dbo.Gen_info.link = dbo.dependent.link LEFT OUTER JOIN
                  dbo.Unit WITH (NOLOCK) ON dbo.Gen_info.unit = dbo.Unit.unit
WHERE     (dbo.dependent.fstatus = 0) AND (dbo.dependent.tag1 = 1) AND (dbo.Gen_info.fstatus = 0)

我正在使用sql management studio,这是我的修改视图,如何向此行添加刷新命令,以便视图始终反映最新的记录

2 个答案:

答案 0 :(得分:1)

你不必这样做。视图不是表,它们是预定义的查询。

当您从视图中选择时,SQL Server会在那时执行视图的查询。

您可以通过创建视图,从中进行选择,然后向基表添加行并再次从视图中进行选择来对此进行测试。

答案 1 :(得分:0)

如果对基础表进行更改,则需要刷新视图以查看反映的更改,但我很确定您无法在alter语句中执行此操作。

sp_refreshview [dbo].[pais2k1view]

然后,您可以在对数据库进行更改时手动运行它,或者将其包含在脚本中,您必须在调用查询查询之前将其自动化到某个位置。