Please see the DDL below:
CREATE TABLE Person
(
ID int identity not null,
Name VARCHAR(100),
Age int,
EyeColour varchar(20),
primary key (ID)
)
CREATE INDEX Name ON Person (Name)
CREATE INDEX Age ON Person (Age)
CREATE INDEX EyeColour ON Person (EyeColour)
I can execute the following statement:
ALTER TABLE person
ALTER COLUMN name VARCHAR(110)
However, I cannot execute the following statement:
ALTER TABLE person
ALTER COLUMN name VARCHAR(90)
The error is:
Msg 5074, Level 16, State 1, Line 1
The index 'Name' is dependent on column 'name'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN name failed because one or more objects access this column.
Why am I seeing these errors when I reduce the length of a VARCHAR. In what other scenarios would I see this error e.g. change a data type?
Is there an automated way of identifying all the indexes and constraints affected by a data type change and dealing with them?
You could definitely automate the identification part. Here is a script that will find all the indices and foreign keys depending on a table column (I have left finding check constraints as an exercise for the reader). As it is a script, you would have to change the parameters when you run it. You could also turn this into a stored procedure.
However, I don't recommend automating the actions such as dropping indices or constraints. You are better off reviewing the output and deciding if it is really OK to reduce the sizes of the columns or not in the light of what you know about the applications that use these data.
-- parameters
DECLARE
#nm_schema sysname = N'Purchasing',
#nm_table sysname = N'PurchaseOrderHeader',
#nm_column sysname = N'EmployeeID';
DECLARE
#id_table int,
#id_column smallint;
SELECT #id_table = o.object_id
FROM sys.objects o
JOIN sys.schemas s
ON o.schema_id = s.schema_id
WHERE o.name = #nm_table
AND s.name = #nm_schema
AND o.type = 'U';
IF ##ROWCOUNT = 0
BEGIN
RAISERROR(N'Schema %s table %s not found', 0, 1, #nm_schema, #nm_table);
RETURN;
END;
SELECT #id_column = column_id
FROM sys.columns
WHERE object_id = #id_table
AND name = #nm_column;
IF ##ROWCOUNT = 0
BEGIN
RAISERROR(N'Column %s not found in schema %s table %s', 0, 2, #nm_column, #nm_schema, #nm_table);
RETURN;
END;
SELECT 'Index' AS 'dependency', i.name
FROM sys.indexes i
JOIN sys.index_columns ic
ON i.object_id = ic.object_id
AND i.index_id = ic.index_id
WHERE i.object_id = #id_table
AND ic.column_id = #id_column
AND NOT i.type = 0 -- heap
UNION ALL
SELECT 'FKey', f.name
FROM sys.foreign_keys f
JOIN sys.foreign_key_columns fc
ON f.object_id = fc.constraint_object_id
WHERE (f.parent_object_id = #id_table AND fc.parent_column_id = #id_column)
OR (f.referenced_object_id = #id_table AND fc.referenced_column_id = #id_column);
Related
The following is my code
IF NOT EXISTS (SELECT 1 FROM sys.objects o INNER JOIN sys.columns c ON o.object_id = c.object_id
WHERE
o.name = 'portfolioAttributeCodes'
AND c.name = 'isDisplayed'
)
BEGIN
ALTER TABLE
[cosmos].[portfolioAttributeCodes]
ADD
[isDisplayed] bit DEFAULT 1;
END
IF EXISTS (SELECT 1 FROM sys.objects o INNER JOIN sys.columns c ON o.object_id = c.object_id
WHERE
o.name = 'portfolioAttributeCodes'
AND c.name = 'isDisplayed')
BEGIN
UPDATE [cosmos].[portfolioAttributeCodes] SET [isDisplayed] = 1;
END
Now what is happening is it will not create a column (skips the first Id statement and gets into the second one and fails with Invalid column name 'isDisplayed'
Can some one help?
If the table doesn't have the column isDisplayed already the entire batch will fail, as the parser will generate an Invalid column name error. This occurs before any of the SQL is run, so it's not that the second IF is being entered, none of the SQL is run at all. It's effectively a compilation error (like when you try to build you C# application and you have a reference to an object you haven't defined).
You cannot reference a new column in the same scope it was created. You would need to use 2 batches or put the reference to the column in a separate scope, so that its validation is deferred.
A deferred validation would seem fine here:
IF NOT EXISTS (SELECT 1
FROM sys.objects o
INNER JOIN sys.columns c ON o.object_id = c.object_id
WHERE o.name = 'portfolioAttributeCodes'
AND c.name = 'isDisplayed')
BEGIN
ALTER TABLE [cosmos].[portfolioAttributeCodes]
ADD [isDisplayed] bit CONSTRAINT DF_isDisplayed DEFAULT 1 WITH VALUES;
END;
ELSE
BEGIN
EXEC sys.sp_executesql N'UPDATE [cosmos].[portfolioAttributeCodes] SET [isDisplayed] = 1;';
END;
I also switch to an ELSE as there is little point updating the column after you've created it; just create the column with the values in the first place. I name the DEFAULT CONSTRAINT as well as that's just good habit.
I need to check the library used by several stored procs that extract data from a remote server.
I have (with SO help, see SO 21708681) built the below code:
DECLARE #tProcs TABLE
(
procID int IDENTITY,
procObjectID nvarchar(100),
procName nvarchar(100)
);
insert into #tProcs
SELECT object_id, name
FROM sys.objects
WHERE type in (N'P', N'PC') and name like '%_Extract'
declare #countProcs int, #I int=0
select #countProcs=COUNT(*) from #tProcs
while #I<#countProcs
Begin
declare #source_code nvarchar(max)
declare #objectID nvarchar(50)
declare #proc_Name nvarchar(200)
select #objectID=procObjectID from #tProcs where procID=#I
select #proc_Name=procName from #tProcs where procID=#I
select #source_code = definition
from sys.sql_modules
where object_id = #objectID
SELECT PATINDEX('BOCTEST.%', #proc_Name) as Pos, #proc_Name
-- or SELECT charindex(#source_code, '%BOCTEST%')
set #I=#I+1
End
Inside each of the target stored procs there is a line like this:
DECLARE YP040P_cursor CURSOR FOR SELECT * FROM BOCTEST.S653C36C.LIVEBOC_A.YP040P
I need to know for each of the stored procs the part 'LIVEBOC_A' (which can either be 'LIVEBOC_A' or LIVEBOC_B)
I tried to use PATINDEX and CHARINDEX to get the location of the start opf that string in the definition from sysmodules but all I get back is either zero or an error that string or binary data would be truncated.
try
SELECT
name,
table_name = CASE WHEN OBJECT_DEFINITION(OBJECT_ID) LIKE '%BOCTEST.S653C36C.LIVEBOC_A.YP040P%' THEN 'LIVEBOC_A'
WHEN OBJECT_DEFINITION(OBJECT_ID) LIKE '%BOCTEST.S653C36C.LIVEBOC_B.YP040P%' THEN 'LIVEBOC_B' END
FROM sys.objects o
WHERE o.[type] IN ('P', 'PC')
AND name like '%_Extract'
You can do what you want with a query like this one:
select name = s.name + '.' + p.name ,
dt_created = p.create_date ,
dt_modified = p.modify_date ,
livboc_usage = case
when m.definition like '%declare%cursor%boctext.[^.].LIVEBOC_A.%' then 'A'
when m.definition like '%declare%cursor%boctext.[^.].LIVEBOC_B.%' then 'B'
else null
end
from sys.schemas s
join sys.procedures p on p.schema_id = s.schema_id
join sys.sql_modules m on m.object_id = p.object_id
But since what you're looking for are cross-server dependencies of a table, you should be able to get that simply by querying the system view sys.sql_expression_dependencies, which
Contains one row for each by-name dependency on a user-defined entity in the current
database. A dependency between two entities is created when one entity, called the
referenced entity, appears by name in a persisted SQL expression of another entity,
called the referencing entity. For example, when a table is referenced in the definition
of a view, the view, as the referencing entity, depends on the table, the referenced
entity. If the table is dropped, the view is unusable.
You can use this catalog view to report dependency information for the following
entities:
Schema-bound entities.
Non-schema-bound entities.
Cross-database and cross-server entities.
Entity names are reported; however, entity IDs are not resolved.
Column-level dependencies on schema-bound entities.
Column-level dependencies for non-schema-bound objects can be returned
by using sys.dm_sql_referenced_entities.
Server-level DDL triggers when in the context of the master database.
To that end, running a query like this in the database the referencing stored procedures live should do you:
select name = o.name ,
type = o.type_desc ,
liveboc_usage = case d.referenced_schema_name
when 'liveboc_a' then 'A'
when 'liveboc_b' then 'B'
else null
end ,
has_dependency_on = d.referenced_server_name
+ '.' + d.referenced_database_name
+ '.' + d.referenced_schema_name
+ '.' + d.referenced_entity_name
from sys.sql_expression_dependencies d
join sys.objects o on o.object_id = d.referenced_id
join sys.schemas s on s.schema_id = o.schema_id
where d.referenced_server_name = 'BOCTEST'
and d.referenced_database_name = 'S653C36C'
and d.referenced_schema_name like 'LIVEBOC_[AB]'
and d.referenced_entity_name = 'YP040P'
I am using SQL server 2008. I need to find if default value constraint does not exist then create it. Here is what I have tried.
IF (NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME ='MyConstraint'))
BEGIN
ALTER TABLE [XX] ADD CONSTRAINT [MyConstraint] DEFAULT ((-1)) FOR [XXXX]
END
GO
if not exists (
select *
from sys.all_columns c
join sys.tables t on t.object_id = c.object_id
join sys.schemas s on s.schema_id = t.schema_id
join sys.default_constraints d on c.default_object_id = d.object_id
where t.name = 'table'
and c.name = 'column'
and s.name = 'schema')
....
I find this to be easier:
IF OBJECT_ID('SchemaName.MyConstraint', 'D') IS NULL
BEGIN
-- create it here
END
I was a bit puzzled as to why this simple task was so complicated. In my case, I don't have constraint names - only table and column names. I want to check if they already have a default before trying to add one.
After a bit more digging, I came up with this:
IF (SELECT Column_Default FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MY_TABLE' AND COLUMN_NAME = 'MY_COLUMN') is NULL
BEGIN
ALTER TABLE [dbo].[MY_TABLE]
ADD DEFAULT ('') FOR [MY_COLUMN]
END
GO
I have to implement this in a ginormous boilerplate script, so the shorter the better.
if not exists(select 1 from sys.default_constraints where name = 'SchemaName.MyConstraint')
begin
-- create it here
end
The following works for me on SQL Server 2016.
Assuming I have a table named MY_TABLE and a column MY_COLIUMN.
I would like to add a constrain (default to '-1' ) on MY_COLIUMN that need to add the constrain on.
/* Test for the specific column */
IF EXISTS (select 1 from sys.all_columns c where c.object_id= OBJECT_ID(N'MY_TABLE') and c.name='MY_COLIUMN')
BEGIN
/* Add default if not exits */
IF NOT EXISTS (
select 1 from sys.default_constraints c where c.object_id =
(
select default_object_id from sys.all_columns c where c.object_id = OBJECT_ID(N'MY_TABLE') and c.name='MY_COLIUMN'
)
)
BEGIN
ALTER TABLE MY_TABLE
ADD DEFAULT '-1' FOR MY_COLIUMN;
END
END
GO
I've used the following in the past:
DECLARE #default sysname
SELECT #default = object_name( cdefault ) FROM syscolumns WHERE id = object_id( 'DBO.TABLE' ) AND name = 'COLUMN'
IF ( not #default is null )
BEGIN
...
END
Search the system table where the default costraints of the database are stored, without the schema name:
IF EXISTS(SELECT 1 FROM sys.default_constraints WHERE [name] = 'MyConstraint')
print 'Costraint exists!';
ELSE
print 'Costraint doesn''t exist!';
I know I'm coming to the party late here, but I am a big fan of OBJECTPROPERTY. Here is how to set a default of 1 on a column if the default does not yet exist.
IF (OBJECTPROPERTY(OBJECT_ID('My_constraint_name'),'CnstIsColumn') IS NULL
ALTER TABLE Mytable ADD CONSTRAINT [MY_constraint_name] DEFAULT ((1)) FOR [My_column_name]
Using SQLServer :
I have a table user :
id
name
email
There are some other tables (about 200 more tables), some of which use user.id as foreign key on cascade delete.
So, I want to find out - Which tables use this foreign key (user.id) ?
I am accessing my sql-server with SQL Server Management Studio.
The way to get ONLY TABLE references (i.e. tables that uses given table as a foreign key and tables that given table uses the same way) you can use this code snippet:
declare #tableName varchar(64);
set #tableName = 'TABLE';
select
SO_P.name as [parent table]
,SC_P.name as [parent column]
,'is a foreign key of' as [direction]
,SO_R.name as [referenced table]
,SC_R.name as [referenced column]
,*
from sys.foreign_key_columns FKC
inner join sys.objects SO_P on SO_P.object_id = FKC.parent_object_id
inner join sys.columns SC_P on (SC_P.object_id = FKC.parent_object_id) AND (SC_P.column_id = FKC.parent_column_id)
inner join sys.objects SO_R on SO_R.object_id = FKC.referenced_object_id
inner join sys.columns SC_R on (SC_R.object_id = FKC.referenced_object_id) AND (SC_R.column_id = FKC.referenced_column_id)
where
((SO_P.name = #tableName) AND (SO_P.type = 'U'))
OR
((SO_R.name = #tableName) AND (SO_R.type = 'U'))
In SQL server management studio, you can right click your table in the object explorer, and then select 'View Dependencies'. This will open a new window in which you can see all other objects (not just tables) that depend on your table, and on which your table depends.
Here is a stored procedure I put together based in part on the above answer.
-- =============================================
-- Author: R. Mycroft
-- Create date: 2012-08-08
-- Description: Lists foreign keys to & from a named table.
-- (Have yet to find this one via Google!)
-- =============================================
alter procedure usp_ListTableForeignKeys
#tableName varchar(300) = ''
as
begin
set nocount on;
select
object_name(parent_object_id) as childObjectName
, object_name(referenced_object_id) as parentObjectName
, name, type_desc, create_date
from sys.foreign_keys
where object_name(parent_object_id) = #tableName
or object_name(referenced_object_id) = #tableName
end
Using SSMS GUI:
In SQL server management studio (SSMS), you can right click your table and select 'View Dependencies'. This will open a new window in which you can see all the objects that depend on your table, and on which your table depends also.
Additionally If you want to do it with TSQL in where all objects that depends on your table
Approach-1: Using sp_depends store procedure , though sql server team is going to remove this feauture in future version but it still useful to get all the dependencies on the specified Object, includes Tables, Views, Stored Procedures, Constraints, etc., sql server team recommend to use sys.dm_sql_referencing_entities and sys.dm_sql_referenced_entities instead.
-- Query to find Table Dependencies in SQL Server:
EXEC sp_depends #objname = N'dbo.aspnet_users' ;
Approach-2:
-- Query to find Table Dependencies in SQL Server:
SELECT referencing_id,
referencing_schema_name,
referencing_entity_name
FROM sys.dm_sql_referencing_entities('dbo.aspnet_users', 'OBJECT');
Approach-3: Find Table dependencies in Function, Procedure and View
SELECT *
FROM sys.sql_expression_dependencies A, sys.objects B
WHERE referenced_id = OBJECT_ID(N'dbo.aspnet_users') AND
A.referencing_id = B.object_id
Approach-4:
-- Value 131527 shows objects that are dependent on the specified object
EXEC sp_MSdependencies N'dbo.aspnet_users', null, 1315327
If you want to get all objects on which your table depends on.
-- Value 1053183 shows objects that the specified object is dependent on
EXEC sp_MSdependencies N'dbo.aspnet_users', null, 1053183
If you've got these defined as foreign keys then just examine the table design and look at the Relationships dialog which will show you everything that's defined for the table.
Alternatively you can use "View Dependencies".
Try this
select
OBJECT_NAME(parent_object_id) as parent_object_name,
*
from sys.foreign_keys
where name = 'YourFKName'
Another option to get foreign keys.
-- CTE to fetch all primary key information.
WITH PrimaryKeys AS (
SELECT
s.name as [Schema],
t.name as [Table],
c.name as [Column],
ic.index_column_id AS [ColumnNumber]
FROM sys.index_columns ic
JOIN sys.columns c ON ic.object_id = c.object_id and ic.column_id = c.column_id
JOIN sys.indexes i ON ic.object_id = i.object_id and ic.index_id = i.index_id
JOIN sys.tables t ON i.object_id = t.object_id
JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE i.is_primary_key = 1
),
-- CTE to fetch table information.
TableInfo AS (
SELECT
tab.name AS [Table],
col.name AS [Column],
sch.name AS [Schema],
tab.object_id AS TableId,
col.column_id AS ColumnId
FROM sys.tables tab
JOIN sys.schemas sch ON tab.schema_id = sch.schema_id
JOIN sys.columns col ON col.object_id = tab.object_id
)
-- Primary query selecting foreign keys and primary/dependent information.
SELECT
obj.name AS FK_NAME,
p.[Schema] AS [PrimarySchema],
p.[Table] AS [PrimaryTable],
p.[Column] AS [PrimaryColumn],
d.[Schema] AS [DependentSchema],
d.[Table] AS [DependentTable],
d.[Column] AS [DependentColumn],
prim.ColumnNumber AS IsDependentPrimaryColumn -- has value if is part of dependent table's primary key
FROM sys.foreign_key_columns fkc
JOIN sys.objects obj ON obj.object_id = fkc.constraint_object_id
JOIN TableInfo d ON d.TableId = fkc.parent_object_id AND d.ColumnId = fkc.parent_column_id
JOIN TableInfo p ON p.TableId = fkc.referenced_object_id AND p.ColumnId = fkc.referenced_column_id
-- Join in primary key information to determine if the dependent key is also
-- part of the dependent table's primary key.
LEFT JOIN PrimaryKeys prim ON prim.[Column] = d.[Column] AND prim.[Table] = d.[Table]
ORDER BY [PrimarySchema], [PrimaryTable], [DependentSchema], [DependentTable]
This will yield all foreign keys and their primary/dependent information. It also includes an extra column if the dependent column is part of the primary key in the dependent table - sometimes important to note that.
To get only the Users table, just add a WHERE clause before the final ORDER BY
WHERE PrimaryTable = 'Users'
Option to get all tables with Schema Names
select
SO_P.name as [parent table]
,SS_P.name as [parent table schema]
,SC_P.name as [parent column]
,'is a foreign key of' as [direction]
,SO_R.name as [referenced table]
,SS_R.name as [referenced table schema]
,SC_R.name as [referenced column]
,*
from sys.foreign_key_columns FKC
inner join sys.objects SO_P on SO_P.object_id = FKC.parent_object_id
inner join sys.schemas SS_P on SS_P.schema_id = SO_P.schema_id
inner join sys.columns SC_P on (SC_P.object_id = FKC.parent_object_id) AND (SC_P.column_id = FKC.parent_column_id)
inner join sys.objects SO_R on SO_R.object_id = FKC.referenced_object_id
inner join sys.schemas SS_R on SS_R.schema_id = SO_P.schema_id
inner join sys.columns SC_R on (SC_R.object_id = FKC.referenced_object_id) AND (SC_R.column_id = FKC.referenced_column_id)
where SO_P.type = 'U' OR SO_R.type = 'U'
How can I find the create date of an index. I am using SQL2008 R2.
I checked sys.indexes but it does not have a create date so I joined the query with sys.objects. The thing is that the object id for an index and the table containing that index is same.
I am using this query...
select i.name, i.object_id, o.create_date, o.object_id, o.name
from sys.indexes i
join sys.objects o on i.object_id=o.object_id
where i.name = 'Index_Name'
Thanks!
For indexes that are constraints, then see marc_s' answer
For other indexes, you'd have to use STATS_DATE to get the creation time of the associated index (every index has statistics on it)
Something like (not tested)
SELECT STATS_DATE(OBJECT_ID('MyTable'),
(SELECT index_id FROM sys.indexes WHERE name = 'Index_Name'))
This relies on the sys.indexes to sys.stats links
Edit: there is no way to find out as far as anyone can find out. Sorry.
Simple query to list indexes in descending date (of statistics) order.
This date is the sate of last statistics update, so is only reliable for recently created indexes.
select STATS_DATE(so.object_id, index_id) StatsDate
, si.name IndexName
, schema_name(so.schema_id) + N'.' + so.Name TableName
, so.object_id, si.index_id
from sys.indexes si
inner join sys.tables so on so.object_id = si.object_id
order by 1 desc
This is now quite a long dead thread but the below query from SQLPanda got me the info I needed on Azure SQL for a non clustered index:
SELECT OBJECT_NAME(i.object_id) AS TableName, i.object_id, i.name, i.type_desc,o.create_date, o.modify_date,o.type,i.is_disabled
FROM sys.indexes i
INNER JOIN sys.objects o ON i.object_id = o.object_id
WHERE o.type NOT IN ('S', 'IT')
AND o.is_ms_shipped = 0
AND i.name IS NOT NULL
ORDER BY modify_date DESC
Credit to http://www.sqlpanda.com/2013/10/how-to-check-index-creation-date.html
I just added the modified date since that was the info I was interested in.
Try this:
SELECT
i.name 'Index Name',
o.create_date
FROM
sys.indexes i
INNER JOIN
sys.objects o ON i.name = o.name
WHERE
o.is_ms_shipped = 0
AND o.type IN ('PK', 'FK', 'UQ')
The object_id refers to the table the index is created on....
When PK or UK is created, SQL Server automatically creates unique index for that constraints. The create_date of those constraints will be the same as the create date for the corresponding indexes.
Since the sys.indexes view does not have create_date column it is absolutely useless for searching this kind of information. Furthermore, object_id column in this view will never refer to the corresponding constraint. It will point to the table the index belongs to.
The following test will demonstrate the point:
CREATE TABLE dbo.TEST_T1
(
COLUMN_1 INT NOT NULL,
COLUMN_2 INT NOT NULL,
CONSTRAINT PK_TEST_T1 PRIMARY KEY (COLUMN_1)
)
GO
WAITFOR DELAY '00:00:01';
ALTER TABLE dbo.TEST_T1
ADD CONSTRAINT UK_TEST_T1 UNIQUE (COLUMN_2)
GO
SELECT O.name, O.object_id, O.create_date, I.object_id, I.name AS index_name
FROM sys.objects AS O
LEFT OUTER JOIN sys.indexes AS I ON O.object_id = i.object_id
WHERE O.name IN ('TEST_T1', 'PK_TEST_T1', 'UK_TEST_T1')
The result is:
name object_id create_date object_id index_name
PK_TEST_T1 272720024 2015-03-17 11:02:47.197 NULL NULL
TEST_T1 256719967 2015-03-17 11:02:47.190 256719967 PK_TEST_T1
TEST_T1 256719967 2015-03-17 11:02:47.190 256719967 UK_TEST_T1
UK_TEST_T1 288720081 2015-03-17 11:02:48.207 NULL NULL
So, if you want to see create_date for PK or UK indexes there is no need to join with sys.indexes. You should select from sys.objects:
SELECT name, object_id, create_date
FROM sys.objects
WHERE name IN ('PK_TEST_T1', 'UK_TEST_T1')
AND type IN ('PK', 'UQ')
The result is:
name object_id create_date
PK_TEST_T1 272720024 2015-03-17 11:02:47.197
UK_TEST_T1 288720081 2015-03-17 11:02:48.207
USE [YourDB Name]
SET NOCOUNT ON
DECLARE #Table_Name varchar(200)
DECLARE #Index_Name varchar(200)
DECLARE #Index_Type varchar(50)
DECLARE Indx_Cursor CURSOR
STATIC FOR
select s_tab.name as Table_Name,
s_indx.name as Index_Name,
s_indx.type_desc as Index_Type
from sys.indexes s_indx
inner join sys.tables s_tab
on s_tab.object_id=s_indx.object_id
where s_indx.name is not null;
OPEN Indx_Cursor
IF ##CURSOR_ROWS > 0
BEGIN
FETCH NEXT FROM Indx_Cursor INTO #Table_Name,#Index_Name,#Index_Type
WHILE ##Fetch_status = 0
BEGIN
INSERT INTO INDEX_HISTORY(table_name,index_name,Index_Type,Created_date)
SELECT #Table_Name,#Index_Name,#Index_Type,
STATS_DATE(OBJECT_ID(#Table_Name),
(SELECT index_id FROM sys.indexes
WHERE name = #Index_Name))as Index_create_Date
FETCH NEXT
FROM Indx_Cursor
INTO #Table_Name,#Index_Name,#Index_Type
END
END
CLOSE Indx_Cursor
DEALLOCATE Indx_Cursor
select distinct * from index_history
But the main problem with indexes is that when we rebuild or reorganize indexes then the index creation date gets changed to the date when the index was last rebuilt or reorganized.
select
crdate, i.name, object_name(o.id)
from
sysindexes i
join
sysobjects o ON o.id = i.id
where
i.name = 'My_Index_Name'