Need a query to find all the objects in a database? - sql-server-2005

I need a query to find all the objects tables,stored procedure,trigger,indexes,function in sql server?

USE MyDB
GO
select * from sys.objects
GO

Related

Get current table name in query

Is it possible to get table name in select statement?
Example:
SELECT Id, "Users" as TableName
FROM Users
I don't want to write "Users" manually but to determine it based on FROM statement.
I'm doing some migrations from one database to another (with different structure), and I have mapping table that stores old table names so that's why I need this (it's less error prone).
You can use following sub-query to get this information (works on SQL Server 2012+):
SELECT *,
(select object_name(ind.object_id)
from sys.fn_PhysLocCracker(%%physloc%%) plc
INNER JOIN SYS.DM_DB_DATABASE_PAGE_ALLOCATIONS(DB_ID(),null,null,null,null) ind
ON ind.allocated_page_file_id = plc.file_id
AND ind.allocated_page_page_id = plc.page_id) as table_name
FROM [your table]
It is painfully slow, caching on the side page allocation will speed things up.
Alternatively, if you want to do it for all tables, you can use following stored procedure:
USE [DB_NAME]
EXEC sp_msforeachtable 'SELECT Id, ''?'' AS [TableName] FROM ?'
Maybe it will be helpful for somebody :)

How can I find a table in SQL Server 2016

How can I find a table in SQL Server 2016?
I could use this to find the table in the current database:
SELECT *
FROM sys.Tables
WHERE name LIKE '%App_Current_Seq_Num%'
The problem is, I have several databases on the server.
Please try with the below sql query.
select * from [YourDatabaseName].INFORMATION_SCHEMA.TABLES where TABLE_NAME LIKE '%App_Current_Seq_Num%'
sp_tables , sys.tables , information_schema.tables will only give you the list of tables from current selected database. Use the below undocumented stored procedure to run this code for each database, with each resultset showing the list of tables belonging to it.
sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like ''%App_Current_Seq_Num%'''
Another option is to use dynamic SQL .
You can either do
use your_database_name
SELECT *
FROM sys.Tables
WHERE name LIKE '%App_Current_Seq_Num%'
or
SELECT *
FROM your_database_name.sys.Tables
WHERE name LIKE '%App_Current_Seq_Num%'
or if you want to search multiple databases at once use a union all clause.
Something like this:
SELECT * FROM database1.sys.Tables
UNION ALL
SELECT * FROM database2.sys.Tables
I used this and it worked perfectly:
USE Master
GO
EXEC sp_MSforeachdb
#command1='use ?; SELECT *
FROM sys.tables
WHERE name LIKE ''%App_Current_Seq_Num%'''
you can use SQL Search
it is a simple search control that you can use in sql server
to find tables/views/stored procedures and etc...
use fast free download from
https://www.red-gate.com/products/sql-development/sql-search/index

SQL Server - rename and merge databases with legacy support

I have two Sql Server 2008 R2 databases, called MyDbOne and MyDbTwo.
I'd like to merge them (all tables and procedures) into a new database called MyDb.
Assume there are no conflicts in table and procedure names between them.
The problem is: there are LOTS of code and procedures that execute queries using the database name, including procedures declared in one of the databases referecing tables from the other. There are queries like:
select * from MyDbOne..SomeTable;
select * from MyDbTwo..AnotherTable;
The tables SomeTable and AnotherTable would then exist in the MyDb database. But I need to support querying them using the leagcy names MyDbOne and MyDbTwo. If I would run the queries above, I'd like them to be effectively the same as:
select * from MyDb..SomeTable;
select * from MyDb..AnotherTable;
Is there some way to do it? Maybe create some sort of global aliases on the new database, resolving MyDbOne and MyDbTwo to MyDb? That would be perfect, but I don't know how to do it.
Thank you ver much!
There's no easy way around it. You'll have to update all those references manually. You can search the SQL server for all objects containing those database names by using one of these queries from this answer;
SELECT ROUTINE_NAME, ROUTINE_DEFINITION
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%Foo%'
AND ROUTINE_TYPE='PROCEDURE'
SELECT OBJECT_NAME(id)
FROM SYSCOMMENTS
WHERE [text] LIKE '%Foo%'
AND OBJECTPROPERTY(id, 'IsProcedure') = 1
GROUP BY OBJECT_NAME(id)
SELECT OBJECT_NAME(object_id)
FROM sys.sql_modules
WHERE OBJECTPROPERTY(object_id, 'IsProcedure') = 1
AND definition LIKE '%Foo%'
I don't think there is an issue solution to this. If I were you, I would keep existing MyDbOne and MyDbTwo databases intact and create a brand new one that references these two tables via views. Basically, you will create a Data Warehouse that seats on top of these existing databases. Good thing about this setup is, you don't have to rewrite any legacy code, which is huge.

Sql Query to find Temp tables in DB

Please suggest me a query to find temp database tables.
select name from tempdb.sys.tables
All temp tables created are stored in the tempdb available on sql server. So run this query in tempdb, you will find available temp tables on the server -
select * from sysobjects where "xtype" = 'u'
Hope this will help you

In SQL Server, why there are duplicated DMVs in all database? Such as sys.databases

In SQL Server, there are some DMVs that appear in all databases and have the same content in it. What's the purpose of this approach? For example, the following 2 query will give the same result.
select *
from master.sys.databases
select *
from tempdb.sys.databases
Thanks.
sys.databases only exists once (actually in the hidden resource database)
All you are doing above is switching db context temporarily by using a 3 part object name