程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> 更多數據庫知識 >> 把sqlserver中的存儲過程注釋後創建到mysql中

把sqlserver中的存儲過程注釋後創建到mysql中

編輯:更多數據庫知識

   把sqlserver中的存儲過程注釋後創建到mysql中

  #!/usr/bin/perl

  use DBI;

  use Switch;

  use Encode;

  use Encode::CN;

  my $source_name = "sqldb";

  my $source_user_name = "sa";

  my $source_user_psd = "123";

  my $db_name="mysqldb";

  my $location="192.168.0.208";

  my $port="3306";

  my $db_user="zoe";

  my $db_pass="123";

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

  #獲取所有的用戶表

  my $sth=$dbh->prepare("select name from sys.objects where type='P'");

  $sth->execute();

  my $n=0;

  my $ok=0;

  my $sort_column="";

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

  {

  #print $data[0];

  print '正在查詢表'.$data[0]."的存儲過程\n";

  my $sql_create="EXEC Sp_HelpText '$data[0]'";

  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_array())

  {

  $select_col.=$select_data;

  }

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

  }

  $sth->finish;

  $dbh ->disconnect;

  print '所有表的存儲過程創建結束'."\n";

  sub do_sql

  {

  print '開始創建'.$_[0].'表的存儲過程'."\n";

  my $sql=$_[1];

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

  syswrite(FILE,"$n\n");

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

  syswrite(FILE,"$sql\n");

  close(FILE);

  $sql=~s/\/\*/------注釋開始--------/g;

  $sql=~s/\*\//------注釋結束--------/g;

  $sql='CREATE PROCEDURE '.$_[0].'()'." \n".' BEGIN '."\n".' /* '.$sql.'*/'."\n".'END; ';

  my $data_base = "DBI:mysql:$db_name:$location:$port";

  my $dbh3=DBI -> connect($data_base,$db_user,$db_pass);

  $dbh3->do("SET character_set_client = 'utf8'");

  $dbh3->do("SET character_set_connection = 'utf8'");

  my $data_str=encode("utf-8",decode("gbk",$sql));

  my $sth=$dbh3->prepare($sql);

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

  syswrite(FILE,"$n\n");

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

  syswrite(FILE,"$sql\n");

  close(FILE);

  $sth->execute() or die "$data_str----ERROR::$data_str::$dbh3->errstr";

  $dbh3->disconnect;

  print '創建'.$_[0].'表的存儲過程結束'."\n";

  }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved