what are indexes in database tables code example
Example 1: search for value in all tables sql
DECLARE @SearchStr nvarchar(100)
SET @SearchStr = '## YOUR STRING HERE ##'
CREATE TABLE
SET NOCOUNT ON
DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
SET @TableName = ''
SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')
WHILE @TableName IS NOT NULL
BEGIN
SET @ColumnName = ''
SET @TableName =
(
SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
AND OBJECTPROPERTY(
OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
), 'IsMSShipped'
) = 0
)
WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
BEGIN
SET @ColumnName =
(
SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
AND TABLE_NAME = PARSENAME(@TableName, 1)
AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
AND QUOTENAME(COLUMN_NAME) > @ColumnName
)
IF @ColumnName IS NOT NULL
BEGIN
INSERT INTO
EXEC
(
'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + ' (NOLOCK) ' +
' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
)
END
END
END
SELECT ColumnName, ColumnValue FROM
DROP TABLE
Example 2: sql get list of domains and the tables that use them
DECLARE @DOMAIN_NAME NVARCHAR(MAX);
SET @DOMAIN_NAME = 'Material';
DECLARE @CLASS_DEFS TABLE
(
Name nvarchar(max),
Definition XML
)
INSERT INTO @CLASS_DEFS
SELECT
sde.gdb_items.Name,
sde.gdb_items.Definition
FROM
((SELECT GDB_ITEMS.UUID AS UUID
FROM sde.gdb_items INNER JOIN sde.gdb_itemtypes
ON sde.gdb_items.Type = sde.gdb_itemtypes.UUID
WHERE
sde.gdb_items.Name = @DOMAIN_NAME AND
sde.gdb_itemtypes.Name IN ('Coded Value Domain','Range Domain')) AS Domain
INNER JOIN sde.gdb_itemrelationships
ON Domain.UUID = sde.gdb_itemrelationships.DestID)
INNER JOIN sde.gdb_items
ON Domain.UUID = sde.gdb_itemrelationships.DestID
SELECT
ClassDefs.Name AS "Class Name",
fieldDef.value('Name[1]', 'nvarchar(max)') AS "Field Name",
NULL AS "Subtype Name"
FROM
@CLASS_DEFS AS ClassDefs
CROSS APPLY
Definition.nodes('/*/GPFieldInfoExs/GPFieldInfoEx') AS FieldDefs(fieldDef)
WHERE
fieldDef.value('DomainName[1]', 'nvarchar(max)') = @DOMAIN_NAME
UNION
SELECT
ClassDefs.Name AS "Class Name",
fieldDef.value('FieldName[1]', 'nvarchar(max)') AS "Field Name",
fieldDef.value('(../../SubtypeName)[1]', 'nvarchar(max)') AS "Subtype Name"
FROM
@CLASS_DEFS AS ClassDefs
CROSS APPLY
Definition.nodes('/*/Subtypes/Subtype/FieldInfos/SubtypeFieldInfo') AS FieldDefs(fieldDef)
WHERE
fieldDef.value('DomainName[1]', 'nvarchar(max)') = @DOMAIN_NAME