how to pull the details together from SQL instance? - sql-server

I am looking to sort the details of number of DB's present in the SQL instance along with their Recovery model type and the size.?
ex: name , recovery_model_desc resides under sys.databases and size from sys.master_files. Also database_id is the shared column.
How to get the result in together?

JOIN the two tables together on the key field you have correctly identified:
SELECT db.*, mf.*
FROM sys.databases db
LEFT JOIN sys.master_files mf ON db.database_id = mf.database_id
I suspect that the LEFT JOIN could just be JOIN/INNER JOIN as I don't think there can be records in sys.databases without any corresponding records in sys.master_files, but I don't know for sure, so stuck with a LEFT JOIN for that reason.
If you need some basics around JOINs to get you started with understanding how this works, here are some resources:
MSDN Blog: Introduction to Joins
W3 Schools: SQL Joins
SQL Authority: Introduction to JOINs
Essential SQL: Introduction to Database Joins
I've also often found a visual explanation of SQL Joins to be a helpful reference at times.

Related

Find schema name for a given database?

How do I determine the schema name for a given database "MyDB", listing all tables contained in the database (in MS SQL Express)? Is it "MyDB.Security.Schemas/INFORMATION_SCHEMA"?
I am using EF Core with Blazor, code first.
Background: I want to determine whether a certain table is present in the database.
You can select all the tables with a given and known schema from one database with this query:
SELECT [schemas].[schema_id] AS SchemaId,
[schemas].[name] AS SchemaName,
[tables].[name] AS TableName
FROM sys.schemas AS [schemas]
INNER JOIN sys.tables AS [tables]
ON [schemas].[schema_id] = [tables].[schema_id]
WHERE [schemas].[name] = 'your-schema-name'
If you like to select over all databases on the server you can have a look at this SO answer:
How do I list all tables in all databases in SQL Server in a single result set?

Is there a way to identify if a column is not been used in a SQL Server Database?

I have a big database and a lot tables and I would like to identify what columns are not been called by any store procedure or any query, or not in use.
I'm not sure if this is what you're looking for, but in your DB under views there's a folder for System Views - three of which are the following. Looking in all_objects look for the table name, then use the object_id of that table to select from the other queries. There may be other meta-data here that is appropriate for your need.
SELECT *
FROM sys.all_objects
SELECT *
FROM sys.all_columns
WHERE object_id = 981578535
SELECT *
FROM sys.all_views
WHERE object_id = 981578535
After an investigation I have found a new feature of SQL Server called Query Store where you can find in disk the executions with other information. If you have a SQL Server 2016 instance you can find it in the properties of the data base. There you can change the amount of days to capture and then you can in theory try to find the columns in question. The idea of this technology is to give you more options for performance tuning.
You can find the information with this query :
SELECT TOP 10 qt.query_sql_text, q.query_id,
qt.query_text_id, p.plan_id, rs.last_execution_time
FROM sys.query_store_query_text AS qt
JOIN sys.query_store_query AS q
ON qt.query_text_id = q.query_text_id
JOIN sys.query_store_plan AS p
ON q.query_id = p.query_id
JOIN sys.query_store_runtime_stats AS rs
ON p.plan_id = rs.plan_id
where qt.query_sql_text LIKE '%ColumnToFind%'
ORDER BY rs.last_execution_time;
Credits: Query Store

What is the 'old style' syntax for joins in T-Sql?

