SQL Server Management Studio quickly locate column? - sql-server

I'm using SSMS and our tables have a lot of columns, so it gets frustrating trying to scroll and try to find a column. Is there a way to quickly locate a column?

You can select from the sys tables
select c.name
from sys.columns c
inner join sys.tables t on t.object_id = c.object_id
where t.name = 'YOURTABLENAME'
and c.name like '%column looking for%'

If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??

Related

Find the schema from database for a table in dbeaver

I am using DBeaver 22.0.1 for a client. I have access to test environment. The schema's in test and prod are little different. The client gave me the table name but the schema is different from prod. Is there a way to find the schema of that table using some shortcut.
FYI - I am working for the first time with DBeaver 22.0.1. My ask might sound little silly.
Assuming you are just trying to find the name of the schema for a given table name you can use this query. But please note that if you have the same table name in more than 1 schema you will get all the schemas where this table name exists.
select s.name
from sys.tables t
join sys.schemas s on s.schema_id = t.schema_id
where t.name = 'YourTableNameHere'

how to pull the details together from SQL instance?

I am looking to sort the details of number of DB's present in the SQL instance along with their Recovery model type and the size.?
ex: name , recovery_model_desc resides under sys.databases and size from sys.master_files. Also database_id is the shared column.
How to get the result in together?
JOIN the two tables together on the key field you have correctly identified:
SELECT db.*, mf.*
FROM sys.databases db
LEFT JOIN sys.master_files mf ON db.database_id = mf.database_id
I suspect that the LEFT JOIN could just be JOIN/INNER JOIN as I don't think there can be records in sys.databases without any corresponding records in sys.master_files, but I don't know for sure, so stuck with a LEFT JOIN for that reason.
If you need some basics around JOINs to get you started with understanding how this works, here are some resources:
MSDN Blog: Introduction to Joins
W3 Schools: SQL Joins
SQL Authority: Introduction to JOINs
Essential SQL: Introduction to Database Joins
I've also often found a visual explanation of SQL Joins to be a helpful reference at times.

Retrieve names columns of all tables in a SQL Server database

Does there exist a system stored procedure that lists the names of all tables in a SQL Server 2000 database and per table the names of all columns in that table? I want to export this data to a file for documentation.
I don't know if a system stored procedure exists, but I use this:
SELECT SysObjects.[Name] as TableName,
SysColumns.[Name] as ColumnName,
SysTypes.[Name] As DataType,
SysColumns.[Length] As Length
FROM
SysObjects INNER JOIN SysColumns
ON SysObjects.[Id] = SysColumns.[Id]
INNER JOIN SysTypes
ON SysTypes.[xtype] = SysColumns.[xtype]
WHERE SysObjects.[type] = 'U'
ORDER BY SysObjects.[Name]
In SQL-Server 2005+ you can do it using system views sys.columns and sys.tables
SELECT t.name TableName, c.name ColumnName
FROM sys.tables t
JOIN sys.columns c ON t.object_id=c.object_id
And you can also query the INFORMATION_SCHEMA.COLUMNS view.
It's also safer to use this view. Microsoft says this about the view "Querying the system tables directly may not provide accurate information if system tables are changed in future releases. These views provide an internal, system table-independent view of the SQL Server meta data. Information schema views allow applications to work properly even though significant changes have been made to the system tables."

Search for column in entire SQL database

