use OLE; use DBI; use DBD::ODBC; use Mail::Sender; my $trace_level=1; my $trace_file='tracefile.txt'; DBI->trace($trace_level,$trace_file); #### lnitialization of Global Variables & Arrays #### my $debug =0; my $t = time; (my $sec, my $min, my $hour, my $mday, my $mon, my $year, my $wday, my $yday, my $isdst)=localtime($t); $year = $year + "1900"; my $SQLServer = "servername"; my $DB = "dbname"; my $SQLUname = "username"; my $SQLpass="password"; my $field1="ClassSchedule.EventDate AS EventDate"; my $field2="DATEDIFF(Day,{fn NOW()},ClassSchedule.EventDate) AS Until,"; my $field3="DATEDIFF(Day,ClassRoster.Enrolled, ClassSchedule.EventDate) AS Since,"; my $field4="Classes.Duration AS Duration, Classes.Topic AS Topic, Classes.Description AS Description, ClassLocation.Location AS Location, ClassLocation.Address AS Address, ClassLocation.Directions AS Directions, ClassLocation.Map AS Map, ClassRoster.idEnrollee AS idEnrollee, ClassRoster.System AS System, ClassRoster.Account AS Account, ClassRoster.PublicID AS PublicID, ClassRoster.Name AS Name, ClassRoster.Email AS emailAddress"; my $field5="ClassSchedule"; my $field6="Classes ON ClassSchedule.id_Class = Classes.idClass"; my $field7="ClassRoster ON ClassSchedule.idEvent = ClassRoster.id_Event"; my $field8="ClassLocation ON ClassSchedule.id_Location = ClassLocation.idLocation"; my $field9condition="ClassSchedule.EventDate > { fn NOW() }) AND (ClassRoster.Cancelled IS NULL) AND (ClassRoster.Reminded IS NULL) AND (DATEDIFF(dd, ClassRoster.Enrolled, ClassSchedule.EventDate) > 7) AND (NOT (DATEPART(dw, { fn NOW() }) IN (1, 7))) AND (DATEDIFF(dd, { fn NOW() }, ClassSchedule.EventDate) < 7)"; # Connect to the database # See footnote my $dbh = DBI->connect("dbi:ODBC:driver=SQL Server;Server=VASVCSSQL;database=MLXhelp;uid=$SQLUname;pwd=$SQLpass;") or die "Couldn't open database: $DBI::errstr; stopped"; print "connected finally\n"; # Prepare the SQL query for execution my $sth=$dbh->prepare("SELECT $field1,$field2,$field3,$field4 from $field5 INNER JOIN $field6 INNER JOIN $field7 INNER JOIN $field8 WHERE $field9condition"); # Execute the query $sth->execute() or die "Couldn't execute statement: $DBI::errstr; stopped"; while (my ($field1)=$sth->fetchrow_array()) { print "Field 1: $field1 \n"; } $sth->finish(); $dbh->disconnect(); DBI 1.602-ithread default trace level set to 0x0/1 (pid 5692) at test3.pl line 12 -> DBI->connect(dbi:ODBC:driver=SQL Server;Server=VASVCSSQL;database=MLXhelp;uid=Frank;pwd=fu3go;, , ****) -> DBI->install_driver(ODBC) for MSWin32 perl=5.010000 pid=5692 ruid=0 euid=0 install_driver: DBD::ODBC version 1.15 loaded from C:/Perl/site/lib/DBD/ODBC.pm <- install_driver= DBI::dr=HASH(0x3376c8c) !! warn: 0 CLEARED by call to default_user method <- default_user(undef, undef, ...)= ( undef undef ) [2 items] at DBI.pm line 625 <- connect('driver=SQL Server;Server=VASVCSSQL;database=MLXhelp;uid=Frank;pwd=fu3go;', undef, ...)= DBI::db=HASH(0x33771ac) at DBI.pm line 637 info: '' '[Microsoft][ODBC SQL Server Driver][SQL Server]Changed database context to 'MLXhelp'. (SQL-01000) [Microsoft][ODBC SQL Server Driver][SQL Server]Changed language setting to us_english. (SQL-01000)' (err#0) <- STORE('PrintError', 1)= 1 at DBI.pm line 689 info: '' '[Microsoft][ODBC SQL Server Driver][SQL Server]Changed database context to 'MLXhelp'. (SQL-01000) [Microsoft][ODBC SQL Server Driver][SQL Server]Changed language setting to us_english. (SQL-01000)' (err#0) <- STORE('AutoCommit', 1)= 1 at DBI.pm line 689 info: '' '[Microsoft][ODBC SQL Server Driver][SQL Server]Changed database context to 'MLXhelp'. (SQL-01000) [Microsoft][ODBC SQL Server Driver][SQL Server]Changed language setting to us_english. (SQL-01000)' (err#0) <- STORE('Username', undef)= 1 at DBI.pm line 692 info: '' '[Microsoft][ODBC SQL Server Driver][SQL Server]Changed database context to 'MLXhelp'. (SQL-01000) [Microsoft][ODBC SQL Server Driver][SQL Server]Changed language setting to us_english. (SQL-01000)' (err#0) <> FETCH('Username')= undef ('Username' from cache) at DBI.pm line 692 info: '' '[Microsoft][ODBC SQL Server Driver][SQL Server]Changed database context to 'MLXhelp'. (SQL-01000) [Microsoft][ODBC SQL Server Driver][SQL Server]Changed language setting to us_english. (SQL-01000)' (err#0) #removed connectstring for security <- connected('dbi:ODBC:driver=SQL;')= undef at DBI.pm line 698 <- connect= DBI::db=HASH(0x33771ac) info: '' '[Microsoft][ODBC SQL Server Driver][SQL Server]Changed database context to 'MLXhelp'. (SQL-01000) [Microsoft][ODBC SQL Server Driver][SQL Server]Changed language setting to us_english. (SQL-01000)' (err#0) <- STORE('dbi_connect_closure', CODE(0x3376bdc))= 1 at DBI.pm line 707 !! info: '' CLEARED by call to prepare method <- prepare('SELECT ClassSchedule.EventDate AS EventDate,DATEDIFF(Day,{fn NOW()},ClassSchedule.EventDate) AS Until,,DATEDIFF(Day,ClassRoster.Enrolled, ClassSchedule.EventDate) AS Since,,Classes.Duration AS Duration, Classes.Topic AS Topic, Classes.Description AS Description, ClassLocation.Location AS Location, ClassLocation.Address AS Address, ClassLocation.Directions AS Directions, Cl...')= DBI::st=HASH(0x33773dc) at test3.pl line 55 !! ERROR: 1 '[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near ','. (SQL-42000) [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (SQL-42000)' (err#2) <- execute= undef at test3.pl line 57 ERROR: 1 '[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near ','. (SQL-42000) [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (SQL-42000)' (err#2) <- DESTROY(DBI::st=HASH(33772ec))= undef at test3.pl line 57 ERROR: 1 '[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near ','. (SQL-42000) [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (SQL-42000)' (err#0) <- DESTROY(DBI::db=HASH(337711c))= undef at test3.pl line 57 !! ERROR: 1 CLEARED by call to disconnect_all method <- disconnect_all= '' at DBI.pm line 715 ! <- DESTROY(DBI::dr=HASH(3376c8c))= undef during global destruction