I'm rewriting a bunch of old, badly written Oracle queries against a new(-er) Sql Server 2008 environment. They use old-school Oracle join syntax like
select <whatever>
from Table1, Table2, Table3
where Table1.T1ID = Table2.T2ID -- old Oracle inner join
and Table2.T3ID = Table3.T3ID (+) -- old Oracle left join (I think)
Except a lot more complicated. There's a lot of mixed joins and a lot of nesting and a lot of views piled on views going on in these things. It's not pretty. The data is disparate between the two servers too, making testing a chore.
I figured the easiest way to replicate would be to make the queries look as similar as possible in Sql Server (ie, using the same style of join), and then do a massive clean-up job after once I'm confident they're both definitely doing the same thing & I don't have a join in the wrong place somewhere (and yes, I have compatibility mode temporarily set to support old joins).
I know the 'old' syntax for an inner join in T-Sql is
select <whatever>
from T1, T2
where T1.ID = T2.ID
but what is the 'old' syntax for a left outer join or a right outer join?
From the documentation on TechNet (on SQL Server 2000, so be aware this might not be supported any more!), you need to use *= instead of the (+) as Oracle does:
select <whatever>
from T1, T2
where T1.ID *= T2.ID

Error in query using SQL Server 2008 R2

I am using SQL Server 2008 R2. I want to join tables but always get the error
The multi-part identifier could not be bound
I have 3 tables Drivers, Request and Journey. I have driver_id foreign key in Journey. How can I join these tables to get details of all three tables??
Select driver.driver_name
from Drivers,
Journey
where driver.id = journey.id
and driver.id=1;
This error usually occurs when an alias is used when referencing a column in a SELECT statement and the alias used is not defined anywhere in the FROM clause of the SELECT statement.
For more details visit here
You only seem to be joining two of your three tables - correct?
The proper, ANSI SQL-92 standard-compliant way to do this would be:
SELECT
driver.driver_name
FROM
Drivers
INNER JOIN
Journey ON driver.id = journey.id
WHERE
driver.id = 1;
This is using the ANSI standard (in place since 1992 - more than 20 years now!) JOIN syntax of INNER JOIN (there's also LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL JOIN and a few more) instead of just comma-separating a list of tables in the FROM clause.
See Aaron Bertrand's excellent blog post Bad habits to kick : using old-style JOINs on that topic, too.
driver+s
Select Drivers.driver_name
from Drivers,
Journey
where Drivers.id = journey.id
and Drivers.id=1;

How to get a list of all tables in two different databases

I'm trying to create a little SQL script (in SQL Server Management Studio) to get a list of all tables in two different databases. The goal is to find out which tables exist in both databases and which ones only exist in one of them.
I have found various scripts on SO to list all the tables of one database, but so far I wasn't able to get a list of tables of multiple databases.
So: is there a way to query SQL Server for all tables in a specific database, e.g. SELECT * FROM ... WHERE databaseName='first_db' so that I can join this with the result for another database?
SELECT * FROM database1.INFORMATION_SCHEMA.TABLES
UNION ALL
SELECT * FROM database2.INFORMATION_SCHEMA.TABLES
UPDATE
In order to compare the two lists, you can use FULL OUTER JOIN, which will show you the tables that are present in both databases as well as those that are only present in one of them:
SELECT *
FROM database1.INFORMATION_SCHEMA.TABLES db1
FULL JOIN database2.INFORMATION_SCHEMA.TABLES db2
ON db1.TABLE_NAME = db2.TABLE_NAME
ORDER BY COALESCE(db1.TABLE_NAME, db2.TABLE_NAME)
You can also add WHERE db1.TABLE_NAME IS NULL OR db2.TABLE_NAME IS NULL to see only the differences between the databases.
As far as I know, you can only query tables for the active database. But you could store them in a temporary table, and join the result:
use db1
insert #TableList select (...) from sys.tables
use db2
insert #TableList2 select (...) from sys.tables
select * from #TableList tl1 join Tablelist2 tl2 on ...
Just for completeness, this is the query I finally used (based on Andriy M's answer):
SELECT * FROM DB1.INFORMATION_SCHEMA.Tables db1
LEFT OUTER JOIN DB2.INFORMATION_SCHEMA.Tables db2
ON db1.TABLE_NAME = db2.TABLE_NAME
ORDER BY db1.TABLE_NAME
To find out which tables exist in db2, but not in db1, replace the LEFT OUTER JOIN with a RIGHT OUTER JOIN.

Resources