List of available keywords for SQL Server - 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.

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'

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.

How to find the stored procedures acting on a table in SQL Server 2012?

I have a table in a database. The fields of the table are being updating by some stored procedure from some other table. Is there any possibility that I can find the stored procedures which are acting on a particular table???
If you want to know which stored procedures are likely to touch your table, you could search that in sys.procedures something on the lines of:
select * from sys.procedures
where object_definition(object_id) like '%tablename%'
-- or probe information_schema
select * from information_schema.routines
where routine_definition like '%tablename%'
Object_Definition
Returns the Transact-SQL source text of the definition of a specified object.
https://msdn.microsoft.com/en-us/library/ms176090(v=sql.105).aspx
When we look into sys.procedures, there'll be a field called object_id. Each object in SQL Server has an ID. object_definition will retrieve definition of that object - in our case, text of the procedure will be retrieved.
Check out the sys.dm_sql_referencing_entities view.
But this won't work if the SP build dynamic SQL to access the table (not schema bound). You might need to use trace or SQL Profiler to help out.
Maybe you use this query. You got a list store procedure in actual database
SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE= 'PROCEDURE'

SSMS Searching for a Column

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

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.

Resources