Get SQL Server database created Information - sql

I have created a database in SQL Server 2012 with name "yadav". Now I want to know where "Yadav" DB info is stored like Created Date, Number of Tables, User Name etc.
Thanks in advance.

Some basic things are accessible in the sys.databases catalog view:
SELECT name, database_id, create_date
FROM sys.databases
WHERE name = 'yadav'
For other, you'll have to go to your database:
USE yadav;
SELECT TableCount = COUNT(*)
FROM sys.tables
and there's a vast collection of other system catalog views which provide insight into your database and its objects

You need something like this
sp_helpdb 'Yadav'

You can get the database information from sys.databases table.
select *
from sys.databases
where name = database_name
and table information from INFORMATION_SCHEMA.COLUMNS table.
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_CATALOG = database_name

Related

How to find the database name using the table name?

I found the table name exist in my system, I used the following query;
select * from sys.tables where name = '%openpotab%';
But how can I find the database name using the table name?
thanks in advance,
Bhavesh
You could use:
select DB_NAME(DB_ID()), * from sys.tables where name LIKE '%openpotab%';

List all table names without knowing the database name within my machine

I am using SQl Server 2014, and Normally we can list out tables if we knew the DB name as :
USE YOURDBNAME
GO
SELECT *
FROM sys.Tables
GO
But I want to know all the tables irrespective of db's that are present on my machine
Or can I go for looping by listing out all DB name.(EXEC sp_databases)
Is there any better way to find out this?
There are several ways of getting all tables in your database
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
OR
SELECT * FROM Sys.Tables
OR
SELECT * FROM INFORMATION_SCHEMA.TABLES
OR
SELECT sobjects.name FROM sysobjects sobjects WHERE sobjects.xtype = 'U'
Docs for all the xtypes
select * from tab
or
select * from * cat
The first one will show the associated CLUSTERID with tables for the current user.
I think you need
SELECT name FROM master.sys.databases
and then you will need to create a cursor query (for your USE XXXX) to iterate through the following query.
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

how to get Last updated Record in sql server

I use SQL Server 2008 R2.
I have a table with 5 records. The table does not have any column that keeps track of LastUpdated or something like that.
Is it possible to find out when was the last date/time that someone added/changed a record? Which record was that?
Thanks.
In General Practice you should add column to do this ,But any ways you can see the last time the table was Modified
if you want to check the Structure Modiifcation
USE
SELECT * FROM SYS.Tables where Name Like '[TableName]'
Here Column Modify_Date will give you last time table was modified
if you want to check the Data Modiifcation
USE
SELECT OBJECT_NAME(OBJECT_ID) AS TableName, last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( '[YourDatabaseName]')
If you want to know the latest modification datetime on that table then use
Select modify_date from sys.tables where name = 'tablename'
If you want to know the particular record then there is no way you have to use :
Alter table tablename add modifieddate datetime
select t.name, user_seeks, user_scans, user_lookups, user_updates, last_user_seek, last_user_scan, last_user_lookup, last_user_update
from sys.dm_db_index_usage_stats i JOIN sys.tables t ON (t.object_id = i.object_id)
where database_id = db_id()

Search SQL Server metadata

I need to search SQL Server's metadata for all stored procedures that were changed between specific dates.
So far I have following query that looking for specific value in the SP.
SELECT DISTINCT sysobjects.name AS [Object Name]
FROM sysobjects,syscomments
WHERE sysobjects.id = syscomments.id
and syscomments.text like '%Stock%'
In the example above I am looking for all SP that have word Stock in it.
But how can I search for all SP's that were changed between specific dates?
USE AdventureWorks;
GO
SELECT name, create_date, modify_date
FROM sys.objects
WHERE type = 'P'
AND name = 'uspUpdateEmployeeHireInfo'
GO
source: http://blog.sqlauthority.com/2007/08/10/sql-server-2005-find-stored-procedure-create-date-and-modified-date/

Using SQL query to determine if a table exists

Guys is there any other way to determine a table exists other than below
select count(*) from <table> where rownum =1
select * from user_table where table_name=<table>
kindly let me know the best way to check whether a table exists using oracle sql.
Thanks for the answer , my requirement is to check from the first date of current month ie 01/12/2010 with table name in the format suresh_20101201 exists in the database, if not then it should check for table suresh_20101202 and thereon till suresh_20101231 . is it possible to do in oracle sql query.
You can do this (in oracle, in mssql there is a bit different):
select count(*)
from all_objects
where object_type in ('TABLE','VIEW')
and object_name = 'your_table_name';
In most sql servers there is a system domain where you can query for a table's existence. It's highly implementation specific though. For example, in recent versions of MySql:
SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name'
AND table_name LIKE 'whatever'
You need to ask your server's system catalog. Not sure what database you meant but for SQL Server it would be:
select * from sys.tables where name='your-table-name-'
Used this in Oracle SQL Developer:
SELECT COUNT(*) FROM DUAL WHERE EXISTS (
SELECT * FROM ALL_OBJECTS WHERE OBJECT_TYPE = 'TABLE' AND OWNER = 'myschema' AND OBJECT_NAME = 'your_table_name')
This will return either a 0 or 1 if your table exists or not in the ALL_OBJECTS records.
Below query can be triggered to Oracle for checking whether any Table present in DB or not:
SELECT count(*) count FROM dba_tables where table_name = 'TABLE_NAME'
Above query will return count 1 if table 'TABLE_NAME' is present in Database
Look in the schema, might event be able to use sys.objects and check for a type at the same time.....
Something like