change data columns into JSON with different roots in SQL - 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

Related

Transform SQL Query to Oracle Query

I've got this SQL Query that I need to write for Oracle, please could you help ?
SELECT key_column_usage.column_name
FROM information_schema.key_column_usage
WHERE table_schema = SCHEMA()
AND constraint_name = 'PRIMARY'
AND table_name = 'posts'
I don't know what parts of your query represent, but - let me try. It looks like you'd like to query constraints table. If so, then one option is to query user_cons_columns:
from user_cons_columns c
where c.owner = user
and c.constraint_name = 'PRIMARY'
and c.table_name = 'POSTS'
where
user represents currently logged user
'PRIMARY' is name of that constraint
'POSTS' is table name; in Oracle, by default, table names are stored in UPPERCASE
Alternatively, join user_constraints and user_cons_columns:
from user_constraints r join user_cons_columns c on c.constraint_name = r.constraint_name
where r.owner = 'SCOTT'
and r.constraint_type = 'P'
and r.table_name = 'POSTS'
because constraint_type = 'P' represents primary keys (if that's what you actually want), and it is stored in user_constraints.
Just for example, this time I used 'SCOTT' as an owner name.
If you want and/or have sufficient privileges, instead of user_, you can query all_ or dba_ views to fetch information you need. So, that would be e.g. all_cons_columns etc.

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 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

Dynamically determining table name given field name in SQL server

Strange situation: I am trying to remove some hard coding from my code. There is a situation where I have a field, lets say "CityID", and using this information, I want to find out which table contains a primary key called CityID.
Logically, you'd say that it's probably a table called "City" but it's not... that table is called "Cities". There are some other inconsistencies in database naming hence I can never be sure if removing the string "ID" and finding out the plural will be sufficient.
Note: Once I figure out that CityID refers to a table called Cities, I will perform a join to replace CityID with city name on the fly. I will appreciate if someonw can also tell me how to find out the first varchar field in a table given its name.
SELECT name FROM sysobjects
WHERE id IN ( SELECT id FROM syscolumns WHERE name = 'THE_COLUMN_NAME' )
To get column information from the specified table:
SELECT column_name, data_type, character_maximum_length
FROM information_schema.columns
WHERE table_name = 'myTable'
select table_name from information_schema.columns where column_name='CityID'
You can use the INFORMATION_SCHEMA tables to read metadata about the database.
SELECT
TABLE_NAME
FROM
[db].[INFORMATION_SCHEMA].[COLUMNS]
WHERE
COLUMN_NAME='CityID';
For a primer in what's in the INFORMAITON_SCHEMA, see INFORMATION_SCHEMA, a map to your database
The information you seek is all available in the information schema views. Note that you will find many sources telling you how to directly query the underlying system tables that these are views onto - and I must admit that I do the same when it's just to find something out quickly - but the recommended way for applications is to go through these views.
For example, to find your CityID column:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'CityID'
To find the first varchar field in a table:
SELECT TOP 1 * FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_NAME = 'TableName'
AND DATA_TYPE = 'varchar' -- This is off the top of my head!
ORDER BY ORDINAL_POSITION
As I understand from your question, you want to find tables which contain CITYID column in primary key
You can use SQL Server system views like sysindexes and sysindexkeys as shown in SQL tutorial to query database table primary keys including composite primary keys which are formed
SELECT
TBL.name as TableName
FROM sysobjects as PK
INNER JOIN sys.objects as TBL
on TBL.object_id = PK.parent_obj
INNER JOIN sysindexes as IND
on IND.name = PK.name AND
IND.id = TBL.object_id
INNER JOIN SysIndexKeys as KEYS
on KEYS.id = IND.id AND
KEYS.indid = IND.indid
INNER JOIN syscolumns as COL
on COL.id = KEYS.id AND
COL.colid = KEYS.colid
WHERE
PK.xtype = 'PK' AND
COL.name = 'CityID'

Finding all Nullable Columns in SQL 2000 Database

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.