給sqlserver中的表創(chuàng)建索引
來(lái)源:易賢網(wǎng) 閱讀:2090 次 日期:2014-11-03 11:52:20
溫馨提示:易賢網(wǎng)小編為您整理了“給sqlserver中的表創(chuàng)建索引”,方便廣大網(wǎng)友查閱!

給sqlserver中的表創(chuàng)建索引

#!/usr/bin/perl

use DBI;

use Switch;

use Encode;

use Encode::CN;

# my $source_name = "zoe";

# my $source_user_name = "sa";

# my $source_user_psd = "123";

# my $db_name="mysqlDb";

# my $location="192.168.0.46";

# my $port="3306";

# my $db_user="zoe";

# my $db_pass="123";

my $source_name = "zoe";

my $source_user_name = "sa";

my $source_user_psd = "123";

my $dbh=DBI->connect("dbi:ODBC:$source_name",$source_user_name,$source_user_psd);

#獲取所有的用戶表

my $sth=$dbh->prepare("select name,object_id from sys.all_objects where type='U' and is_ms_shipped=0 and name <>'sysdiagrams'");

$sth->execute();

my $n=0;

my $ok=0;

my $sort_column="";

while (@data=$sth->fetchrow_array())

{

#print $data[0].$data[1];

$n+=1;

$ok=0;

#獲取列

get_columns($data[0],$data[1]);

if($ok ==1){

print '正在測(cè)試'.$data[0].'表的索引'.$sort_column."n";

my $sql_create="select * from sysindexes where id=object_id('$data[0]') and name='$sort_column'";

my $dbh_mssql=DBI->connect("dbi:ODBC:$source_name",$source_user_name,$source_user_psd,{RaiseError =>1});

$dbh_mssql->{LongTruncOk}=1;

$dbh_mssql->{LongReadLen}=1048576;

my $sth_select=$dbh_mssql->prepare($sql_create);

$sth_select->execute() or die 'Cannot execute: '. $sth_select->errstr();

my @select_col;

my $select_data;

while($select_data=$sth_select->fetchrow_arrayref())

{

$select_col[$nn]=[@$select_data];

}

my $col=@select_col;

if($col !=0)

{

print '表'.$data[0].'已存在索引'.$sort_column."n";

}

else

{

do_sql($data[0],$sort_column);

open(FILE,"》createtableallindex.txt");

syswrite(FILE,"$nn");

syswrite(FILE,"$data[0]n");

close(FILE);

}

}

}

$sth->finish;

$dbh ->disconnect;

print '所有表的索引創(chuàng)建結(jié)束'."n";

##獲取所有的列

sub get_columns

{

$dbh2=DBI->connect("dbi:ODBC:$source_name",$source_user_name,$source_user_psd);

my $sql="select col.name,tp.name,col.max_length,col.[precision],col.[scale],col.[is_nullable],col.[is_identity] from sys.all_columns col

inner join sys.types tp on col.system_type_id=tp.system_type_id and col.user_type_id=tp.user_type_id

where object_id=$_[1]";

my $cols=$dbh2 -> prepare($sql);

$cols->execute();

my $cols_str = "";

my $n=0;

$sort_column="";

while(@col= $cols->fetchrow_array())

{

($col_name,$type_name,$max_length,$precision,$scale,$is_nullable,$is_identity)=@col;

if($is_identity == 1)

{

$ok=1;

$sort_column="$col_name";

}

}

}

sub do_sql

{

print '開始創(chuàng)建'.$_[0].'表的索引'.$_[1]."n";

my $sql_create="CREATE UNIQUE INDEX $_[1] ON $_[0] ($_[1])";

my $dbh_mssql=DBI->connect("dbi:ODBC:$source_name",$source_user_name,$source_user_psd,{RaiseError =>1});

$dbh_mssql->{LongTruncOk}=1;

$dbh_mssql->{LongReadLen}=1048576;

my $sth_select=$dbh_mssql->prepare($sql_create);

# open(FILE,"》all_export_data222.txt");

# syswrite(FILE,"$sql_selectn");

# close(FILE);

$sth_select->execute() or die 'Cannot execute: '. $sth_select->errstr();

print '創(chuàng)建'.$_[0].'表的索引'.$_[1].'結(jié)束'."n";

}

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看數(shù)據(jù)庫(kù)
易賢網(wǎng)手機(jī)網(wǎng)站地址:給sqlserver中的表創(chuàng)建索引
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)