How can I find tables name of selected database in MsSql - sql-server

I want get tables name of specific Database in MsSql without
using db-name
something like this in mysqlIi need
select table_name from information_schema.tables where table_schema=db-name
how I can do it?

Instead of Table_schema use Table_catalog and you can prefix the DB-name before information_schema to get the tables in that database
SELECT *
FROM [db-name].information_schema.tables
WHERE TABLE_CATALOG = 'db-name'

Use UR_DBNAME
SELECT * FROM information_schema.tables

I believe this is the shortest version of what your are looking for
select name from [db_name].sys.tables

Related

Is there any way or tool in SQL Server to find a column in the entire database based on the web page field name?

We have a web base tool where its database is SQL Server and we don't have an idea what could be the name of the column in database wrt field name on the web page.
We don't have access to the code. Is there any possible way to find the names in database column names?
You can use ;
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
view and you can look at the INFORMATION_SCHEMA.COLUMNS article about this view.
Following code can help in finding the column name along with the table name in the database:
use <replace with DB_Name>
GO
select * from information_schema.COLUMNS where COLUMN_NAME like '%<replace with column_name>%'
/*Also remove the angular brackets<> as well while replacing the column_name and DB_Name */
The INFORMATION_SCHEMA.COLUMNS view allows you to get information about all columns for all tables and views within a database :
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
To query for just one table you can use a query like this:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourTable'

How to find database name of any table

How to find the database name for a particular table when we have created a table and we forgot the database where we have created the same.
I have found the solution, we can simply find a database where we have created the table.
SELECT DISTINCT DB_NAME(database_id)
FROM [sys].[dm_db_index_operational_stats](NULL,NULL,NULL,NULL)
WHERE OBJECT_NAME(object_id,database_id) = 'tablename'
Try this EXEC sp_MSforeachdb
EXEC sp_MSforeachdb 'USE ? SELECT sc.TABLE_CATALOG, sc.TABLE_SCHEMA, sc.TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS sc WHERE TABLE_NAME=''YourTableName'''
if you are using SQL Server 2012 or more then simply you can find DataBase name using INFORMATION_SCHEMA like
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME ='your_table_name'
Here TABLE_CATALOG column shows DataBase Name.
There is no SQL Server ready solution to your problem.
To resolve it you need to loop through each database and to put the information schema into new table and then to query it.
Some idea can be found in the site bellow:
https://blog.sqlauthority.com/2008/04/29/sql-server-find-table-in-every-database-of-sql-server/

Find which schema table belongs to in sql developer?

I am using sql server connections in sql developer with the help of a plug-in...
Now my question is I have a list of tables which belong to sql server connections but I don't have the information on, which table belongs to which schema?
I have tried using the script
select owner, table_name
from all_tables
where table_name like 'xxxxxxxx%';
but it didn't work out, can any one please help out on this???
Thanks in advance!!!
Select schema_name (schema_id), name
from sys.tables
where name like 'your pattern'
#Ben Thul has an answer that is absolutely fine.
This is just an alternative using INFORMATION_SCHEMA (both of which essentially use sys.objects under the hood):
SELECT
t.TABLE_CATALOG
,t.TABLE_SCHEMA
,t.TABLE_NAME
,t.TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.TABLE_NAME LIKE '%<YOUTABLE>%'

Database issue for everyone (QUERY)

I want to create a query that generates the table name.
I tried something like this :
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='mytableName'
but it return an empty query. So , any ideas ? Thx
Why WHERE AND TABLE_SCHEMA='mytableName'
The AND is invalid at this point.
Besides the Tablename is in the column TABLE_NAME not TABLE_SCHEMA, maby you should try to filter using the schema name instead of the table name like:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='YOUR_SCHEMA_NAME'
Or if you need information regarding a specific table:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME='YOUR_TABLE_NAME'
To start with: that query is invalid (remove the AND).
2nd it's empty because there are no tables within the schema (also called database) named mytableName.
Also take a look at SHOW TABLES (documentation) and SHOW DATABASES (documentation)
There is incorrect syntax at "WHERE AND".
You should execute:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='mytableName'
You should try TABLE_SCHEMA='dbo', it looks like your schema 'mytableName' is really empty.

How to grep SQL Server stored procedures?

I would like to run a standard grep over all stored procedures in a given SQL Server database (assume 2005 or later). I have found a variety of simple queries to list the names of stored procedures containing a specific object, e.g.
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%table_I_want_to_find%'
But what I really want is, like grep, to list the specific lines in the identified stored procedures (so I do not have to manually open each one and see if it is what I am looking for).
I am open to solutions in T-SQL or PowerShell, or even an off-the-shelf utility.
Use SQL Search from Red Gate. It's a free tool and is fantastic.
I am brand new to SQL Server, but the following seems to work great and has proved itself to be quite useful already. I am using SQL Server 2008.
select ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE, ROUTINE_BODY, ROUTINE_DEFINITION
from INFORMATION_SCHEMA.ROUTINES
where ROUTINE_DEFINITION like '%searchtext%'
order by ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE, ROUTINE_DEFINITION;
For completeness, here are some similar queries you'll probably want to know about:
exec sp_help 'myTable'
select name
from sys.all_views
order by name;
select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE
from INFORMATION_SCHEMA.TABLES
order by TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE;
select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'myColumn'
order by TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME;
You could use SMO to dump the stored procedure DDL to files, and then use grep on them.
There are some examples of extracting the DDL here (called Scripting).

Resources