Zha*_*g18 2 sql-server perl dbi
在 SQL Server Management Studio 2008 中,我可以运行
sp_columns MY_TABLE
Run Code Online (Sandbox Code Playgroud)
获取所有列名(在 下COLUMN_NAME)。但是,如何使用 Perl DBI 获取相同的信息?
特别是,我试过
my $result = $dbh->selectall_hashref("sp_columns MY_TABLE", 'COLUMN_NAME');
Run Code Online (Sandbox Code Playgroud)
希望列名是返回散列的键。
事实上,即使那是成功的,这也不是我想要的,因为我需要保留该表中列的顺序。所以我试过了
my $sth = $dbh->prepare("sp_columns $table");
$sth->execute();
while (my @row = $sth->fetchrow_array) {
# process @row;
}
Run Code Online (Sandbox Code Playgroud)
但两者都不起作用。我收到错误消息
DBD::Sybase::db selectall_hashref failed: Server message number=102 severity=15 state=1 line=1 server=XXXX text=Incorrect syntax near '.'
Run Code Online (Sandbox Code Playgroud)
我也参考了这篇文章。显然,下面的查询也不要在我Management Studio中的工作:
select * from information_schema.columns where table_name = MY_TABLE
Run Code Online (Sandbox Code Playgroud)
错误信息为
Msg 208, Level 16, State 1, Line 2
Invalid object name 'information_schema.columns'.
Run Code Online (Sandbox Code Playgroud)
请帮忙?谢谢!
返回的列名是准备好的语句的属性,因此您可以使用:
my $dbh = DBI->connect('dbi:DBMS:...','','');
my $sth = $dbh->prepare("SELECT * FROM SomeTable");
Run Code Online (Sandbox Code Playgroud)
现在$sth->{NAME}(数组引用)包含列的名称(如别名等)。