Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connect to SQL Server 1

Status
Not open for further replies.

bluegroper

Technical User
Dec 12, 2002
407
AU
Hi forum
Newbie here, so please be gentle.
I'm trying to connect to an SQL Server using Perl (of course).
Works fine IF my SQL database name is SINGLE word.
I'd like to also connect to database name which includes space.
But rather than rename the database, I'm sure there's a better way to write my perl.
Here's the sample code snippet. This borks because of space in 'Database Name'.

Code:
#!/usr/bin/perl -w
use strict;
use DBI;
my $server=qq|SQLSERVER|;
my $database=qq|'Database Name'|;
my $user="user";
my $pass="password";
my $dbh=DBI->connect("dbi:Sybase:server=$server", $user, $pass, {PrintError=>0, RaiseError=>1});
$dbh->do("use $database");
my $sth=$dbh->prepare( "SELECT * FROM Table1" );
my $result=$sth->execute();
while($result=$sth->fetchrow_hashref) {
    print "$result->{CODE}\t$result->{NAME}\n";
}
$sth->finish;
$dbh->disconnect;

TIA's for tips and clues

 
Hi

As far as I know, in SQL Server identifiers are quoted as in SQL-92 standard, with double quotes ( " ), so I would try one of these :
Perl:
[b]my[/b] [navy]$database[/navy][teal]=[/teal][b]qq[/b][green][i]|"Database Name"|[/i][/green][teal];[/teal]

[gray]# or[/gray]

[b]my[/b] [navy]$database[/navy][teal]=[/teal][b]q[/b][green][i]|"Database Name"|[/i][/green][teal];[/teal]

[gray]# or[/gray]

[b]my[/b] [navy]$database[/navy][teal]=[/teal][green][i]'"Database Name"'[/i][/green][teal];[/teal]


Feherke.
 
As feherke said, the double quotes ([blue]"DB Name"[/blue]) should work for you. You might also have luck with square brackets ([blue][DB Name][/blue]) but that could depend on your DBMS.
 
Thanks feherke for suggestions. Unfortunately, all those options throw errors, but your input is greatly appreciated.

Thanks rharsh for suggestion. Yo the man. It works.
BTW, I'm connecting to an an old SQL Server 2005 Express database.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top