Extract the name of a Database which is used in a view (refering other Database) - sql

i dynamicaly create Views which refering other Databases (mainly to import data from there)
use [RezepteDB]
in this DB i create a view like this
CREATE VIEW [dbo].bla as select * from ZutatenDB.dbo.Bla
Later i need the name of the Database, on which is the view based.
For this example 'ZutatenDB'.
How can i done this? a small function to pass the name of the view and return the name of the database.

One technique is to use sys.dm_sql_referenced_entities:
SELECT *
FROM sys.dm_sql_referenced_entities ('dbo.bla', 'OBJECT');
You can see the referenced server, database, schema and objects.

Related

Where used list in BigQuery

I have a view, Wanted to check what sort of SQL can help to identify list of all views and Stored procedure using it.
For Example,
View A is being used in View B, View C and Stored Procedure D.
Is there any metadata schema which can help writing such query where we can track all dependent objects of a certain object in BigQuery. Please assist.
For Example,
View A is being used in View B, View C and Stored Procedure D.
Is there any metadata schema which can help writing such query where we can track all dependent objects of a certain object in BigQuery. Please assist.
You can run the following query that will list all views, this can be done on an individual dataset basis. Of course if you have a set of datasets you could create one UNION ALL query for each dataset to combine the results.
SELECT * FROM dataset_name.INFORMATION_SCHEMA.VIEWS WHERE
REGEXP_CONTAINS(view_definition, 'dataset_name.table_name')
Replacing dataset_name and table_name in the above query.
view_definition in the schema table contains the actual SQL code to create the view. So we are essentially just searching that field for the table name, same would work if you are looking for a view being used in a view.

Azure SQL: Is it possible to use the view from another database?

Being new to Azure SQL, I am already successfully JOINing my table with the table from the other database (by defining the CREATE EXTERNAL TABLE and the related things). However, some queries contain views from the other database, not the tables. How can I join my table with that external view? Is it possible at all? Or do I have to copy the code from the remote view and work directly with the external tables?
with create external table you can access a table or view :
take a look at
https://medium.com/#fbeltrao/access-another-database-in-azure-sql-1afc526b7ad4

SQL Server : creating a table

When creating a table in SQL Server, it is created using dbo.<tableName> format.
I want to change dbo.tableName to source.tableName, as I want to import data into a source table and then cook that data.
Thanks
You are talking about schemas. If the schema source doesn't exist yet, you need to run create schema source. Once the schema exists it's as easy as create table source.tableName (...).

vb.net query for creating table

I have Access as my back end. Student is the database name and its path is c:\Project\student.mdb
And am having few tables in it. When i click a command button in vb.net a new table must be created in the path mentioned. And it should have the fields I declare.
What s the query I need to use ? Is it possible to do like that ?
Yes, it's possible, you can check out the syntax for the create table command.
However, I have to say that creating tables dynamically suggests a bad database design. The database layout should normally remain the same whatever data you put in the database.
If you create tables dynamically, that means that you have data in the table names, but data should go inside the tables, not in table names (or even field names).
Yes it's possible . Just pass the create table command as query and the table name as variable , which you want to add dynamically. for reference of create table command
Create table command

Query driving a view and actual view not returning same results

I have a view that is returning four columns of data to be pushed to an external program. When I simply query the view ("Select * from schema.view_name") I get 10353 rows. When I run the actual SQL that created the view (I literally copied and pasted what Oracle had stored, minus the "Create or Replace" statement), I get 238745 rows.
Any ideas why this might occur?
Best guess: when you run the query standalone you're not running it in the same schema the view was created in (I am inferring this from the fact that you included the schema name in your example SELECT). The schema where you're running the query either has its own table with the same name as one of the base tables in the view, or one of the names is a synonym pointing to yet another view that contains only a subset of the rows in the underlying table.