Sql Query to find Temp tables in DB - sql

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

Related

SQL query to return the table structure

Knowing only the name of a table, is there a way in SQL to query the structure of the table so I can inspect the columns?
This works in MS SQL server.This will show you column datatype and size.
select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='<Table Name>'

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 :)

SQL Select list of tables in a database

I am using SQL Server 2005. I am trying to SELECT a list of tables in one of my database. Here is my structure of my SQL Server:
- <IP>(SQL Server 9.0 -userlogin)
- Databases
- Company
- Tables
- dbo.UserDB
- dbo.detailsDB
- dbo.goodsDB
I would like to retrieve the values of dbo.UserDB, dbo.detailsDB, dbo.goodsDB
But i do not know what is the exact sql query needed.
I have tried many ways like
SELECT * FROM userlogin.Tables; and
SELECT * FROM userlogin.Company.Tables;, but none of them works.
I have seen quite a few posts which suggests using show databases and show tables, but they don't seem to work as well.
Is it possible to select a list of table names in a database in the first place?
Thank you for any help in advance.
Thanks for the MSDNA link that #TomTom provided, I can now list my tables in my database.
However, I would want to list specific tables where TABLE_NAME contains "user".
How can I do it? I am trying on the following sql but it is not displaying result at the moment:
SELECT DISTINCT TABLE_NAME
FROM Company.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%"user"%';
GO
Try the iNFORMATION_SCHEMA.
http://msdn.microsoft.com/en-us/library/ms186778.aspx
They contain all the schema information you need.
Use the new sys system catalog in SQL Server 2005 and up:
SELECT Name
FROM sys.tables
WHERE is_ms_shipped = 0 -- only list *your* tables - not the system / MS table
You can read more about system catalog views and how to query them in the MSDN Books Online - and be warned - there's lots more to read and learn!
You could use this
Use ***database name***
SELECT *
FROM sys.tables
WHERE name like '%user%'
Sorry, have seen that #marc_s has provided the answer before me!
You can use INFORMATION_SCHEMA as told by #TomTom:
USE <<YOUR_DB_NAME>>
SELECT TABLE_SCHEMA + '.' + TABLE_NAME, *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_SCHEMA + '.' + TABLE_NAME
You can customize above query as per your requirements.
For your other question to find tables where the name contains 'user', execute the following statement:
USE <<YOUR_DB_NAME>>
Exec sp_tables '%user%'
Try:
SELECT *
from sysobjects
where type = 'U' AND
NAME LIKE '%user%'
GO

Need a query to find all the objects in a database?

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

How to check a SQL Server CE database for indexes?

Is there a way to list all SQL Server CE database table indexes, or at least for individual tables?
-- Retrieves information about the indexes contained in the database.
SELECT * FROM
INFORMATION_SCHEMA.INDEXES
-- Retrieves all the Tables in the database including the System tables.
SELECT * FROM
INFORMATION_SCHEMA.TABLES
Arjuna Chiththananda - Retrieving Schema Information of SQL CE Database
Thank you Arjuna! you pointed me in the right direction..... The following works.
SELECT 'Y' FROM INFORMATION_SCHEMA.INDEXES
where table_name = 'PP_JOB_TICKET_LIVE'
and index_name = 'PP_JOB_TICKET_LIVE_PK'
Thanks so much for your time.