Putting many tables in to one table in SQL Server? - sql-server

I'm trying to insert the result from a query into a new table.
I'm using this query and want to gather the result into a single table.
The query (I found somewhere) looks like this:
USE [AdventureWorksDW2012]
SELECT
OBJECT_SCHEMA_NAME(T.[object_id],DB_ID()) AS [Schema],
T.[name] AS [table_name],
AC.[name] AS [column_name],
TY.[name] AS system_data_type,
AC.[max_length],
AC.[precision], AC.[scale],
AC.[is_nullable], AC.[is_ansi_padded]
FROM
sys.[tables] AS T
INNER JOIN
sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
INNER JOIN
sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id]
AND AC.[user_type_id] = TY.[user_type_id]
WHERE
T.[is_ms_shipped] = 0
ORDER BY
T.[name], AC.[column_id];

Try using an INTO clause like this:
USE [AdventureWorksDW2012]
SELECT OBJECT_SCHEMA_NAME(T.[object_id],DB_ID()) AS [Schema],
T.[name] AS [table_name], AC.[name] AS [column_name],
TY.[name] AS system_data_type, AC.[max_length],
AC.[precision], AC.[scale], AC.[is_nullable], AC.[is_ansi_padded]
INTO dbo.MyNewTable
FROM sys.[tables] AS T
INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
INNER JOIN sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id]
AND AC.[user_type_id] = TY.[user_type_id]
WHERE T.[is_ms_shipped] = 0
ORDER BY T.[name], AC.[column_id]
;

Use INTO clause as next:-
USE [AdventureWorksDW2012]
SELECT OBJECT_SCHEMA_NAME(T.[object_id],DB_ID()) AS [Schema],
T.[name] AS [table_name], AC.[name] AS [column_name],
TY.[name] AS system_data_type, AC.[max_length],
AC.[precision], AC.[scale], AC.[is_nullable], AC.[is_ansi_padded]
Into New_table -- this line is added.
FROM sys.[tables] AS T
INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
INNER JOIN sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id] AND AC.[user_type_id] = TY.[user_type_id]
WHERE T.[is_ms_shipped] = 0
ORDER BY T.[name], AC.[column_id]
The sample code is
Select *
Into New_table
From Exist_Table
and as MSDN says:-
SELECT…INTO creates a new table in the default filegroup and inserts
the resulting rows from the query into it.

Related

SQL Server 2012 Metadata Syntax

Sorry for the dumb question in advance and thanks for your time!
I have two queries
Query A
SELECT sc.name +'.'+ta.name TableName
,SUM(pa.rows) RowCnt
FROM sys.tables ta
INNER JOIN sys.partitions pa
ON pa.object_id = ta.object_id
INNER JOIN sys.schemas sc
ON ta.schema_id=sc.schema_id
WHERE ta.is_ms_shipped = 0 AND pa.index_id IN (1,0)
GROUP BY sc.name,ta.name
ORDER BY sum(pa.rows) DESC;
and
Query B
SELECT ta.name TableName
, ta.modify_date ModDate
, ta.max_column_id_used "Columns"
FROM sys.tables ta
ORDER BY ta.name;
which execute fine.
When I add the lines
, ta.modify_date ModDate
, ta.max_column_id_used "Columns"
I create Query C
SELECT sc.name +'.'+ta.name TableName
,SUM(pa.rows) RowCnt
, ta.modify_date ModDate
, ta.max_column_id_used "Columns"
FROM sys.tables ta
INNER JOIN sys.partitions pa
ON pa.object_id = ta.object_id
INNER JOIN sys.schemas sc
ON ta.schema_id=sc.schema_id
WHERE ta.is_ms_shipped = 0 AND pa.index_id IN (1,0)
GROUP BY sc.name,ta.name
ORDER BY sum(pa.rows) DESC;
I get the error
8120, Level 16, State 1, Line 3
Column 'sys.tables.modify_date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I understand what aggregate fx are as well as the purpose of GROUP BY.
Do I need to create a subquery to include this information in one query

How can we check whether data exists or not in a table thru sys. tables/functions directly

Is there any way to check whether data exists in a table thru sys. tables or functions directly without querying the table.
Any such sys. available?
** Not querying the dynamic sql..:)
Read this article:
Find Row Count in Table – Find Largest Table in Database
Here is a query to find a ROWCOUNT of a table:
SELECT SUM(pa.rows) RowCnt
FROM sys.tables ta
INNER JOIN sys.partitions pa
ON pa.OBJECT_ID = ta.OBJECT_ID
WHERE ta.is_ms_shipped = 0
AND pa.index_id IN (1,0)
AND ta.name='table1'
SQLFiddle demo
Or if you need only information about empty table or not then something like this:
SELECT
ISNULL(
(SELECT TOP 1 1 from sys.partitions pa
where pa.OBJECT_ID = ta.OBJECT_ID
AND
pa.rows>0
AND
pa.index_id IN (1,0)
)
,0) as TableIsNotEmpty
FROM sys.tables ta
WHERE ta.is_ms_shipped = 0
AND ta.name='table1'
-- This is how I got the result...
SELECT Distinct tbl.name, C.name , X.rowcnt
FROM Sys.Columns as c
INNER JOIN Sys.Tables as tbl
ON tbl.object_id = c.object_id
INNER JOIN Sys.Types as t
ON c.system_type_id = t.system_type_id
INNER JOIN Sys.Indexes I
ON I.object_id = Tbl.object_id
Inner Join Sys.sysindexes X
On I.index_id = X.indid
And I.object_id = X.id
WHERE X.rowcnt > 0
ORDER BY tbl.name

