Find which schema table belongs to in sql developer? - sql-server

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>%'

Related

How to view SQL table structure in Oracle SQL developer

I am connecting to Microsoft SQL Server using Oracle SQL Developer.
Describe table-name is not giving me any results. Can anyone please help me out with the right command to use to view SQL Server table structure in Oracle SQL developer?
Try this:
-- custom table information
select schema_name(t.schema_id)+'.'+t.name as TableName,
t.*
from sys.tables t
where t.name = 'MyTableName'
-- table columns information
select schema_name(t.schema_id)+'.'+t.name as TableName,
TYPE_NAME(t2.system_type_id) as DataType,
t2.*
from sys.tables t
inner join sys.columns t2 on t2.object_id = t.object_id
where t.name = 'MyTableName'
order by 1,
t2.column_id
Or this:
-- custom table information
exec sp_help 'MyTableName'
-- table columns information
exec sp_columns 'MyTableName'
DESC is an Oracle client command - works in SQLPlus, SQLcl, and SQL Developer - WHEN connected to an Oracle Database.
The best we have to offer you is, open the table from your browser, and see the Columns page.
Or like someone has offered, write query or use the provided SP that MSFT gives you.
Thank you guys for your answers. I have found one more method which is below
select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='XX_TABLE_NAME';
Thought of sharing as it might be helpful for others
Shift + F4
or Right click on tablename and click on popup Describe

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/

How can I find tables name of selected database in MsSql

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

How do you change the table_schema when the current table_schema is null?

I somehow managed to create a table in a database with a null table schema.
I can't query the table since it has no owner, and altering the table doesn't work for the same reason.
I would alter the table using:
ALTER SCHEMA null TRANSFER dbo.SubscriptionAnswerMR
But that doesn't work.
The information_schema.tables looks like this:
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE
q_Profiles NULL SubscriptionAnswerMR BASE TABLE
So my question is: How do I change q_Profiles' table_schema?
SQL Server 2000 (edit)
Microsoft SQL Server Management Studio 2008R2
You should be able to verify that your table is fine by seeing the result of the following query:
SELECT u.name
FROM q_Profiles..sysobjects AS o
INNER JOIN q_Profiles..sysusers AS u
ON o.uid = u.uid
WHERE o.name = 'SubscriptionAnswerMR';
This should be dbo unless someone explicitly created them with a different owner or used sp_changeobjectowner. Which you can use if you find that sysobjects also has the wrong answer:
EXEC sp_changeobjectowner 'SubscriptionAnswerMR', 'dbo';
ALTER SCHEMA is not valid here because it was introduced in SQL Server 2005. Though it would be useful for you to describe what "doesn't work" means.
INFORMATION_SCHEMA is a horribly unreliable set of views as #Pondlife points out. Also see the following, which doesn't help you much in SQL Server 2000, but should help going forward:
The case against INFORMATION_SCHEMA views
Also as a side note you seem to be confused about tables and database. TABLE_CATALOG is the database, not the table.
Did you note this comment in the documentation?
Do not use INFORMATION_SCHEMA views to determine the schema of an
object. The only reliable way to find the schema of a object is to
query the sys.objects catalog view or use the OBJECT_SCHEMA_NAME
function.

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