Imagine the following schema:
create table tempdb..t1 (id int, name sysname);
create table tempdb..t2 (id int, name sysname);
create index IX1 on tempdb..t1 (id);
create index IX2 on tempdb..t2 (id);
Now I'm trying to script index definition:
declare @stmt nvarchar(max) = '';
select @stmt += 'create index ' + ix.name + ' on ' + t.name
+ isnull(' where ' + ix.filter_definition, '') + char(13)
from tempdb.sys.tables t
join tempdb.sys.indexes ix on t.object_id = ix.object_id
where ix.type > 0 and t.name in ('t1','t2')
order by ix.name;
print @stmt;
I'm expecting to get two index definitions:
create index IX1 on t1
create index IX2 on t2
but get only second. If I remove order by
or isnull
part, or add top
statement, I get both definitions.
Am I missing something obvious?