Finding all Nullable Columns in SQL 2000 Database - sql

How to find out column with NULL values allowed in the insert in whole database ?

I don't have sql at hand, but the query goes something like this
SELECT * FROM information_schema.columns WHERE is_nullable = 'YES'
In general, search for this stardard view, for all the metadata info about your schema and structure of the database; there are many others (information_schema.tables, information_schema.constraints, etc)

Those who only want to see columns from base tables (not views) should join with INFORMATION_SCHEMA.TABLES. I also like to exclude the system table sysdiagrams.
Query
SELECT
c.TABLE_NAME,
COLUMN_NAME,
DATA_TYPE
FROM
INFORMATION_SCHEMA.COLUMNS AS c
JOIN INFORMATION_SCHEMA.TABLES AS t ON t.TABLE_NAME = c.TABLE_NAME
WHERE
is_nullable = 'YES' AND
TABLE_TYPE = 'BASE TABLE' AND
c.TABLE_NAME != 'sysdiagrams'
ORDER BY
c.TABLE_NAME,
COLUMN_NAME
If you have duplicate table names across schemas or table catalogs, you should involve those fields in the join as well, as shown in the answers here:
Differentiating tables and views in INFORMATION_SCHEMA.COLUMNS.

Related

change data columns into JSON with different roots in SQL

in SQL Server, I have to change the data columns format into a JSON with different roots format, but I do not know how I should wrote.
Input:
PersonCode
Name
Realation
961113026
Sara Smite
Wife
Output:
[{"Name":{"DataType":"string","value":"Sara Smite"},"Relation":{"DataType":"string","value":"Wife"}}]
I found out what I should do.It might help you guys:
SELECT 'string' AS 'Name.DataType'
,Name AS 'Name.value'
,'string' AS 'Relation.DataType'
,Relation AS 'Relation.value'
FROM [dbo].[Person]
FOR JSON PATH
You can use INFORMATION_SCHEMA.TABLES and INFORMATION_SCHEMA.COLUMNS tables to get the data type.
SELECT c.DATA_TYPE as 'Name.DATA_TYPE',p.Name as 'Name.value',
c.DATA_TYPE as 'Relation.DATA_TYPE',p.Relation as 'Relation.value'
FROM Person p join INFORMATION_SCHEMA.TABLES t on t.TABLE_NAME = 'Person'
join INFORMATION_SCHEMA.COLUMNS c on t.TABLE_NAME = c.TABLE_NAME
GROUP BY c.COLUMN_NAME,c.DATA_TYPE,p.Name,p.
FOR JSON PATH

SQL multiple tables query for column names

I have multiple tables (number > 100) within a database in SQL, each table may have a few hundred entries.
For every table, I am seeking to retrieve simply the names of the columns from the tables which have at least 1 non-null entry.
How can I do this?
To return table/column name:
SELECT table_name, column_name
FROM information_schema.columns
That's pretty easy, here's a solution for nulls depending on if you have permissions:
select a.table_name
, schema_name
, sum(c.rows) total_rows
from
information_schema.tables a
join information_schema.schemas b on (a.schema_id = b.schema_id)
join information_schema.partitions c on (a.object_id = c.object_id)
where c.index_id in (0,1)
group by a.name,b.name
having sum(c.rows) = 0;
Note: I did this in vertica, and you have to have access to partitions. Also, some dbs use sys instead of information_schema, but the idea is the same.

SQL Query to lookup a column

I want to lookup if a column by the name of 'LOB' exist among thousands of views.
Schema name: PACONE
any help is appreciated here. Thanks.
That solution will depend on what Database Server you have.
If you have Oracle, go to Search an Oracle database for tables with specific column names?
If you have mySQL, go to How do i search a mysql database for a specific column name
If you have PostgreSQL, go to How to find a table having a specific column in postgresql
If you have SQL Server, this is a possible solution:
How-To: Find Fields or Tables Within a SQL Server Database
Suppose you need to find a field called GLASS_ID, you simply run this:
-- SEARCHING FOR A CERTAIN FIELD NAME --
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE UPPER(COLUMN_NAME) = 'GLASS_ID'
Similarly, you can look for all field names that end with 'CO'
-- SEARCHING FOR FIELD NAMES THAT END WITH... --
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE UPPER(COLUMN_NAME) LIKE '%CO'
Of course, this is supposed to work under SQL Server, and that was tested in SQL Server 2005 only, but I guess that it will work in any recent version too.
use [your database name]
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name = 'LOB'
ORDER BY schema_name, table_name;
First of all you should provide some information about your environment.
If you use Oracle you can query the dba_tab_columns with dab_view (user_ or all_ is also possible) e.g.:
select a.view_name
from dba_views a
join dba_tab_columns b
on a.view_name = b.table_name
where lower(b.column_name) = 'lob'
and lower(b.owner) = <view_owner>;

SQL Information_Schema Syntax for finding servername, table and column details

I am looking for SQL Information_Schema syntax to retrieve Server Name, Table Name, Column Name, View and all the details.
I also need to find which Table : Column has Image files.
Is there any specific syntax to help me out.
There are many different information_schema views. If you want to see what views are available then in SSMS object explorer you can navigate to databases > system databases > msdb > views > system views and scroll down to the information_schema. Since these are views you can just query them. From your question the ones you'll be interested in are
INFORMATION_SCHEMA.columns
INFORMATION_SCHEMA.tables
INFORMATION_SCHEMA.views
Here's an example query that lists servername and all columns
select ##servername, *
from INFORMATION_SCHEMA.columns
You can join the views and filter your data just like you would any query. Hope this helps.
select *
from information_schema.tables t
join INFORMATION_SCHEMA.columns c
on t.table_name = c.table_name
where c.data_type = 'image'

SQL Script To Generate a Database Dictionary **With Linked Fields**

I would like to generate a Data Dictionary for a SQL Server 2008 database that has one row for each field, and the following columns:
table_name
field_name
data_type
link_table (for when the field in question is a foreign key)
link_field (for when the field in question is a foreign key)
I can get the first 3 columns with something like the SQL script below...but I don't know how to get the last two columns of foreign key information. INFORMATION_SCHEMA.TABLE_CONSTRAINTS gets close, but doesn't have the data I'm looking for. Can someone help with this point?
SELECT TABLE_NAME,COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
Secondarily if anyone has any suggestions on additional fields which would be helpful please post.
The following query will duplicate a column if it is involved in more than one relationship.
Select C.TABLE_SCHEMA, C.TABLE_NAME, C.COLUMN_NAME, C.DATA_TYPE
, PKCol.TABLE_SCHEMA, PKCol.TABLE_NAME, PKCol.COLUMN_NAME
From INFORMATION_SCHEMA.COLUMNS As C
Left Join (INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As FKCol
Join INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS As FK
On FK.CONSTRAINT_NAME = FKCol.CONSTRAINT_NAME
Join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As PKCol
On PKCol.CONSTRAINT_NAME = FK.UNIQUE_CONSTRAINT_NAME)
On FKCol.TABLE_SCHEMA = C.TABLE_SCHEMA
And FKCol.TABLE_NAME = C.TABLE_NAME
ANd FKCol.COLUMN_NAME = C.COLUMN_NAME