×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Connect to SQL Server

Connect to SQL Server

Connect to SQL Server

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

 

RE: Connect to SQL Server

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 :

CODE --> Perl

my $database=qq|"Database Name"|;

# or

my $database=q|"Database Name"|;

# or

my $database='"Database Name"';
 

Feherke.
http://feherke.github.com/

RE: Connect to SQL Server

As feherke said, the double quotes ("DB Name") should work for you. You might also have luck with square brackets ([DB Name]) but that could depend on your DBMS.

RE: Connect to SQL Server

(OP)
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.

 

RE: Connect to SQL Server

(OP)
[DB Name] is the solution.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close