我在SQL Server中的视图在哪里?

时间:2014-01-27 11:58:56

标签: sql-server sql-server-2008-express

我有一个像这样的结构表:

sensorId int, Value int, obDate datetime

在SQL Server 2008 Express中。

我想获得每个传感器的最新值,所以我创建了一个像这样的视图:

CREATE VIEW [dbo].[LAT_POLDATA] 
AS (
    SELECT A.sensorId, A.obDate, A.Value 
    FROM [Met].[dbo].[POLDATA] AS A
    INNER JOIN 
        (SELECT sensorId, MAX(obDate) AS obDate 
         FROM [Met].[dbo].[POLDATA] 
         GROUP BY sensorId) AS B ON A.sensorId = B.sensorId 
                                 AND A.obDate = B.obDate 
)

现在如果我查询Select * from [dbo].[LAT_POLDATA]我在Microsoft SQL Server Management Studio中执行此操作时会获得值,但我在数据库中的Views下看不到此视图。

如果我从ASP.net服务查询它,查询将失败并抛出错误:

  

System.Data.SqlClient.SqlException:无效的对象名称'LAT_POLDATA'

观点在哪里?为什么我没有看到SSMS?

2 个答案:

答案 0 :(得分:2)

在Management Studio中,请尝试添加首选数据库名称并执行它。

use [yourDatabaseName]

Select * from [dbo].[LAT_POLDATA]

如果这样会给您一个错误,即您的数据库中没有该视图

然后按照

进行操作
USE [yourDatabasename]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

Create View [dbo].[LAT_POLDATA] AS (
    SELECT A.sensorId , A.obDate , A.Value 
    FROM [Met].[dbo].[POLDATA] AS A
    INNER JOIN (
        SELECT sensorId , MAX(obDate) AS obDate 
        FROM [Met].[dbo].[POLDATA] 
        GROUP BY sensorId  
    ) AS B
        ON A.sensorId  = B.sensorId 
        AND A.obDate  = B.obDate 
)

GO

答案 1 :(得分:0)

执行此查询并查看是否可以在此处找到它,此查询将返回在您的数据库中创建的所有视图

SELECT SCHEMA_NAME(schema_id) AS schema_name
 ,name AS view_name
 ,OBJECTPROPERTYEX(OBJECT_ID,'IsIndexed') AS IsIndexed
 ,OBJECTPROPERTYEX(OBJECT_ID,'IsIndexable') AS IsIndexable
 FROM sys.views
 WHERE name LIKE 'LAT_POLDATA%'
 GO