bfdi533 has asked for the wisdom of the Perl Monks concerning the following question:
I have some code that I need to execute from withing SQL Server and it is written in TSQL. The short version is that there are several sql statements and are all being executed at one time, back-to-back, with one query and this works fine with Microsoft Query Analyzer but not with DBD.
For instance, if I have the following query (which is valid in Microsoft Query Analyzer), how would I run it through DBD:
use Northwind declare @d datetime declare @c int select @d = getdate() print @d print @d - 365*8 select @c = count(*) from Orders where ShippedDate > @d - 365*6 print @c
And my Perl code:
print "=== processing $server server ===\n"; my $DSN = "driver={SQL Server};Server=$server;database=master;uid=;pwd +=;"; my $dbh = DBI->connect("dbi:ODBC:$DSN") or die "$DBI::errstr\n"; $tsql = <<'END_QUERY'; use Northwind declare @d datetime declare @c int select @d = getdate() print @d print @d - 365*8 select @c = count(*) from Orders where ShippedDate > @d - 365*6 print @c END_QUERY # create a modified sp_helprotect that will output results in table ve +rsion $sth = $dbh->prepare ($tsql); $sth->execute(); $out = ""; while (@row = $sth->fetchrow_array()) { # this is line 44 print $server,':',join(':',@row),"\n"; } $sth->finish(); $dbh->disconnect();
Here is the result if I run it through Perl DBD:
C:\Data\Scripts\sql>tsql_test.pl === processing new-borg server === DBD::ODBC::st fetchrow_array failed: (DBD: no select statement current +ly executing err=-1) at C:\Data\Scripts\sql\tsql_test.pl line 44. C:\Data\Scripts\sql>
Ed
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBD::ODBC and TSQL
by mpeppler (Vicar) on Apr 24, 2004 at 06:38 UTC | |
|
Re: DBD::ODBC and TSQL
by mifflin (Curate) on Apr 23, 2004 at 21:12 UTC | |
|
Re: DBD::ODBC and TSQL
by Grygonos (Chaplain) on Apr 24, 2004 at 02:31 UTC | |
|
Re: DBD::ODBC and TSQL
by bilfurd (Hermit) on Apr 23, 2004 at 21:36 UTC |