Wonderware Historian SQL Server INSQL - sql-server

I am trying to get the definition of a view which contains historical values of different tags in Wonderware Historian. The target is to identify the source tables and enable CDC on those, in order to enable data streaming to a remote server. But when I look at view definition it shows something like
Select *
From [INSQL].Runtime.dbo.History
But History is the view itself. Attached the image from DBeaver for this:
Any hints? Looks like it has something to do with this [INSQL]

To find the tables referenced by a view a few options are available, probably the easiest is to use sql_expression_dependencies
select referenced_entity_name
from sys.sql_expression_dependencies
where referencing_id=Object_Id('View name')

Related

Unable to get definition for Sales.vw_CustomerOrders view in AdventureWorks2017 sample database

Looking at the AdventureWorks2017 database I ran across a view called Sales.vw_CustomerOrders. Wondering where it was getting its data I tried to query the object definition and the results came back NULL
SELECT definition
FROM sys.sql_modules
WHERE object_id = Object_id('Sales.vw_CustomerOrders')
I noticed the icon is slightly different from the other views and SSMS won't allow you to generate a create statement either (only a drop). What is this particular view? Why can't you generate a create statement for it? How can I find out how it gets its data?
TIA
As #MeyssamToluie mentioned in comments, view Sales.vw_CustomerOrders is not part of the out-of-box AdventureWorks database.
The different SSMS icon indicates the object is encrypted. When a view is created using WITH ENCRYPION option, the definition is obfuscated, not visible in the sys.sql_modules system view, and the CREATE cannot be scripted via normal methods. You'll find undocumented/unsupported ways to get the clear text definition of obfuscated objects with an internet search.
Use OBJECTPROPERTY to identify the encryption status of the view:
SELECT OBJECTPROPERTY(object_id, 'IsEncrypted') AS IsEncrypted, *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'Sales.vw_CustomerOrders');

Mule - Record cannot be mapped as it contains multiple columns with the same label

I need to do join query to MS SQL Server 2014 DB based on a column name value. The same query runs when doing query directly to DB, but when doing query through Mule I'm getting error. The query looks something like this :
SELECT * FROM sch.emple JOIN sch.dept on sch.emple.empid = sch.dept.empid;
The above query work fine while doing query directly to MS SQL Server DB, but gives the following error through mulesoft.
Record cannot be mapped as it contains multiple columns with the same label. Define column aliases to solve this problem (java.lang.IllegalArgumentException). Message payload is of type: String
Request you to please help me out.
Specify columns list directly:
SELECT e.<col1>, e.<col2>, ...., d.<col1>,...
FROM sch.emple AS e
JOIN sch.dept AS d
ON e.empid = d.empid;
Remarks:
You could use aliases instead of schema.table_name
SELECT * in production code in 95% cases is bad practice
The column that has duplicate is empid(or more). You could add alias for it e.empid AS emple_empid and d.empid AS dept_empid or just specify e.empid once.
To avoid specifying all columns manually, you could drag and drop them from object explorer to query pane like Drag and Drop Column List into query window.
Second way is to use plugin like Redgate Prompt to expand SELECT *:
Image from: https://www.simple-talk.com/sql/sql-tools/sql-server-intellisense-vs.-red-gate-sql-prompt/
Addendum
But the same query works directly.
It works because you don't bind them. Please read carefully link I provided for SELECT * antipattern and especially:
Binding Problems
When you SELECT *, it's possible to retrieve two columns of the same name from two different tables. This can
often crash your data consumer. Imagine a query that joins two
tables, both of which contain a column called "ID". How would a
consumer know which was which? SELECT * can also confuse views (at
least in some versions SQL Server) when underlying table structures
change -- the view is not rebuilt, and the data which comes back can
be nonsense. And the worst part of it is that you can take care
to name your columns whatever you want, but the next guy who comes
along might have no way of knowing that he has to worry about adding a
column which will collide with your already-developed names.
But the same query works directly.
by Dave Markle

How to get the correct DB2/400 library(database) using Squeryl Client

I've installed Squeryl Client and can easily access my iSeries DB2/400 and select and see the data within the tables. However, it seems I have to modify the URL in the alias every time I want to change from one library(database) to another. If I want to query a file(table) from library(database) "LibraryA", I use URL "jdbc:as400://www.system.com/LibraryA". If I want to query a file(table) from library(database) "LibraryB", I use URL "jdbc:as400://www.system.com/LibraryB". Even when I try to use a URL with a library list like "jdbc:as400://www.system.com/;libraries=LibraryA LibraryB", it only looks at the first library when trying to access a table in "LibraryB".
When I drag a table to the graph and select some fields, I would expect the sql to qualify the table with the library(database) name. After all, it knows which library the table is being dragged from. The generated sql looks like this:
SELECT
tableB.field1,tableB.field2
FROM tableB
What I would expect is for it to look something like this (iSeries sql syntax):
SELECT
tableB.field1,tableB.field2
FROM LibraryB/tableB
When I try to key over the generated SQL command, it still tries to access the table from
LibraryA.
If I use the URL ""jdbc:as400://www.system.com/", it will try to find a libary(database) named the same as my user ID.
When Squeryl Client can build the objects list showing the library and table I'm selecting, I should think it would be able to build a query to access the correct library as well.
What am I missing?
Thanks
Bob
I seem to have figured this out. I changed the URL to the following:
jdbc:as400://www.system.com/;naming=system; libraries=LibraryA LibraryB

What is spt_columns_odbc_view and why is it not accessible?

I did sp_helptext 'sys.sp_columns' and saw that the the underlying table is sys.spt_columns_odbc_view, but I could not execute queries as I get Invalid object name 'sys.spt_columns_odbc_view'.
Is this system table not queryable or am I lacking rights? Not much documentation is provided in msdn.
You should make a DAC connection first,
because sys.spt_columns_odbc_view is internal view.
After you make DAC, simply query it, like that:
select * from sys.spt_columns_odbc_view
More info on DAC

How do you find out which database links are used in queries for the Oracle RDBMS?

I would like to find out if any and which database links are being used, for a schema, and in which tables. Is it possible via the data dictionary somehow?
Is this possible with the Oracle RDBMS?
I know the answer by Dougman is accepted and accurate. But here is some more information.
If the user is not a DBA user, it will not have access to DBA_DB_LINKS. Also, USER_DB_LINKS will display the db links created by the current user, so that won't list all the DB links that the user has access to.
You can use ALL_DB_LINKS to get the links that the user has access to.
select * from all_db_links;
This will show you any database links set up on the database:
select * from dba_db_links;
You would then have to search for any queries or objects using the db link by doing a text search of them for the link syntax <tablename>#<dblink name>
You can first edit all sql code of a schema:
ex:
SELECT DBMS_METADATA.GET_DDL(object_type, object_name, owner)
FROM all_OBJECTS
WHERE (OWNER = 'your schema name');
and then search in the result the pattern of the db_link, which looks like #dblink_name.

Resources