How to find list of tables having no records in SQL server

How to display a list of tables having no record in them and they are existing in the sql server database.Required is only to show tables with no record in them.
Try this:
SELECT
t.NAME AS TableName,
p.rows AS RowCounts
FROM
sys.tables t
INNER JOIN
sys.partitions p ON t.object_id = p.OBJECT_ID
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND p.rows = 0
GROUP BY
t.Name, p.Rows
ORDER BY
t.Name
The query goes to the sys.tables and other catalog views to find the tables, their indexes and partitions, to find those tables that have a row count of 0.
Alteration to add Schema names:
SELECT
sch.name,
t.NAME AS TableName,
p.rows AS RowCounts
FROM
sys.tables t
INNER JOIN
sys.partitions p ON t.object_id = p.OBJECT_ID
inner Join sys.schemas sch
on t.schema_id = sch.schema_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND p.rows = 0
GROUP BY
sch.name,t.Name, p.Rows
ORDER BY
sch.name,t.Name
I found some of the previous answers still returned tables with data for me.
However, the following seems to correctly on report those with no rows (using SQL Server 2019):
SELECT schema_name(tab.schema_id) + '.' + tab.name AS [table]
FROM sys.tables tab
INNER JOIN sys.partitions part ON tab.object_id = part.object_id
WHERE part.index_id IN (
1
,0
) -- 0 - table without PK, 1 table with PK
GROUP BY schema_name(tab.schema_id) + '.' + tab.name
HAVING sum(part.rows) = 0
ORDER BY [table]

how to get table's schema name

I'm using SQL Server 2008 and have the following query:
SELECT SO1.name AS Tab,
SC1.name AS Col,
SO2.name AS RefTab,
SC2.name AS RefCol,
FO.name AS FKName
FROM dbo.sysforeignkeys FK
INNER JOIN dbo.syscolumns SC1 ON FK.fkeyid = SC1.id AND FK.fkey = SC1.colid
INNER JOIN dbo.syscolumns SC2 ON FK.rkeyid = SC2.id AND FK.rkey = SC2.colid
INNER JOIN dbo.sysobjects SO1 ON FK.fkeyid = SO1.id
INNER JOIN dbo.sysobjects SO2 ON FK.rkeyid = SO2.id
INNER JOIN dbo.sysobjects FO ON FK.constid = FO.id
How do you retrieve the table's schema name?
Thanks for your help
Use OBJECT_SCHEMA_NAME
SELECT
OBJECT_SCHEMA_NAME(f.parent_object_id) AS TableNameSchema, -- this
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id,fc.parent_column_id) AS ColumnName,
OBJECT_SCHEMA_NAME(f.referenced_object_id) AS ReferenceTableNameSchema,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName,
f.name AS ForeignKey
FROM
sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
As per Sql Server 2008:
SELECT Object_name(f.parent_object_id)
AS
TableName,
Col_name(fc.parent_object_id, fc.parent_column_id)
AS ColumnName,
Object_name (f.referenced_object_id)
AS ReferenceTableName,
Col_name(fc.referenced_object_id, fc.referenced_column_id)
AS
ReferenceColumnName,
f.name
AS ForeignKey,
Quotename(Schema_name(f.schema_id)) + '.' + Quotename(
Object_name(f.parent_object_id)) AS
schemaname,
Quotename(Schema_name(o.schema_id)) + '.' + Quotename(
Object_name(f.referenced_object_id))
AS ReferenceSchemaName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN sys.objects AS o
ON o.OBJECT_ID = fc.referenced_object_id
If to want to know schema name on basis of object_id then use OBJECT_SCHEMA_NAME(), if you want to get schema name on basis of schema_id then use SCHEMA_NAME().

Get the creation date of a table's column

I found a script to get the column information:
SELECT
crdate as thisFieldIsNotTheCreationOfTheField1 ,
refdate as thisFieldIsNotTheCreationOfTheField2 ,
o.name AS [TableName],
o.type,
c.name AS [ColName],
s.name AS [ColType],
c.prec,
c.scale,
c.isnullable
FROM
dbo.sysobjects AS o
INNER JOIN
dbo.syscolumns AS c ON c.id = o.id
INNER JOIN
dbo.systypes AS s ON c.xtype = s.xtype
WHERE
o.type = 'U' and o.name='TableNa'
ORDER BY
crdate
The column creation datetime isn't found.
How can this be done?
This is what you want :
SELECT obj.create_date
from sys.objects obj
inner join sys.columns col on obj.object_Id=col.object_Id
WHERE col.name = #columnName
and obj.Name=#tableName

Resources