How do i make it work?
SELECT * FROM
(SELECT TABLE_NAME FROM
databasename.INFORMATION_SCHEMA.TABLES);
How do i make it work?
SELECT * FROM
(SELECT TABLE_NAME FROM
databasename.INFORMATION_SCHEMA.TABLES);
The simplest query is:
SELECT name FROM [databasename].sys.tables;
But the better query is to take schema into account:
SELECT [schema] = s.name,
[table] = t.name
FROM [databasename].sys.tables AS t
INNER JOIN [databasename].sys.schemas AS s
ON t.[schema_id] = s.[schema_id];
You want to add a filter to prevent replication or diagram objects that you didn't create, e.g.
WHERE t.is_ms_shipped = 0;
If you're actually trying to say "I want all the data from all the tables" well, you'll need dynamic SQL for that, and I hope your tables aren't big.
DECLARE @sql nvarchar(max) = N'';
SELECT @sql += N'SELECT ' + char(39) + ts + char(39) + ';
SELECT * FROM ' + ts + N';'
FROM
(
SELECT ts = QUOTENAME(s.name) + N'.' + QUOTENAME(t.name)
FROM sys.tables AS t
INNER JOIN sys.schemas AS s
ON t.[schema_id] = s.[schema_id]
WHERE t.is_ms_shipped = 0
) AS x
PRINT @sql;
-- EXEC sys.sp_executesql @sql;
I would only use INFORMATION_SCHEMA
if I really needed metadata queries that were limited in scope because they need to work on multiple platforms. See this blog post for more info.