Monday, March 26, 2012

how to use variable to create index

Hi ,

I would like to create index for a table and that index name must be random generated.

How to do this?

declare @.value varchar(50)

set @.value = rand()

set @.value = @.value + 'index-name'

create index @.value on tablename(variables)

Here it is:

IF OBJECT_ID('T1') IS NOT NULL
DROP TABLE T1;

CREATE TABLE T1(
id int primary key,
[Name] sysname,
code sysname
)
GO

declare @.value varchar(50);
set @.value = rand()
set @.value = @.value + 'index-name'

declare @.cmd sysname;
SET @.cmd = 'CREATE INDEX [' + @.value + '] ON T1([Name])';

EXEC(@.cmd);

Thanks,
Zuomin

No comments:

Post a Comment