I am searching for a query that tell me the NAME of the primary-key of a table
example: In oracle I do this
select CONSTRAINT_NAME from user_constraints where table_name = 'CT' AND CONSTRAINT_TYPE ='P'
how to do that in sybase ?
Try this way:
select name
from sysindexes
where indid > 0
and status2 & 2 = 2
Related
Can you please help me with a SQL Query , I want to find all tables name ,column names and total number of rows count of each columns for all tables in all databases. Is there a query that can do that for me. Please refer the below example
I want the below result.
TableName ColumnName TotalRowCount
TABLEA A 10
TABLEA B 20
TABLEA C 17
TABLEA D 20
TABLEB X 60
TABLEB Y 45
TABLEB Z 67
Regards
I have tried this....
SELECT
QUOTENAME(SCHEMA_NAME(sOBJ.schema_id)) + '.' + QUOTENAME(sOBJ.name) AS [TableName]
, SUM(sPTN.Rows) AS [RowCount]
FROM
sys.objects AS sOBJ
INNER JOIN sys.partitions AS sPTN
ON sOBJ.object_id = sPTN.object_id
WHERE
sOBJ.type = 'U'
AND sOBJ.is_ms_shipped = 0x0
AND index_id < 2 -- 0:Heap, 1:Clustered
GROUP BY
sOBJ.schema_id
, sOBJ.name
ORDER BY [TableName]
I am trying to get a record count in my output... but struggling with the syntax. Can't see what I'm doing wrong.... help please
SELECT
InfoSchema.table_catalog + '.' +
InfoSchema.Table_Schema + '.' +
InfoSchema.Table_Name as PhysicalName,
CONVERT(char(10), SysObj.crdate,126) as Created,
SysInx.rows Records
FROM
INFORMATION_SCHEMA.TABLES AS InfoSchema left join
SYSOBJECTS AS SysObj ON InfoSchema.TABLE_NAME = SysObj.name
WHERE
TABLE_TYPE = 'BASE TABLE'
AND SysObj.crdate is not null
AND SysObj.id = (
SELECT MIN(SysInx.id), rows
FROM SYSINDEXES AS SysInx
WHERE SysInx.id = SysObj.id
)
The desired output would be this:
PhysicalName Created RowCount
--------------+------------+-------------
tableName1 2020-01-01 127653
tableName2 2018-01-01 234098
tableName3 2019-01-01 0
Use a subquery in the result column.
SELECT
...
(
SELECT MAX(rows)
FROM SYSINDEXES AS SysInx
WHERE SysInx.id = SysObj.id
) AS Records
FROM
...
WHERE
...
AND SysObj.id IN (
SELECT SysInx.id
FROM SYSINDEXES AS SysInx
)
A colleague asked me to help them to identify the views in a database that have one or more computed columns. The database has hundreds of views so they're trying to find an automated way to accomplish this task. I am not seeing the results in the database that I was expecting. Here is an example:
--DROP TABLE dbo.Products
CREATE TABLE dbo.Products
(
ProductID int IDENTITY (1,1) NOT NULL
, QtyAvailable smallint
, UnitPrice money
);
--DROP VIEW dbo.uvw_Products
CREATE VIEW dbo.uvw_Products
AS
SELECT ProductID
, QtyAvailable
, UnitPrice
, (QtyAvailable * UnitPrice) AS InventoryValue
FROM dbo.Products;
-- Look at the view and find the computed column
SELECT OBJECT_SCHEMA_NAME(T.[object_id],DB_ID()) AS [Schema],
T.[name] AS [table_name], AC.[name] AS [column_name],
TY.[name] AS system_data_type, AC.[max_length],
AC.[precision], AC.[scale], AC.[is_nullable], AC.[is_ansi_padded], AC.[is_computed]
FROM sys.[views] AS T
INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id]
INNER JOIN sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id] AND AC.[user_type_id] = TY.[user_type_id]
WHERE T.[is_ms_shipped] = 0
AND T.[name] = 'uvw_Products'
ORDER BY T.[name], AC.[column_id]
-- Pulls up no results - no entries in sys.computed_columns
SELECT TOP 10 *
FROM sys.computed_columns C
INNER JOIN sys.views V ON C.[object_id] = V.[object_id]
WHERE V.[name] = 'uvw_Products'
As you can see from this simple example, SQL Server does not seem to be storing the value in the is_computed column.
What am I missing? How can we find the computed columns in views?
I know it's not ideal, I don't think a fool-proof solution exists for this problem, but depending on your naming conventions you could do a join to the tables sys view on column names so see which columns exist and don't exist in there.
for example:
SELECT v.*
FROM
(
SELECT [Schema] = OBJECT_SCHEMA_NAME(t.[object_id], DB_ID())
, [table_name] = t.[name]
, [column_name] = vc.[name]
FROM sys.[views] t
JOIN sys.[all_columns] vc ON t.[object_id] = vc.[object_id]
) v
LEFT JOIN
(
SELECT [Schema] = OBJECT_SCHEMA_NAME(t.[object_id], DB_ID())
, [table_name] = t.[name]
, [column_name] = vc.[name]
FROM sys.[tables] t
JOIN sys.[all_columns] vc ON t.[object_id] = vc.[object_id]
) t
ON t.[column_name] = v.[column_name]
WHERE t.[table_name] IS NULL
Returns:
Schema | table_name | column_name
----------------------------------------
dbo | uvw_Products | InventoryValue
And if your views contain the source table in its name like 'uvw_Products' you could also use that in your join to avoid columns in other tables getting in the way.
Again its not ideal but a relatively simple solution to narrow the search
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.
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')