I need to change a column's data type but I do not every location in the database that might be referencing it. Is there a script or application that can search all views, stored procedures, functions, table computed columns, etc. within a database and show what is referencing the column? I am using SQL Server 2005 Standard.
Thanks,
Aaron
You can always inspect the sys.columns catalog view:
SELECT
c.NAME 'Col Name',
OBJECT_NAME(c.OBJECT_ID) 'Table Name',
t.name
FROM
sys.columns c
INNER JOIN
sys.types t ON c.system_type_id = t.system_type_id
WHERE
c.Name = 'your-column-name-here'
and based on that information, you can generate the ALTER statements for a database:
SELECT
'ALTER TABLE dbo.' + OBJECT_NAME(c.OBJECT_ID) +
' ALTER COLUMN ' + c.NAME ' NewDataType NULL'
FROM
sys.columns c
WHERE
c.Name = 'your-column-name-here'
This query generates a set of ALTER TABLE .... statements which you can then copy to a SSMS query window and execute.
Word of warning: if any of the columns are being referenced - in a foreign key relationship, or if there's a default or check constraint on them - this approach might fail. In that case, you'd need to do some extra steps for those columns (like drop the constraints first etc.)
Update: this searches for the columns as defined in tables.
If you need to search into stored procedures, view and functions as well, I would strongly recommend using Red-Gate's excellent and free (!!) SQL Search utility - excellent stuff!
I like using a free search add-in tool from redgate software. I'm amazed at how useful it is - you can find all references to text quickly with it.
This description is from SQL Curry:
SQL Search finds fragments of SQL text within stored procedures, functions, views and more and once you find them, it quickly allows you to click and jump to the objects, wherever they happen to be on your servers. It’s pretty cool!
Here is the link: SQL Search
This query will help you find any table's column and the column it is referring to -
SELECT OBJECT_NAME(f.parent_object_id) AS [Table], COL_NAME(fc.parent_object_id,fc.parent_column_id) AS [Column],
OBJECT_NAME (f.referenced_object_id) AS RefTable, COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS RefColumn,
f.name AS FK
FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
WHERE OBJECT_NAME(f.parent_object_id) = '<your table name>'
AND COL_NAME(fc.parent_object_id,fc.parent_column_id) = '<your column name>'
I work for Red Gate and I see that SQL Search as already been mentioned. Glad that works for you. Another tool that can specifically list column dependencies is SQL Prompt 5, due to be released soon. You can download the EA build by visiting: http://www.surveymk.com/s.aspx?sm=zDJogAY5rwdIwOX/SqtTCQ%3d%3d and joining the early access list. I'd welcome you to try this out and let me know if it doesn't match your requirements. Another great feature it has is the ability to list your invalid objects. In other words, if you rename a column and you have a stored procedure that references the old column, it will draw your attention to this.

SQL Query to search schema of all tables

I am working on a SQL Server 2008 Db that has many tables in it (around 200). Many of these tables contain a field by the name "CreatedDate". I am trying to identify all the table schema with this particular field.
Is there a SQL query to do this?
I would query the information_schema - this has views that are much more readable than the underlying tables.
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%create%'
You can also try doing this using one of many third party tools that are available for this.
Queries are great for simple searches but if you need to do more manipulation with data, search for references and such this is where you can do a much better job with these.
Also, these come in very handy when some objects are encrypted and you need to search for
I’m using ApexSQL Search which is free but there are also many more (also free) on the market such as Red Gate or SSMS Tool Pack.
select object_name(c.object_id) as table_name
, schema_name(t.schema_id) as schema_name
from sys.columns c
join sys.tables t on c.object_id = t.object_id
where c.name=N'CreatedDate';
It gets a little more complicated if you want alsoother table properties, but you'll refer to the object catalog views like sys.tables, sys.columns etc.
My favorite...
SELECT objParent.name AS parent, obj.name, col.*
FROM sysobjects obj
LEFT JOIN syscolumns col
ON obj.id = col.id
LEFT JOIN sysobjects objParent
ON objParent.id = obj.parent_obj
WHERE col.name LIKE '%Comment%'
OR obj.name LIKE '%Comment%'
Above I'm searching for "Comment".
Drop the percent signs if you want a direct match.
This searches tables, fields and things like primary key names, constraints, views, etc.
And when you want to search in StoredProcs after monkeying with the tables (and need to make the procs match), use the following...
SELECT name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Comment%'
Hope that helps, I find these two queries to be extremely useful.
For me I only have read access to run querys so I need to use this function often here is what I use:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where TABLES.TABLE_NAME like '%your table name here%'
You can replace .TABLES with .COLUMNS then it would look like this:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE columns.COLUMN_NAME like '%your column name here%'
Use this query :
SELECT
t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name , *
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
Where
( c.name LIKE '%' + '<ColumnName>' + '%' )
AND
( t.type = 'U' ) -- Use This To Prevent Selecting System Tables
Same thing but in ANSI way
SELECT
*
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_NAME IN (
SELECT
TABLE_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME = 'CreateDate'
)
You do not need to type SQL Query for this in SQL Server 2008.
In SSMS Object Explorer choose Databases or Tables of the required database (if you need to search in one database), open menu View--> Object Explorer Details (alternatively press F7), type %CreatedDate% in Search textbox, ENTER, enjoy

Resources