Is it possible to find all the varchar() columns in my database?
I am using SQL Server 2008 and would like to get the list in SQL server management console.
JD.
Yep, this should work:
select * from INFORMATION_SCHEMA.COLUMNS
where DATA_TYPE = 'varchar'
Try, this will give varchar and nvarchar, if the character_maximum_length column returns -1 then it is varchar(max) or nvarchar(max)
select * from
INFORMATION_SCHEMA.COLUMNS
where DATA_TYPE in('varchar','nvarchar')
Another method not limited to Views or User tables that utilizes sys objects instead of information_schema.
Multiple questions previously answered regarding usage of sys vs information_schema.
SELECT OBJECT_NAME(c.OBJECT_ID) TableName, c.name ColumnName
FROM sys.columns AS c
JOIN sys.types AS t ON c.user_type_id=t.user_type_id
WHERE t.name = 'varchar' --you can change text to other datatypes
ORDER BY c.OBJECT_ID;
GO
Original source utilized for Sql Server 2005, just executed on Sql Server 2016.
Reference : Pinal Dave (https://blog.sqlauthority.com)
https://blog.sqlauthority.com/2007/08/09/sql-server-2005-list-all-the-column-with-specific-data-types/
Related
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
This question already has answers here:
Get list of all tables in Oracle?
(24 answers)
Closed 3 years ago.
I need to retrieve Table Names from Linked Oracle Database which is on Linked Server with Microsoft SQL Server.
e.g. I can get table names in SQL Server using:
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%TELEPHONE%'
but, I need Oracle Code.
I am using Microsoft SQL Server with a linked server to Oracle.
SELECT
*
FROM
OPENQUERY(ORACLE_DB_NAME,'
SELECT
table_name,
column_name
FROM
cols
WHERE
column_name LIKE ''%TELEPHONE%'''
)
Based on your comment "Hi, SQL Query to retrieve Oracle Table Names", you can do the following:
SELECT
table_name,
column_name
FROM
cols
WHERE
column_name LIKE '%TELEPHONE%';
You can use USER_TAB_COLS or ALL_TAB_COLS view for it. Refer oracle documentation for more information on this views
SELECT TABLE_NAME, COLUMN_NAME
FROM USER_TAB_COLS
WHERE UPPER(COLUMN_NAME) LIKE '%TELEPHONE%';
UPPER is used in WHERE clause as names are case sensitive if they are created with double quotes and case insesitive if created with no quotes.
Cheers!!
I am using SQL Server 2014 Management Studio. I tried this query:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%SomeColumn%'
I get this after executing above query:
However, I know that the column I specified in the WHERE is a valid column in one of my tables in one of my databases.
I just thought there would be maybe a table where each row has information on a column (including which table it belongs to and which database); so I could query for column 'SomeColumn' and then see which table(s) and database(s) it is in.
You can use sp_MSforeachdb
DECLARE #sql VARCHAR(MAX)
SET #sql =
'USE ?;
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG LIKE ''%%''
AND COLUMN_NAME LIKE ''%SomeColumn%'''
EXEC sys.sp_MSforeachdb #sql
This may work for you:
EXEC sp_MSforeachdb 'USE ? SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE ''%SomeColumn%'''
The sp_msforeachdb sp will run your statement on all databases on your server.
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).
I exported a table to a server but I can't find the table. Maybe I didn't put the right destination database. How can I find this table if my server has multiple databases, without opening each one of them?
I use MS Sql Server Management Studio 2008.
Rough and dirty, but it would do the job.
-- Instructions. Replace "table_name_here" with actual table name
sp_MSforeachdb 'USE ?
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N''[table_name_here]'') AND OBJECTPROPERTY(id, N''IsUserTable'') = 1)
BEGIN
PRINT ''Found in db ?''
END'
One way
SELECT DISTINCT DB_NAME(database_id)
FROM [sys].[dm_db_index_operational_stats](NULL,NULL,NULL,NULL)
WHERE OBJECT_NAME(object_id,database_id) = 'table_name'
Or if you are reasonably confident it would be in the dbo schema in whichever database
SELECT name
FROM sys.databases
WHERE CASE
WHEN state_desc = 'ONLINE'
THEN OBJECT_ID(QUOTENAME(name) + '.[dbo].[table_name]', 'U')
END IS NOT NULL
Based off Martin Smith's answer above but generalised into a view to give a sort of cross-DB version of sys.tables -
CREATE VIEW ListTablesAllDBs
AS
SELECT
DB_NAME(database_id) as DBName,
OBJECT_SCHEMA_NAME(object_id,database_id) as SchemaName,
OBJECT_NAME(object_id,database_id) as TableName
FROM
[sys].[dm_db_index_operational_stats](NULL,NULL,NULL,NULL)
Now, if only I can work out a way to do the same for columns.......
EDIT - Ignore this, finding it sometimes misses tables altogether.
Minor clarification just to avoid headaches for those with 'SuperUsers' who don't know how to name DBs:
EXEC sp_MSForEachDB '
USE [?]
IF OBJECT_ID(''mytable'') IS NOT NULL AND
OBJECTPROPERTY(OBJECT_ID(''mytable''), ''IsTable'') = 1
PRINT ''Found here: ?'''
select 'select * from '+name+'.sys.tables where name=
''[yourtable]'';' from sys.databases
Instead of [yourtable], type the name of the missing table, and run the result again.
EXEC sp_MSForEachDB '
USE ?
IF OBJECT_ID(''mytable'') IS NOT NULL AND
OBJECTPROPERTY(OBJECT_ID(''mytable''), ''IsTable'') = 1
PRINT ''?''
'