Question

I've done quite a bit of google searching on this topic already, and I do know it is a repetitive issue. However, here is my query:

SELECT 
    i.tag_name, i.value, i.aggregate_id, i.aggregate_interval, i.time_stamp
FROM 
    OPENQUERY(odbcclient, 
              'Select 
                   time_stamp, tag_name, value, 
                   aggregate_id, aggregate_interval 
               from 
                   canarydata.data 
               where 
                   aggregate_id = ''TimeAverage2'' 
                   and aggregate_interval = ''1:00:00:00'' 
                   and tag_name LIKE ''%SearchVal%'''
              ) AS i

When I run this query it executes fine. Once I try to create it into a view though, I receive the following error message:

Error Source: .Net SqlClient Data Provider
Error Message: Cannot find either column "time_stamp" or the user-defined function or aggregate "time_stamp.ToString", or the name is ambiguous.

How can I rewrite my query to function in a view?

Also, what is the difference between executing this from a script vs executing this query in a view?

Was it helpful?

Solution

I am going to make a guess from the last time I ran into something similar(ish). The View Designer for SQL Server will fail on some things that are supported by the database, and I believe aspects of using openquery in the view designer is one of those.

If I am correct (and you are not already trying to script it like below), you should be able to create the view via scripting it directly, something like:

CREATE  VIEW dbo.myView AS

    SELECT i.tag_name, i.value, i.aggregate_id, 
           i.aggregate_interval, i.time_stamp
    FROM OPENQUERY(odbcclient, 
       'Select time_stamp, tag_name, value, 
               aggregate_id,aggregate_interval 
        from canarydata.data 
        where aggregate_id = ''TimeAverage2'' 
              and aggregate_interval = ''1:00:00:00'' 
              and tag_name LIKE ''%SearchVal%''') 
    AS i
GO

OTHER TIPS

Full Command after Matthews suggestion:

USE [ODBC]
GO

 /****** Object:  View [dbo].[Averages]  ******/
 SET ANSI_NULLS ON
 GO

 SET QUOTED_IDENTIFIER ON
 GO

 CREATE View [dbo].[Averages] AS

 SELECT     i.time_stamp, i.tag_name, i.value, 
            i.aggregate_id, i.aggregate_interval
 FROM       OPENQUERY(odbcclient, 
                      'Select time_stamp, tag_name, value, 
                              aggregate_id, aggregate_interval 
                       from data.data 
                       where aggregate_id = ''TimeAverage2'' 
                             and aggregate_interval = ''1:00:00:00'' 
                             and tag_name LIKE ''%Point%''')
            AS i
 GO
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top