SQL Server - Constructing a Column Definition [duplicate] - sql

This question already has answers here:
In SQL Server, how do I generate a CREATE TABLE statement for a given table?
(16 answers)
Closed 8 years ago.
I need to compare two databases and identify what columns have changed. Once I have identified a column that has changed in some way (size, type, etc) I need to capture (write to a table) the old column definition and the new column definition.
For instance, if using the INFORMATION_SCHEMA.COLUMNS table I discover that a column size has changed from 25 to 50 I will need to store the two column definition. In this case it may be 'char(25)' and 'char(50)'.
I have not problem using the INFORMATION_SCHEMA.COLUMNS table to identify when something has changed.
The issue I have is once I determine that the column has changed how do I build the column definition? In this case how to I build 'char(25)' and 'char(50)'?
Is there somewhere I can obtain this type of definition? If I have to build the definition piece by piece how do I determine all the components of the definition.
Any advice or suggestions are appreciated.
Thanks in advance.

select table_name, column_name,
column_definition = data_type + isnull('(' + convert(varchar, character_maximum_length) + ')', '')
from information_schema.columns

Related

How to check, find and see the values of a specific column inside of a big database with many tables, if the column actually exists [duplicate]

This question already has answers here:
Find a string by searching all tables in SQL Server
(12 answers)
Closed 1 year ago.
I have a lot of tables inside some database of SQL Server and I think that some of those tables have some specific column that, I want to check if it exists and also see the values inside, if it actually exists.
I guess that the column was named like: ‘’Status’’
Please, consider that I don't have any idea about what are the values that maybe exist inside of this supposed column or even the kind of it.
Database name: PrincipalGroup
I won't say the name of the tables, because I don’t think it's feasible to write all the tables in this query, because there are many.
So, the point is: how can I query this by the easiest and simplest way?
You can try the following query that will produce a list of SQL statements you can then execute to "see the values inside" each table that contains the column named Status, and obviously tweak to your specific requirements.
select Concat('select distinct ', QUOTENAME(table_name, ''''), ' , [Status] from ', QuoteName(table_name))
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'Status'

Find list of tables from list of cols [duplicate]

This question already has answers here:
Find all tables containing column with specified name - MS SQL Server
(35 answers)
Closed 2 years ago.
I did google this a bit but my biggest problem is not knowing what to search. I felt like no matter what I searched it wasn't my problem so if you have a link to another question do to my search inability feel free to link it and be done with it.
Say I have like 250 different fields (columns) and they are all from 1 of 10 tables but I need to figure out which field comes from which table. Maybe i could do a massive select statement but there are possible duplicate column names and that sound like a headache
looking at each column name and the scouring through all these tables to find each one seems too difficult? Any thoughts? Does my question make sense? Let me know if you need more information.
you can use information_schema.columns to get information
select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME in ('col1', 'col2', ...)
For SQL Server you can find metadata information from the system base tables
SELECT t.name AS table_name,
c.name AS column_name
FROM sys.tables AS t
JOIN sys.columns AS c ON t.object_id = c.object_id
WHERE c.name IN('col1','col2') --give the columns names here.
Now, this code is specific to SQL Server and not a portable code.
But few cases to be noted that instead of using INFORMATION_SCHEMA which doesn't always have all the information like if the column has an IDENTITY property as it is proprietary to SQL Server.
You can read more about this here

Displaying the name and data type of every column in a table [duplicate]

This question already has answers here:
How To Find Datatypes information in oracle schema?
(2 answers)
Closed 9 years ago.
I am working on a Database Management Systems assignment and have been unable to find a way to display the name and datatype of every column in a specific table. I don't know if this is needed but here is the link to the schema I am using for this assignment.
http://mym.cdn.laureate-media.com/2dett4d/Walden/ITEC/ITEC2060/documents/Schema.sql
The employee table is what I am trying to display from.
To list all columns for a specific table:
select column_name, data_type
from all_tab_columns
where table_name = 'EMPLOYEES';
More details in the manual: http://docs.oracle.com/cd/E11882_01/server.112/e25513/statviews_2103.htm#I1020277
If you are using SQL*Plus you can simply run
desc EMPLOYEES
instead.
Most other SQL clients have a similar feature.

How do I return a list of all column titles in a table in SQLite? [duplicate]

This question already has answers here:
How can I get the list of a columns in a table for a SQLite database?
(9 answers)
Closed 9 years ago.
I have a 20-or-so column table 'Contacts' in a Contacts.sqlite file. I use the Mac Terminal to interact with it, and am writing a program that will perform certain actions based on the number of columns in the table, as well as the name of each column.
Thus, I need to perform some sort of query that will return a list of all my column titles.
I have found various recommendations that look like some version of this:
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'Schema' AND table_name = 'Table_Name'
but it almost always returns an error saying there is "no such table: information_scheme.columns".
Looks like SQLite does not support information_schema. You can retrieve the list of table names from sqlite_master. But there is no table with columns.
Instead, SQLite supports the proprietary pragma table_info():
pragma table_info(table_name);

How can I SELECT that lists all the field names and their types for a given Db Table? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
SQL Server: How do you return the column names from a table?
Assume that I have Data base table: LunchMenuItems
For the MS-SQL Server, how can I write a TSQL query that :
Lists all the column names and their types in LunchMenuItems table?
Description
This Information can be found in information_schema.columns.
Sample
select column_name, DATA_TYPE from information_schema.columns
where table_name = 'LunchMenuItems'
More Information
COLUMNS (Transact-SQL)