Database issue for everyone (QUERY) - database
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.
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'
Invalid column name 'TABLETYPE' in INFORMATION_SCHEMA.TABLES
The following statement: SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLETYPE = 'VIEW'; Throws 'Invalid column name 'TABLENAME'. But the column is shown in query. I tried to replace 'TABLENAME' for 'tablename' and nothing worked. I read that this could be a InteliSense cache problem, that is needs to be refreshed. But I execute statements like this in my application, so unless I can be able to do it via sql, that is not really an option for me. Correct me if I am wrong :)
The 'Table Type' column has an underscore in it, so you'll want to use: SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'VIEW';
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.
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
doing select from stored procedure such as : select * from sp_tables
is there a way to simply select the result returned from sp_tables?
No there isn't. But you could try to replace sp_tables by querying the information_schema. For example: sp_tables 'T_Raum' You can substitute with this: SELECT TABLE_CATALOG AS TABLE_QUALIFIER, TABLE_SCHEMA AS TABLE_OWNER, TABLE_NAME, CASE TABLE_TYPE WHEN 'BASE TABLE' THEN 'TABLE' ELSE TABLE_TYPE END AS TABLE_TYPE, NULL AS REMARKS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE != 'VIEW' AND TABLE_NAME = 'T_Raum' I don't know what exactly sp_tables does, or what you need it for, but at least in this case, it seems to do the above schema query.
If you call sp_tables, the result you get is the result of a SELECT statement. In that respect, it is no different than any store procedure that returns a result set.
If you mean you want to filter the resultset returned by that sproc then depending on what you want to filter on, you can pass in params to that sproc (e.g. #table_name parameter, which supports wildcards). Check out the BOL ref on sp_tables Alternatively, you will need to insert the results into a temp table and select from that. Or finally, depending on what you want, you could query the sys tables directly. If only interested in tables: SELECT * FROM sys.tables WHERE...
If you want to select from each table, you can use the undocumented sp_msforeachtable command: sp_msforeachtable 'SELECT * FROM ?' The ? is a wildcard that indicates the current table name, and the command needs to be a string enclosed in single quotes just like in other Dynamic SQL.