Find SQL Server Tables that have two specified column names - sql-server

I would like to search tables in sql server for a table that has two specific column names ex (columnA and columnB). I have a query that searches on one column name
SELECT name FROM sysobjects WHERE id IN
( SELECT id FROM syscolumns WHERE name = 'columnA' )
I don't know how to modify this to search for two columns.

SELECT name FROM sysobjects WHERE id IN
( SELECT id FROM syscolumns WHERE name = 'columnA' )
and id in
( SELECT id FROM syscolumns WHERE name = 'columnB' )
should do the trick.

This is the right way to do it:
select so.name
from sysobjects so
where so.type = 'U' -- it's a user's table
and exists (select * from syscolumns sc where sc.id = so.id and sc.name='columnA')
and exists (select * from syscolumns sc where sc.id = so.id and sc.name='columnB')
It's important to check that it's a user table. On the contrary you could find views, table valued functions and so on.

Try something like:
select syscolumns.id, sysobjects.name from syscolumns
join sysobjects so on sysobjects.id = syscolumns.id
where exists (select 1 from syscolumns sc where sc.id = syscolumns.id and name = 'columnA')
and exists (select 1 from syscolumns sc2 where sc2.id = syscolumns.id and name = 'columnB')

Related

Join INFORMATION_SCHEMA.TABLES with another table with more TABLE_NAMES and result would be different if table name is in the first table or not

we have a Table with a list of table names we want to be created. They don't have an ID column or anything, it's just a few rows of data with 2 columns. Thing is we want to merge that table with Information_schema.table to check which of the tables we have already created and which we have not, so we wrote the query below as a temp to achieve such:
with cte1 as (
select d.TABNAME, d.CLASS from dbo.table_list as d
left join INFORMATION_SCHEMA.TABLES as t on t.TABLE_NAME = d.TABNAME
where d.CLASS in ('INIT','STERN') and table_schema = 'dbo'),
cte2 as (select d.TABNAME, d.CLASS
from dbo.table_list as d
where d.CLASS in ('INIT','TERN') and d.TABNAME not in (select [TABLE NAME] from cte1))
select *, 'Active' as [Status] from cte1 union all
select * , 'Inactive' from cte2
This is what table_list looks like:
TABNAME
CLASS
TABLE1
INIT
TABLE2
STERN
TABLE3
STERN
TABLE4
STERN
TABLE5
INIT
We already have TABLE1 and TABLE2 created so the result of the query looks like this:
TABNAME
CLASS
STATUS
TABLE1
INIT
Active
TABLE2
STERN
Active
TABLE3
STERN
Inactive
TABLE4
STERN
Inactive
TABLE5
INIT
Inactive
It works well enough like this but we were wondering if we could make it shorter.
This can be way shorter, yes. You could just reference the table dbo.table_list and see if you get a valid OBJECT_ID:
SELECT tl.TABNAME,
tl.CLASS,
CASE WHEN OBJECT_ID(N'dbo.' + QUOTENAME(tl.TABNAME)) IS NULL THEN 'Inactive' ELSE 'Active' END AS Status
FROM dbo.table_list tl --"d" for "table_list" doesn't make a lot of sense.
WHERE tl.CLASS IN ('INIT','STERN');
If you wanted to use the catalog views, you could use CROSS APPLY to join to the table while supplying a value for both the schema and table name, or just JOIN to sys.schemas based on a literal and then LEFT JOIN to sys.tables:
SELECT tl.TABNAME,
tl.CLASS,
CASE WHEN st.[name] IS NULL THEN 'Inactive' ELSE 'Active' END AS Status
FROM dbo.table_list tl --"d" for "table_list" doesn't make a lot of sense.
CROSS APPLY (SELECT t.[name]
FROM sys.schemas s
JOIN sys.tables t ON s.schema_id = t.schema_id
WHERE s.[name] = N'dbo'
AND t.[name] = tl.TABNAME) st
WHERE tl.CLASS IN ('INIT','STERN');
SELECT tl.TABNAME,
tl.CLASS,
CASE WHEN t.[name] IS NULL THEN 'Inactive' ELSE 'Active' END AS Status
FROM dbo.table_list tl --"d" for "table_list" doesn't make a lot of sense.
JOIN sys.schemas s ON s.[name] = N'dbo'
LEFT JOIN sys.tables t ON s.schema_id = t.schema_id
AND tl.TABNAME = t.[name]
WHERE tl.CLASS IN ('INIT','STERN');

System Versioned Table check schema from sys.tables

