I execute a select to get the structure of a table.
I want to get info about the columns like its name or if it's null or if it's primary key..
I do something like this
....sys.columns c...
c.precision,
c.scale,
c.is_nullable as isnullable,
c.default_object_id as columndefault,
c.is_computed as iscomputed,
but for default value i get the id..something like 454545454 but i want to get the value "xxxx". What is the table to search or what is the function to convert that id to the value.
Thanks
You can do this (done a SELECT * just so you can see all the info available):
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE....
This includes a "COLUMN_DEFAULT" column in the resultset.
Use
Select * From INFORMATION_SCHEMA.COLUMNS
there is a column called COLUMN_DEFAULT
The property you want is called "cdefault".
http://sql-server-performance.com/Community/forums/p/20588/114944.aspx
'bills' is an example table
select
COLUMN_DEFAULT --default
,IS_NULLABLE -- is nullable
,NUMERIC_PRECISION --number of digits (binary or decimal depending on radix)
,NUMERIC_PRECISION_RADIX --decimal places
,NUMERIC_SCALE --number of digits to right of decimal point
,COLUMNPROPERTY(OBJECT_ID('bills'),COLUMN_NAME,'Iscomputed') AS ISCOMPUTED --is computed
from INFORMATION_SCHEMA.columns where TABLE_name='bills'
select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where TABLE_NAME='bills' and CONSTRAINT_TYPE='PRIMARY KEY'
Related
I need to calculate Min/Max/etc. values for columns in my a table. So I want to filter the columns from INFORMATION_SCHEMA to get only the numeric ones.
The following query returns an error: Error converting data type varchar to int. and I don't know how to fix it.
SELECT #vQuery =
'SELECT '''+TABLE_NAME+''' AS TableName
, '''+COLUMN_NAME+''' AS ColumnName
, '''+DATA_TYPE+''' AS DataType
, MIN(TRY_CAST(TRY_CAST(['+COLUMN_NAME+'] AS VARCHAR(MAX)) AS NUMERIC(30,4))) AS MinValue
, MAX(TRY_CAST(TRY_CAST(['+COLUMN_NAME+'] AS VARCHAR(MAX)) AS NUMERIC(30,4))) AS MaxValue
, AVG(TRY_CAST(TRY_CAST(['+COLUMN_NAME+'] AS VARCHAR(MAX)) AS NUMERIC(30,4))) AS AvgValue
, STDEV(TRY_CAST(TRY_CAST(['+COLUMN_NAME+'] AS VARCHAR(MAX)) AS NUMERIC(30,4))) AS StandardDeviation
, SUM(TRY_CAST(TRY_CAST(['+COLUMN_NAME+'] AS VARCHAR(MAX)) AS NUMERIC(30,4))) AS TotalSum
FROM '+QUOTENAME(TABLE_SCHEMA)+'.'+QUOTENAME(TABLE_NAME)+';'+ CHAR(10)
FROM
(SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = #Schema
AND TABLE_NAME = #Table
AND DATA_TYPE IN ('BIGINT','NUMERIC','SMALLINT','DECIMAL','SMALLMONEY','INTEGER','INT','TINYINT','MONEY','FLOAT','REAL')) t
This is a bit long for a comment:
Your query appears to work here.
If you were not using the try_() functions, then the problem might occur when you run the query but not when you generate it. And the issue is exponential notation. For instance:
select convert(varchar(255), convert(float, 1234556678990.0))
returns:
'1.23456e+012'
And this value cannot be converted to a numeric value.
I see no advantage to converting to a string before converting to a numeric, so I would drop those conversions. However, with try_cast() this should not be an issue.
In fact, you are generating separate queries for each type. I don't think any conversion is warranted. The queries can return different types if they want. You would need to be concerned about types if you connected the queries using UNION ALL.
Note: You should also use QUOTENAME() on the column names. It can protect against SQL injection, but that seems like a very unlikely problem when you are selecting from INFORMATION_SCHEMA tables (someone would have to create very curious table/column names). However, it also handles non-conforming column names, such as those with spaces.
Is it possible to display just certain table names in the query:
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
Most of the time I would need all the table names but there are curtain situations where I need just certain row names.
When I try doing the following:
USE [WebContact]
SELECT COLUMN_NAME, ContactDateTime
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
It tell me that
Invalid column name 'ContactDateTime'
even though that is one of the row names.
Is this possible to do?
The column ContactDateTime may be a column in your table but it is not a column in the INFORMATION_SCHEMA.COLUMNS view.
Since it is not a column there, SQL Server is going to error out saying that it is invalid.
I think what you're trying to do is add another WHERE clause to your statement:
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
AND [COLUMN_NAME] = 'ContactDateTime'; -- Here!
Or if you want to add multiple columns...
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
AND [COLUMN_NAME]
IN ('ContactDateTime', 'column2', 'column3', ... 'column(n)'); -- Here!
Also see here for the case against using INFORMATION_SCHEMAS.
if ContactDateTime is a column that you are looking for in table memberEmails then you can do this
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
and COLUMN_NAME='ContactDateTime'
The view INFORMATION_SCHEMA.COLUMNS don't have column name ContactDateTime. Please read document first
I have thousand of tables in database. Some names end with _History.
For example :
abc_History
bcd_History
123_History
How do I find all tables which name is end with _History.
Some thing like:
SELECT
table_name
FROM sys.tables WHERE table_name LIKE '_History%'
And
error : Invalid column name 'table_name'.
Try this:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.tables
WHERE TABLE_NAME LIKE '%_History'
OR
SELECT name
FROM sys.tables
WHERE name LIKE '%_History'
Thanks to #Saharsh-shah.
To get table name with all column names in the same query, use this :
SELECT
`TABLE_NAME`,
group_concat(`COLUMN_NAME` separator ',') AS `COLUMNS`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_NAME` IN(
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.tables
WHERE `TABLE_NAME` LIKE '%_History'
)
GROUP BY `TABLE_NAME`
You can push it in multidimensional PHP array very simply with the following :
$tables['columns'] = explode(',', $tables['columns']);
Hope it can help some.
So I have this:
CREATE PROCEDURE getBattingColumnNames
AS
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Batting'
AND COLUMN_NAME NOT IN ('playerID','yearID','stint','teamID','lgID', 'G_batting','GIDP','G_old');
GO
And it works great. I get all the column names that I want, in c# I use this to populate a drop down with the column names. however, one of my column names, "Doub" I would like to change. So playing around with it I tried:
SELECT COLUMN_NAME.Doub AS 'DB'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE (TABLE_NAME = 'Batting')
and a variation of that, and the error is the mulitplart identifier could not be bound. How can i change that column name in this query?
You could use a case to translate the column name:
select case COLUMN_NAME
when 'Doub' then 'DB'
else COLUMN_NAME
end
from ...
I added a table to a database with the following column names and datatypes
"ID" bigInt, "Description" nchar(11)
If I check the design view of the table, the datatypes of the columns are as I set them.
If I write a query that will get the columns in that table from SYS.COLUMNS the column properties are different e.g.(the max_length of description is 22, not 11)
Can someone explain why that is?
I am using Microsoft SQL Server Enterprise Edition (64-bit) if that is relevant.
The column max_length in sys.columns show the max length in bytes. nchar is double byte so 11 chars take 22 bytes.
If you want the number of characters you could use INFORMATION_SCHEMA.COLUMNS
select COLUMN_NAME, CHARACTER_MAXIMUM_LENGTH
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'YourTable'
Or you could use the function COLUMNPROPERTY
select name,
columnproperty(object_id, name, 'charmaxlen') as charmaxlen
from sys.columns
where object_name(object_id) = 'YourTable'