newbieperlperson has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl geniuses,

Need a sanity check please. The following sql "select DATEADD(day, DATEDIFF(day, 2, GETDATE()), '20:00:00') as ADATE" runs correctly in Microsoft SQL query analyzer..

However in perl on a Win7 64bit environment, it complains with the following error "Description: MicrosoftODBC SQL Server Driver Cursor type changed".

My code can runs select and update statements against tables with no issues so I am a bit stumbled about the cursor error.

Note that I have not included my connection string but have illustrated that I am using Win32::OLE.

use Win32::OLE; $conn->{ConnectionString} = "..."; $conn->open; # open connection to the DB $state = $conn ->state; #1 means connected if($state ne "1"){... $mssql_select = "select DATEADD(day, DATEDIFF(day, 2, GETDATE()), '20 +:00:00') as ADATE"; $rs->Open( $mssql_select, $conn); my $error_collection = $conn->Errors(); my $ecount = $error_collection->Count; my ($is_message, $real_error_found); foreach my $error (in $error_collection) { #output error statements $is_message = ($error->{SQLState} eq "01000" && $error->{NativeErr +or}==0); $real_error_found=1 unless $is_message; $status = "ERROR # " . $error->{Number} . "\n Description: " . $error->{Description} . "\nSource: " . $error->{Source} . "\n"; }

Results in "Description: Microsoft ODBC SQL Server Driver Cursor type changed". Any ideas the group can provide would be appreciated.

Thank You,

AJ

  • Comment on Perl OLE32 MSSQL SELECT dateadd function results in “Cursor type changed” error
  • Download Code

Replies are listed 'Best First'.
Re: Perl OLE32 MSSQL SELECT dateadd function results in “Cursor type changed” error
by dasgar (Priest) on Dec 09, 2013 at 06:24 UTC

    If you look at the Description section of Win32::OLE's documentation, the first two sentences are: "This module provides an interface to OLE Automation from Perl. OLE Automation brings VisualBasic like scripting capabilities and offers powerful extensibility and the ability to control many Win32 applications from Perl scripts."

    Basically, that means you're not going to be able to run queries directly against a database using that module. Instead, it would allow you to control Windows applications (such as Microsoft Office products). If you want to run queries directly against a database, you might want to look into Win32::ODBC and/or DBD::ODBC.

    Although it sounds like you're not allowed to install other modules, that may not be an issue. Here's info on 3 popular Perl distributions for Windows:

    Also, I think there's more to your code than what you posted. In your posted code, it looks like $conn and $rs are objects, but those objects are not defined or created. And your posted code has an unfinished if statement. When I try running your code (with the if statement commented out), it bails on the $conn->open; line with the following error message: Can't call method "open" on unblessed reference at test.pl line 3.

    In other words, your posted code doesn't reproduce the error messages that you say that you're seeing. Perhaps if you can share more information on your limitations and what you're trying to do or maybe some reduced code that can reproduce your error message, others might be able to help you out more.

      Hi there,

      Thank you for taking the time to look into this.

      I have been using the WIN32:OLE to run select, update SQL statements against a mssql DB with no issues at all.

      Here is a different CPAN where it shows the ADO object under WIN32:OLE is used to execute SQL

      http://search.cpan.org/dist/Win32-OLE/lib/Win32/OLE/TPJ.pod

      For some reason it is not working for this one statement which is the odd thing.

      I will be in the office tmw and will update the code to include the connection string. Thanks,

      AJ

        I recommended you take a look at DBD::ADO, it does all that Win32::OLE ADO stuff for you in familiar DBI style ... so you don't have to suffer the maze of OLE :)
        #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd pp /; use DBI(); Main( @ARGV ); exit( 0 ); sub Main { my $att = {qw/RaiseError 1/}; my $user = my $pass = undef; my $dbh = DBI->connect("dbi:ADO:$dsn", $usr, $pwd, $att ) ; $dbh->trace(3); my $sth = $dbh->prepare( $mssql_select ); dd( $sth->execute ); dd( $sth->fetchall_arrayref ); }

        See also Tutorials->Database Programming->DBI recipes

      Basically, that means you're not going to be able to run queries directly against a database using that module. Instead, it would allow you to control Windows applications ... I don't know much about Win32 and OLE

      Sorry dasgar, but on previous occasion you've said (or demonstrated) you don't win32 much, so if you know you don't know, resist the temptation please

Re: Perl OLE32 MSSQL SELECT dateadd function results in “Cursor type changed” error
by Anonymous Monk on Dec 09, 2013 at 02:30 UTC
    Well, that isn't specific enough ... and I don't have MSSQL to try, so you try DBD::ADO or DBD::ODBC and increase trace level to get a better error message?
      Hi there, thanks for responding, unfortunately I am not able to use DBD and need to use OLE32, I would prefer to use DBD but that is not an option in our environment. AJ
Re: Perl OLE32 MSSQL SELECT dateadd function results in “Cursor type changed” error
by newbieperlperson (Acolyte) on Dec 09, 2013 at 23:51 UTC
    I figured it out, turns out this is just a warning as driver does not know what type of cursor will be returned, the results actually got returned. See http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e15141e7-3084-487d-a60f-47afac046a55/odbcsql-cursor-error