How to check if a table has versioning enabled by specifying not only the table name but also the schema.
I use:
IF EXISTS (SELECT 1 FROM sys.tables
WHERE NAME = 'tablename'
AND temporal_type = 2)
SELECT 1 AS TABLE_EXIST
ELSE
SELECT 0 AS TABLE_EXIST
Regards
You can JOIN sys.schemas and filter on the name column from both tables:
IF EXISTS (
SELECT 1
FROM sys.tables t
INNER JOIN sys.schemas s ON s.schema_id = t.schema_id
WHERE temporal_type = 2
AND s.name = 'schemaname'
AND t.name = 'tablename'
)
SELECT 1 AS TABLE_EXIST
ELSE
SELECT 0 AS TABLE_EXIST;
You can use the SCHEMA_ID function, which exists to simplify queries like this:
SELECT *
FROM SYS.tables
WHERE
NAME = 'tablename'
and schema_id = schema_id('dbo')
and temporal_type = 2

Return Stored Procedures, tables and columns in reference to a particular database

I am looking to write a query that will return all of the stored procedures and views that reference a specific database that I am looking to rebuild. I also need the query to return the columns and table names that the returned stored procedures / views contain in reference to the database being rebuilt.
I have a query right now that will return the stored procs and views, but have been unsuccessful in returning the column and table names that reference the database.
SELECT Distinct so.Name, so.type--, sc.text
FROM sysobjects so(NOLOCK)
INNER JOIN syscomments sc (NOLOCK) on so.Id = sc.ID
WHERE so.Type in( 'p' , 'v')
AND sc.Text LIKE '%membership_dw.%'
ORDER BY so.Name
Membership_DW is the name of the database where the stored procedures and views are currently stored, and it is also the database that is being recreated.
Here is a quick and dirty query to do it
;WITH cte as(
select *
from sys.sysdepends t1
inner join sys.objects t2 on t1.depid = t2.object_id
where t2.type = 'U' and is_ms_shipped = 0)
SELECT t5.name as SP_NAME, t6.name as TBL_NAME, t9.name COLNAME from sys.objects t5
INNER JOIN cte t6 on t6.id = t5.object_id
INNER JOIN sys.columns t9 on t6.object_id = t9.object_id
where t5.is_ms_shipped = 0 and t6.name not like 'sys%'
order by sp_name, TBL_NAME

Find tables for which specific column not exists sql server

How to find tables in which specific column not exists.
E.g column ID not present in table Contact, then Contact table will be in result set.
I tried:
SELECT DISTINCT t.name
FROM sys.tables t
INNER join sys.columns C ON c.object_id = t.object_id
WHERE c.name <> 'ID'
But above query return all table for which column ID is present in it.
Can u try this query,
SELECT * FROM sys.tables WHERE type = 'U' AND object_id NOT IN (
SELECT DISTINCT c.object_id FROM sys.columns c WHERE c.name = 'ID')
SELECT name
FROM sys.Tables
WHERE Object_ID NOT IN (SELECT Object_ID FROM sys.Columns WHERE name = 'ID')
You don't need to perform a join, you can just add a subquery that identifies tables that have an ID column and exclude the object_id's using NOT IN(..the subquery..):
SELECT DISTINCT t.name
FROM sys.tables t
WHERE t.object_id NOT IN (SELECT object_id FROM sys.columns WHERE name = 'id')
The reason your query doesn't work is because you are simply getting all columns that aren't = ID and each table has many columns that match that criteria so they will be returned even if there is an ID column.
One method is with NOT EXISTS:
SELECT OBJECT_SCHEMA_NAME(t.object_id) AS SchemaName
, t.name AS TableName
FROM sys.tables t
WHERE NOT EXISTS ( SELECT *
FROM sys.columns c
WHERE c.object_id = t.object_id
AND c.name = 'ID' );
Try the following, lists all table names NOT having ID.
SELECT tableName
FROM (
SELECT DISTINCT
t.name AS tableName
,c.name AS columnName
FROM sys.tables t
INNER JOIN sys.columns C
ON c.object_id = t.object_id
) AS t
WHERE columnName NOT IN( 'ID')
GROUP BY tableName
If you down't want to see system tables, then..
SELECT DISTINCT OBJECT_NAME(OBJECT_ID) TableName FROM SYS.COLUMNS WHERE NAME <> 'id'
and OBJECTPROPERTY (OBJECT_ID, 'IsUserTable') = 1
EXCEPT
SELECT DISTINCT OBJECT_NAME(OBJECT_ID) TableName FROM SYS.COLUMNS WHERE NAME = 'id'

Can we refer to a column from sysconstraints using name in sybase?

since the option of accessing via 'name' attribute is possible via
sysobjects
, is the same possible by
sysconstraints/syscolumns??
because i have not given id attribute to table or column or any constraints.
sysobjects does not contains any column name. I don't understand your first statement.
But if you have a table name and want to display sysconstraints table from table name and column name you can have a join like this :
SELECT constrid
FROM sysconstraints sc
JOIN sysobjects o ON o.id = sc.tableid
JOIN syscolumns c ON c.colid = sc.colid AND c.id = o.id
WHERE o.name = 'tablename'
AND c.name = 'columnname'
The sysconstraints has colid and tableid columns that match syscolumns.colid and sysobjects.id.

Resources