Ways of seeing relationships among views of a database? - sql

I need to extract data from views in a database in Microsoft SQLServer Management Studio. There are about 100 views in the database and I'm not familiar with most of the views. Is there any easier way to check the relationship among the views instead of click into each view and guessing their relationships?
I can only find the solution of seeing relationship of table but no views through Google search.

For MS SQL try:
select 'insert into ViewDepend exec sp_depends ''' + name + '''' from sys.views
You'll need to define an appropriate table for ViewDepend. It will have the same columns that sp_depends generates.

Related

Azure / Transact SQL: Dynamically create View of tables with the same name OR add tables after View creation

I'm new to Azure and not great with SQL so any help would be greatly appreciated.
I have a Database where each user has a Schema. Each Schema has identically structured tables with the same name, say "Table".
I now require a View in another Schema which provides a read-only union of all the rows from all the tables Table.
I was successful in creating a Schema, say Views, handling its permissions and creating a View, "TableView", with the following SQL from Partitioned Views # learn.microsoft.com:
CREATE VIEW Views.TableView
AS
SELECT *
FROM Schema1.Table
UNION ALL
SELECT *
FROM Schema2.Table
UNION ALL
SELECT *
FROM Schema3.Table
...
GO
I now wish for this View to be dynamic as future Schemas (SchemaX) are added or even possibly removed without having to repeatedly DROP and CREATE TableView over and over.
Is it possible to create the View in such a way that it would automatically query all tables with the same name? Or perhaps there is some way to 'add' an additional table post creation?
I can get a list of all SchemaX.Table by querying INFORMATION_SCHEMA, but other than having a python script DROP and CREATE the View again I am lost.
Thanks
Thanks for #larnu's comments, it's very useful and professional:
To achieve this dynamically, it would be impossible to do in a VIEW.
You would have to use a stored procedure, and that means you can't do
simple things like SELECT from it, making it far harder to use.
Instead of having 17 tables, all identical, on different schemas you
have one table, with a column BusinessName. Instead of
MySmartCompany.Mytable you have a column in the table dbo.MyTable (or
your generic schema), called BusinessName which has the value 'MySmartCompany'.
This also can be beneficial to other community members.

How to view a normal table in SQL

SQL using sp_HelpText to view a stored procedure is used to view a stored procedure but what if i want to view my table,
is there any query to view my table? (I am newb to Sql and C#)
You can use sp_help "table_name" or select table & press Alt+F1 to view details of table..To View script, you need to script that using SQL server Management Studio.
Expand Database > Tables > Right Click on the table > Click on Create Script.
Thanks
I would suggest using the native INFORMATION_SCHEMA views to get table information. The following query contains a complete list of columns, data types etc for a given table without needing to join several sys views.
SELECT
*
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
[TABLE_NAME] = 'YourTable'
The table details can be viewed by pressing Alt+F1 after highlighting the table name.

How to alter a column from char(2) to varchar(5) on 1000 Tables in 100 Databases on 5 servers?

I need to come up with a script to alter a column from char(2) to varchar(5) on 1000 Tables in 100 Databases on 5 servers. The column will most probably have 'Office' in the name. However:
The column may have different name, not necessarily containing 'Office'.
Sometimes a column is indexed, sometimes not.
Sometimes a column is a Primary Key, sometimes not.
Sometimes there are computed columns, sometimes not.
Often there are many Views dependent on the above tables.
What would be the best methodology?
I started with Red Gate's Compare and Dependency Tracker, etc. but there are many independent tables where a column needs to be altered.
If you provide some more info I may be able to give a more specific answer. Maybe you could give a couple of examples of the different scenarios.
The generic answer is you need to find the different scenarios and work out how you'd do it manually. e.g. If it's just changing a column with no dependencies then you the ALTER TABLE will give you the right result. If you're not comfortable with building the SQL you can use the table designer in Management Studio and rather than saving the changes there is an option to generate the SQL.
Once you've got the sample SQL for each scenario then use the INFORMATION_SCHEMA tables to build up some SQL dynamically to find each scenario and build the correct ALTER TABLE as one of the columns. e.g.
select 'ALTER TABLE [' + table_schema + '].[' + table_name + '] ALTER COLUMN [' + column_name + '] varchar(5);' FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like '%office%'
Because you're going across multiple databases and servers you may be able to use the sys.databases or sp_msforeachdb to apply to each database. To go across multiple servers you may be able to use linked servers or because there's only 5 you could just apply the same process 5 times.
hope that helps
SQL Prompt 5 will have a feature to identify column dependencies. It's currently in early access so if you sign up you can try out the feature and provide us with feedback.
For more details: http://www.red-gate.com/messageboard/viewtopic.php?t=11846
To sign up: http://www.surveymk.com/s/DZLN2JW
In the meantime you can always use the free SQL Search tool to find references to an object in your SQL Server schema: http://www.red-gate.com/products/SQL_Search/
Hope this helps!

run sql queries under a particular schema context

We are thinking about to create new schema with its own 3 tables which will be created on the fly for an individual customer.
To run a particular query for those tables in a procedure, should we have something like this.
declare #sName nvarchar(200);
select #sName =Schema_Name from schema where Schema_Id = passed_id_from_code
ALTER USER UserName WITH DEFAULT_SCHEMA = #sName
-- Run the statements here --
...
-- After finishing executing statements
ALTER USER UserName WITH DEFAULT_SCHEMA = db;
In this scenario, can concurrent customers from various schema can update their own schema table or it will conflict.
Your suggestions are welcome.
Anil
Most SQL databases have each table create as an unique entity in that database. That means that each table can be modified and altered individually with no relation to the other tables. CUSTOMERA.TABLE_ONE is a different object in the database that CUSTOMERB.TABLE_ONE. They do share the same name, but they are not the same object with potentially different layouts (as they have different schemas).
So unless there is some restriction on the RDBMS you can do this. Now having different schemas for each user may not be good. If you are develop the same app to handle several customers, you have to make sure it will work with all schemas and all custoemrs. In potentially different versions of the schema.
If you are going for a multi-tenant architecture, it may be wiser to use some kind of extension to to table. So you have a single DB.TABLE_ONE, with a CUSTOMER_DATA column where you put data in a know and flexible format (say JSON or XML). Some RDBMS have that that as a native features (I believe DB2 is one of them).
Hope this helps.

Where Used Statements in SQL

I have data used primarily in one table in a database and referenced in several different tables. I need to correct the data. Is it possible to write a "where-used" statement that will search every table in the database and return all tables where the data is referenced?
I am using SQL 2005.
Thanks.
I've found this sql statement here:
SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc LIKE '%CONSTRAINT'
AND OBJECT_NAME(OBJECT_ID) LIKE 'FK_%'
NOTE:- I name all foreign key constraints starting with FK_ so it is easy to filter them.
Try adding a diagram to the database, and drop all of the tables onto it. If I've interpreted your question correctly you're trying to understand a database schema that already exists? IF you use the diagram it will draw on the references for you which will allow you to see where the data is linked in your table structure.
As for SQL you can use joins, or where conditions to link data from different tables.
What are you trying to "correct"?
In Management Studio you can right click on a table/view/stored procedure and select View Depenencies. In the dependencies window you can choose to view objects that the selected item depends on or view the items which depend on the selected item.
You can't do it after the fact, but at the table design time you can set up relationships to "ON UPDATE CASCADE".
You could reverse engineer the database (see this posting for a script that does this) if it has foreign keys physically present in the database. If this is not the case then you are up for some manual detective work.
Visio professional has a tool for reverse engineering a database. 2003 doesn't play nicely with SQL Server 2005, so you will need either:
Visio 2007
Modify the script linked to above so it doesn't generate schema references and load the script into a SQL Server 2000 database. Then, reverse engineer from the SQL 2000 DB.
If you don't have foreign keys or have incomplete foreign key coverage, you can look for them manually (for example, look for fields with similar looking codes to reference data) and manually annotate the keys in the diagram. Don't try to do this with the diagramming tool that comes in SSMS as it can attempt to populate the FKs back into the database.