SSMS Searching for a Column - sql-server

How do I look for a certain column in a Database with 100+ tables?
For instance, in the frontend, I see a tab with displayed data..
A information with columns = email's, mailbox, note1, sent dt/tm, note2 etc.. but in the DB there are about 100+ Tables (messy),
I just want to find the table that includes the columns of the A information with out expanding the columns of each table to investigate on where columns = email's, mailbox, note1, sent dt/tm, note2 etc.. Is there an easy way to do this on SQL Server Management studio?

sp_msforeachdb 'USE ?; Select *
From INFORMATION_SCHEMA.COLUMNS
Where Column_Name like ''%Col%'''

Try information schema:
Select *
From INFORMATION_SCHEMA.COLUMNS
Where Column_Name like '%Col%'

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'

List of available keywords for SQL Server

It seems I can't use SHOW command in SQL Server like SHOW COLUMNS FROM... or SHOW TABLES.
Is there somewhere a list of all the commands that are available so I know what I can use ?
To get Table and Column information
Select * From INFORMATION_SCHEMA.COLUMNS
Select * From INFORMATION_SCHEMA.Tables
Reserved Words
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql
Stripped and modified from a validation routine
Declare #Reserved table (Word varchar(100))
Insert Into #Reserved values
('ADD'),('ALL'),('ALTER'),('AND'),('ANY'),('AS'),('ASC'),('AUTHORIZATION'),('BACKUP'),('BEGIN'),('BETWEEN'),('BREAK'),('BROWSE'),('BULK'),('BY'),
('CASCADE'),('CASE'),('CHECK'),('CHECKPOINT'),('CLOSE'),('CLUSTERED'),('COALESCE'),('COLLATE'),('COLUMN'),('COMMIT'),('COMPUTE'),('CONSTRAINT'),
('CONTAINS'),('CONTAINSTABLE'),('CONTINUE'),('CONVERT'),('CREATE'),('CROSS'),('CURRENT'),('CURRENT_DATE'),('CURRENT_TIME'),('CURRENT_TIMESTAMP'),
('CURRENT_USER'),('CURSOR'),('DATABASE'),('DBCC'),('DEALLOCATE'),('DECLARE'),('DEFAULT'),('DELETE'),('DENY'),('DESC'),('DISK'),('DISTINCT'),
('DISTRIBUTED'),('DOUBLE'),('DROP'),('DUMP'),('ELSE'),('END'),('ERRLVL'),('ESCAPE'),('EXCEPT'),('EXEC'),('EXECUTE'),('EXISTS'),('EXIT'),('EXTERNAL'),
('FETCH'),('FILE'),('FILLFACTOR'),('FOR'),('FOREIGN'),('FREETEXT'),('FREETEXTTABLE'),('FROM'),('FULL'),('FUNCTION'),('GOTO'),('GRANT'),('GROUP'),
('HAVING'),('HOLDLOCK'),('IDENTITY'),('IDENTITY_INSERT'),('IDENTITYCOL'),('IF'),('IN'),('INDEX'),('INNER'),('INSERT'),('INTERSECT'),('INTO'),('IS'),
('JOIN'),('KEY'),('KILL'),('LEFT'),('LIKE'),('LINENO'),('LOAD'),('MERGE'),('NATIONAL'),('NOCHECK'),('NONCLUSTERED'),('NOT'),('NULL'),('NULLIF'),
('OF'),('OFF'),('OFFSETS'),('ON'),('OPEN'),('OPENDATASOURCE'),('OPENQUERY'),('OPENROWSET'),('OPENXML'),('OPTION'),('OR'),('ORDER'),('OUTER'),('OVER'),
('PERCENT'),('PIVOT'),('PLAN'),('PRECISION'),('PRIMARY'),('PRINT'),('PROC'),('PROCEDURE'),('PUBLIC'),('RAISERROR'),('READ'),('READTEXT'),('RECONFIGURE'),
('REFERENCES'),('REPLICATION'),('RESTORE'),('RESTRICT'),('RETURN'),('REVERT'),('REVOKE'),('RIGHT'),('ROLLBACK'),('ROWCOUNT'),('ROWGUIDCOL'),('RULE'),
('SAVE'),('SCHEMA'),('SECURITYAUDIT'),('SELECT'),('SEMANTICKEYPHRASETABLE'),('SEMANTICSIMILARITYDETAILSTABLE'),('SEMANTICSIMILARITYTABLE'),('SESSION_USER'),
('SET'),('SETUSER'),('SHUTDOWN'),('SOME'),('STATISTICS'),('SYSTEM_USER'),('TABLE'),('TABLESAMPLE'),('TEXTSIZE'),('THEN'),('TO'),('TOP'),('TRAN'),('TRANSACTION'),
('TRIGGER'),('TRUNCATE'),('TRY_CONVERT'),('TSEQUAL'),('UNION'),('UNIQUE'),('UNPIVOT'),('UPDATE'),('UPDATETEXT'),('USE'),('USER'),('VALUES'),('VARYING'),
('VIEW'),('WAITFOR'),('WHEN'),('WHERE'),('WHILE'),('WITH'),('WITHIN GROUP'),('WRITETEXT')
Select A.*
From INFORMATION_SCHEMA.COLUMNS A
Join #Reserved
on Column_Name = Word
The ISO standard is to query the INFORMATION_SCHEMA views for meta-data information. A more concise way is to list table/view columns in SQL Server is sp_help:
EXEC sp_help 'your-object-name-here';
In SSMS, you can highlight the desired object name in a query window and press ALT+F1 to execute sp_help for that object. With SQL Operations Studio, ALT-F12 will peek the object definition.
SELECT Distinct TABLE_NAME FROM information_schema.TABLES
Hope this helps and is similar for columns. Plus, I haven't seen any such list till now but surely will add in future once I see it.

Failing to understand how to select all rows based on table- and database-name

WHAT I EXPECT:
I want to create a Job in my SQL Server Agent that allows me to fire off a stored procedure to clean up a particular table. The spu would take two parameters: TableName and Days.
TableName would be the name of the table I'm looking for and Days would be how far back I wish to delete records.
WHAT I'VE DONE:
After having looked around online I've found sources on how to see if a User Database holds the supplied TableName:
SELECT *
FROM INFORMATION_SCHEMA.Tables
WHERE TABLE_NAME = #TableName
This results in a few rows looking a bit like this:
TABLE_CATALOG | TABLE_SCHEMA |TABLE_NAME |TABLE_TYPE
Database_A | table_schema |table_A |table_type
WHAT I DON'T UNDERSTAND:
How can I use the resulting rows of the previous query to find all rows of the supplied #TableName in a particular Database? In pseudo:
SELECT * FROM table_A WHERE database = database_A
I know I need to use a cursor somehow, that's not the problem.
What I'm simply struggling to understand is how I can use the database name and the table name to find the rows of the table in a particular database.
In my case I've got 10 or so databases that need to be iterated through to find the initial dataset (all user databases where #TableName exists) and then a secondary query to find all rows of the #TableName in the database that the cursor currently is pointing at.
You have to do select * from ..table_A
but you can't do that in a a simple TSQL. Possibly you could generate a sub script and execute.

SQL Server : gather data from different databases

I have many different application databases with a [Log] table. I have one central database with a similar log table, but with one extra column called TenantId. There is also a Tenant table with a TenantId and a DatabaseName column. These DatabaseName contain the names of the application databases.
Now I want to loop all the application databases and copy the log entries to the central log table, with the TenantId that belongs to the application database name.
Would it be possible to write one procedure in the central database instead of creating many procedures in the application databases? All databases are on the same SQL Server instance.
Just some quick Dynamic SQL. In the example below, CHINRUS is my central database and would therefore be excluded from consolidation.
I should add, that the WHERE should be tailored to exclude any misc database on the server. Yet another option would be to maintain a table which has the proper definitions.
Declare #LogTable varchar(100)='[Chinrus].[dbo].[TransactionLog]'
Declare #CentralDB varchar(100)='Chinrus'
Declare #SQL varchar(max) = ''
Select #SQL = #SQL + SQL
From (
Select Name,SQL=';Insert Into '+#LogTable+' Select *,TenantId='''+Name+''' From ['+Name+'].[dbo].[TransactionLog] '
From master.dbo.sysdatabases
Where Name<>#CentralDB
) A
Select #SQL
--Exec(#SQL)
You can get list of all databases with following query:
SELECT name
FROM master.dbo.sysdatabases
and then you can use a cursor to get each database data and insert in one table in current database.

getting schema + first 100 records from every table in a db

i have a large sql server db, and i want to get the schema (all tables/triggers/sprocs), i'm pretty sure that's easy.
but the tough part is that i want to get 100 records from each table. it's a huge db on a remote server and i can't develop locally without a mockup copy.
thanks for your help!
To get the schema, basically just select everything from the sys.objects catalog view:
SELECT * FROM sys.objects
For the data: you could use the undocumented (but extremely helpful) stored procedure sp_MSForEachTable for that purpose:
exec sp_MSforeachtable 'select top 100 * from ? '
I would create a cursor with sys.objects to get the user defined tables and populate the new database with select query of top 100 rows.
Make sure you have (NOLOCK) hint to your query, so that it can avoid locks

Resources