my task is to get the table structure in SQL Server (that means what are all the columns and their data type and constraints)
This will list all columns, the schema and table they belong to, their datatype, max length, precision and scale (for numerical types) - what more are you looking for??
SELECT
SchemaName = sch.name,
TableName = t.Name,
ColumnName = c.Name,
TypeName = ty.Name,
MaxLength = c.max_length,
Precision = c.precision,
Scale = c.scale
FROM
sys.columns c
INNER JOIN
sys.tables t ON t.object_id = c.object_id
INNER JOIN
sys.schemas sch ON sch.schema_id = t.schema_id
INNER JOIN
sys.types ty ON c.user_type_id = ty.user_type_id
sp_help can be used to get all the details about a table.
You can do something like this -
Execute sp_help yourtableName
Related
Using this SQL statement to find in my database all tables the have columns whose names contain ItemId:
SELECT o.name,
c.name
FROM sys.columns c
INNER JOIN sys.objects o ON c.object_id = o.object_id
WHERE c.name LIKE '%ItemId%'
ORDER BY o.name, c.column_id
I received a result containing two tables:
ResourceData
TT_Data_117F9D94
I know about the ResourceData table and I can find it in the list of my tables.
But I have not been able to find in my database any table name TT_Data_117F9D94.
Any idea where/how did this table show up in the result of my query?
First of all
You should have used
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%itemid%';
Probably TT_Data_117F9D94 is a data table in your database.
Please check the database design of that table. Or simply do a select query from that table.
Check about table types -
CREATE TYPE dbo.TT_Type AS TABLE (a INT)
SELECT o.name, c.name
FROM sys.columns c
JOIN sys.objects o ON c.[object_id] = o.[object_id]
WHERE c.name = 'a'
Correct query -
SELECT SCHEMA_NAME(o.[schema_id]), o.name, c.name
FROM sys.columns c
JOIN sys.objects o ON c.[object_id] = o.[object_id]
WHERE o.[type] = 'U' -- only user tables
I want to get ColumnName and it's datatype and is_identity for each column of different databases for specific table and schema.
I use following code but SCHEMA_NAME is for current database but I want to run this query from master or another database.
How to check schema name?
SELECT
c.name AS column_name, tp.name as data_type, c.is_identity iden
FROM
DatabaseName.sys.tables AS t
INNER JOIN
DatabaseName.sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
INNER JOIN
DatabaseName.sys.types tp ON c.user_type_id = tp.user_type_id
WHERE
t.name = 'PlaceType'
AND SCHEMA_NAME(t.schema_id) = 'Coding'
Instead of SCHEMA_NAME, use OBJECT_SCHEMA_NAME, like so:
SELECT c.name AS column_name,tp.name as data_type,c.is_identity iden FROM
DatabaseName.sys.tables AS t INNER JOIN
DatabaseName.sys.columns c ON t.OBJECT_ID = c.OBJECT_ID INNER JOIN
DatabaseName.sys.types tp ON c.user_type_id = tp.user_type_id where
t.name='PlaceType' and OBJECT_SCHEMA_NAME(t.object_id,DB_ID(Databasename))='Coding'
Just change the last condition like
and SCHEMA_NAME(t.schema_id) in (Select name from Sys.Databases where database_id > 4)
How can I get back the column information for a User-Defined Table Type?
EXEC sp_columns TABLENAME
gets me back all the column information for a table.
I want to do the same thing for a User-Defined Table Type called SearchList and roughly the same column information back.
I want to get this so I can code generate the Data Tables that I need in c#.
UPDATE TO SHOW WHAT I USED
select c.name as COLUMN_NAME, t.name as [TYPE_NAME], c.precision as [PRECISION], c.is_nullable as [NULLABLE], c.system_type_id, c.precision as [LENGTH]
from sys.columns c, sys.types t
where c.object_id = (select type_table_object_id from sys.table_types where name = 'SearchList')
and t.user_type_id = c.user_type_id
order by c.column_id
This will list all table types and their columns:
select tt.name, c.name from sys.table_types tt
inner join sys.columns c on c.object_id = tt.type_table_object_id
order by c.column_id
You can add a where clause and select other columns, as appropriate, to get what you need.
I have used below query in my project to get the table type with column name and data type.
select tt.name AS table_Type, c.name AS table_Type_col_name,st.name AS
table_Type_col_datatype
from sys.table_types tt
inner join sys.columns c on c.object_id = tt.type_table_object_id
INNER JOIN sys.systypes AS ST ON ST.xtype = c.system_type_id
order by tt.name
I am trying to get the table information give a table name so I wrote a query like this:
SELECT so.name, sc.name, st.name, sc.length, CASE WHEN sc.status = 0x80 THEN 'Y' ELSE 'N' END AS IsIdent, ColOrder
FROM Asdim.dbo.sysobjects so
INNER JOIN Asdim.dbo.syscolumns sc
ON so.id= sc.id
INNER JOIN Asdim.dbo.systypes st
ON sc.xtype = st.xusertype
WHERE so.Name = 'Admin'
The problem is that I have two tables with name 'Admin' but they have different schemas. So when I run this query:
SELECT * FROM Asdim.dbo.sysobjects WHERE name LIKE 'Admin'
I get two records since the table names are same. Is there a way that I caould filter out based on the schema name too?
The views you are using are all deprecated and just supplied for backward compatibility. Using the new views gives.
SELECT t.name,
c.name,
ty.name,
c.is_identity,
c.max_length,
c.column_id
FROM sys.tables t
JOIN sys.schemas s ON s.schema_id=t.schema_id
JOIN sys.columns c ON c.object_id = t.object_id
JOIN sys.types ty ON ty.user_type_id = c.user_type_id
WHERE t.name LIKE '%Admin%' AND s.name = 'dbo'
INFORMATION_SCHEMA.COLUMNS has nearly all the information you need but is missing info about identity columns.
Is there a command, or a set of tables I can look at to determine which tables, stored procedures and views in SQL Server server 2005 have a certain user defined data type?
For tables and views:
Select *
From Information_Schema.Columns
Where DOMAIN_NAME = 'YourUserDefinedTypeName'
For procedures and functions:
Select *
From Information_Schema.PARAMETERS
Where USER_DEFINED_TYPE_NAME = 'YourUserDefinedTypeName'
Tables are relatively easy, sys.columns and sys.types allow you to link columns to types. The query below will get this out.
select s.name
,o.name
,c.name
,t.name
from sys.schemas s
join sys.objects o
on o.schema_id = s.schema_id
join sys.columns c
on c.object_id = o.object_id
join sys.types t
on c.user_type_id = t.user_type_id
where t.name = 'Foo'
EDIT: as G Mastros has shown above, you can get parameters with a similar query.
select s.name
,o.name
,p.name
,t.name
from sys.schemas s
join sys.objects o
on o.schema_id = s.schema_id
join sys.parameters p
on p.object_id = o.object_id
join sys.types t
on p.user_type_id = t.user_type_id
where t.name = 'Foo'