Jump to content

Query Not displaying


 Share

Recommended Posts

Hi everyone,

I am trying to make a script that runs a query and show it to me to see if everything is right and then decide if I finish it or not so I made a little script as below :

#include <ADO.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>

_ADO_EVENTS_SetUp(True)

_ADO_ComErrorHandler_UserFunction(_ADO_COMErrorHandler)

Local $sDriver = 'SQL Server'
Local $sDatabase = 'DataBase' ; change this string to YourDatabaseName
Local $sServer = 'Localhost' ; change this string to YourServerLocation

Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & ';PWD=' & ';'

;~ Global $Query = _
;~ "BEGIN TRAN" & @CRLF & _
;~ "UPDATE Table" & @CRLF & _
;~ "SET HOUR = 4" & @CRLF & _
;~ "WHERE CUST = 'TEST'" & @CRLF & _
;~ "SELECT * FROM Table" & @CRLF & _
;~ "WHERE CUST = 'TEST'" & @CRLF & _
;~ "ROLLBACK TRAN"

Global $Query = _
"BEGIN TRAN" & @CRLF & _
"SELECT * FROM Table" & @CRLF & _
"WHERE CUST = 'TEST'" & @CRLF & _
"ROLLBACK TRAN"


_Query_Display($sConnectionString, $Query)


Func _Query_Display($sConnectionString, $sQUERY)

; Create connection object
Local $oConnection = _ADO_Connection_Create()

; Open connection with $sConnectionString
_ADO_Connection_OpenConString($oConnection, $sConnectionString)
If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)

; Executing some query directly to Array of Arrays (instead to $oRecordset)
Local $aRecordset = _ADO_Execute($oConnection, $sQUERY, True)

; Clean Up
_ADO_Connection_Close($oConnection)
$oConnection = Null

; Display Array Content with column names as headers
_ADO_Recordset_Display($aRecordset, 'Query Result')

EndFunc   ;==> _Query_Display

When I ran this script it works great, but when I run the query below :

Global $Query = _
"BEGIN TRAN" & @CRLF & _
"UPDATE Table" & @CRLF & _
"SET HOUR = 4" & @CRLF & _
"WHERE CUST = 'TEST'" & @CRLF & _
"SELECT * FROM Table" & @CRLF & _
"WHERE CUST = 'TEST'" & @CRLF & _
"ROLLBACK TRAN"

It doesn't show anything, when I take those begin and rollback it does what it should but still not showing me anything at all, is there a way around it that you know of?

Thank you.

Link to comment
Share on other sites

1- Is there another way? please explain.

2- I am trying to see the changes happening first, and then decide if I want to roll with them or not on a later step.

3- I did no errors at all.

Link to comment
Share on other sites

It doesn't show anything even with just the code below:

Global $Query = _
"UPDATE Table" & @CRLF & _
"SET HOUR = 4" & @CRLF & _
"WHERE CUST = 'TEST'" & @CRLF & _
"SELECT * FROM Table" & @CRLF & _
"WHERE CUST = 'TEST'"

Yes the changes were made but nothing was displayed.

Edited by Nas
Link to comment
Share on other sites

I don't use MS engine(s) but standard SQL requires a semicolumn at the end of each statement.

Otherwise, execute your statements separately.

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Try:

BEGIN

UPDATE

SELECT

(here decide what to do, commit or rollback)

ALL in their own query.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

$QUERY = _
"BEGIN TRANSACTION"

_Query_Display($sConnectionString, $QUERY)

$QUERY = _
"UPDATE AdventureWorks2016.HumanResources.Department" & @CRLF & _
"SET GroupName = 'Research % Development'" & @CRLF & _
"WHERE Name = 'Engineering'"

_Query_Display($sConnectionString, $QUERY)

$QUERY = _
"SELECT * FROM AdventureWorks2016.HumanResources.Department" & @CRLF & _
"WHERE Name = 'Engineering'"

_Query_Display($sConnectionString, $QUERY)

$QUERY = _
"ROLLBACK TRANSACTION"

_Query_Display($sConnectionString, $QUERY)

$QUERY = _
"SELECT * FROM AdventureWorks2016.HumanResources.Department" & @CRLF & _
"WHERE Name = 'Engineering'"

_Query_Display($sConnectionString, $QUERY)

The changes were committed and I got this error :

The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.

Link to comment
Share on other sites

Well, the update now works which is a good thing, but I've no idea why and how you can get this error using this SQL, whatever engine you use.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

8 hours ago, Nas said:
$QUERY = _
"BEGIN TRANSACTION"

_Query_Display($sConnectionString, $QUERY)

$QUERY = _
"UPDATE AdventureWorks2016.HumanResources.Department" & @CRLF & _
"SET GroupName = 'Research % Development'" & @CRLF & _
"WHERE Name = 'Engineering'"

_Query_Display($sConnectionString, $QUERY)

$QUERY = _
"SELECT * FROM AdventureWorks2016.HumanResources.Department" & @CRLF & _
"WHERE Name = 'Engineering'"

_Query_Display($sConnectionString, $QUERY)

$QUERY = _
"ROLLBACK TRANSACTION"

_Query_Display($sConnectionString, $QUERY)

$QUERY = _
"SELECT * FROM AdventureWorks2016.HumanResources.Department" & @CRLF & _
"WHERE Name = 'Engineering'"

_Query_Display($sConnectionString, $QUERY)

The changes were committed and I got this error :

The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.

 

6 hours ago, jchd said:

but I've no idea why and how you can get this error using this SQL, whatever engine you use.

The reason that you got the error message "The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION", in this particular case, is because your _Query_Display() function opens and closes a new connection each time it is executed.  SQL checkpoints, created using BEGIN TRANSACTION statements, only exist within their given connections.  Therefore, when you executed the ROLLBACK, there was no BEGIN TRANSACTION in effect.  The only connection that had a valid BEGIN TRANSACTION was the first one and it was the only statement executed in that connection.

Although it is true that in MSSQL's T-SQL that a semicolon is only necessary in a few conditions, if you are going to batch statements together, you should use GO statements to separate the commands that should logically be executed together.  If/Where you place the GO statements can have effect the result sets returned within the batched statements.

Link to comment
Share on other sites

1 hour ago, TheXman said:

The reason that you got the error message "The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION", in this particular case, is because your _Query_Display() function opens and closes a new connection each time it is executed. 

Oops, sorry I didn't look at the function itself.  It's indeed a weird (and even completely wrong) way to issue a statement!

@Nas keep up using vanilla ADO_* functions.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Something like this? :

$QUERY = _
"BEGIN TRANSACTION" & @CRLF & _
"UPDATE AdventureWorks2016.HumanResources.Department" & @CRLF & _
"SET GroupName = 'Research 15 Development'" & @CRLF & _
"WHERE Name = 'Engineering'" & @CRLF & _
"Go"

_Query_Display($sConnectionString, $QUERY)

$QUERY = _
"SELECT * FROM AdventureWorks2016.HumanResources.Department" & @CRLF & _
"WHERE Name = 'Engineering'" & @CRLF & _
"Go" & @CRLF & _
"ROLLBACK TRANSACTION"

_Query_Display($sConnectionString, $QUERY)

It didn't work giving me an error: Incorrect syntax near 'Go'.

Link to comment
Share on other sites

35 minutes ago, Nas said:

It didn't work giving me an error: Incorrect syntax near 'Go'.

Sorry, the bit about the GO command was in relation to sqlcmd, osql, or SSMS.

Applications based on the ODBC or OLE DB APIs will receive a syntax error if they try to execute a GO command, as you have seen.

Edited by TheXman
Link to comment
Share on other sites

1 hour ago, TheXman said:

Sorry, the bit about the GO command was in relation to sqlcmd, osql, or SSMS.

Applications based on the ODBC or OLE DB APIs will receive a syntax error if they try to execute a GO command, as you have seen.

Oh I see, that's too bad, any other Ideas, by the way I tried to put that query between the open connection and close it, still same issue.

Link to comment
Share on other sites

 

I don't have a SQL Server instance installed at the moment so I can't test the script.  Can you run the script below and see if it executes successfully and returns the expected results?  If it fails with any errors, please post the error and the console log.

 

#include <ADO.au3>
#include <Constants.au3>


Global $sDriver           = 'SQL Server'
Global $sDatabase         = 'DataBase' ; change this string to YourDatabaseName
Global $sServer           = 'Localhost' ; change this string to YourServerLocation
Global $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & ';PWD=' & ';'

Global $sQUERY

Global $aRecordset

Global $oConnection


_ADO_EVENTS_SetUp(True)

;Open connection
_ADO_Connection_OpenConString($oConnection, $sConnectionString)
If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "Unable to open MSSQL connection.  @error = " & @error)
ConsoleWrite("Connection opened." & @CRLF)

;Begin transaction
_ADO_Execute($oConnection, "BEGIN TRANSACTION", True)
If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "Unable to begin transactions.  @error = " & @error)
ConsoleWrite("BEGIN TRANSACTION successful." & @CRLF)

;Update table
_ADO_Execute($oConnection, "UPDATE table SET hour = 4 WHERE cust = 'TEST'", True)
If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "Unable to update table.  @error = " & @error)
ConsoleWrite("UPDATE successful." & @CRLF)

;Query table
$aRecordset = _ADO_Execute($oConnection, "SELECT * FROM table WHERE cust = 'TEST'", True)
If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "Unable to update table.  @error = " & @error)
ConsoleWrite("SELECT successful." & @CRLF)

;Rollback the update
_ADO_Execute($oConnection, "ROLLBACK TRANSACTION", True)
If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "Unable to rollback transactions.  @error = " & @error)
ConsoleWrite("ROLLBACK TRANSACTION successful." & @CRLF)

;Display query result (This is result set captured before rollback)
_ADO_Recordset_Display($aRecordset, 'Query Result')

;Close connection
_ADO_Connection_Close($oConnection)
If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "Unable to close MSSQL connection.  @error = " & @error)
ConsoleWrite("Connetion closed." & @CRLF)

 

Edited by TheXman
Link to comment
Share on other sites

Thanks @TheXman that sounds much more reasonable!

@Nas you typically open an SQL connection at application startup, use it by issuing SQL statements at will, then only close it at program termination, just like you generally do with a GUI.  That's true for any SQL engine I can think of.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

When I ran your script I got this error : Unable to open MSSQL connection.  @error = 5.

I found that the $oConnection is missing  so I added this line :

Global $oConnection = ObjCreate("ADODB.Connection")
If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "Unable to open MSSQL connection.  @error = " & @error)

I was able to open the connection with SQL and then I got this error :

Unable to begin transactions.  @error = 6.

Update:

For some reason it considers every _ADO_Execute as an error, so I removed those lines and it worked like a charm, @TheXman you're the best, can't thank you enough, thank you @jchd I appreciate your help.

#include <ADO.au3>
#include <Constants.au3>

Local $sDriver = 'SQL Server'
Local $sDatabase = 'AdventureWorks2016' ; change this string to YourDatabaseName
Local $sServer = 'Localhost' ; change this string to YourServerLocation
Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & ';PWD=' & ';'

Global $sQUERY

Global $aRecordset

Local $oConnection = ObjCreate("ADODB.Connection")
If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "Unable to open MSSQL connection.  @error = " & @error)


_ADO_EVENTS_SetUp(True)

;Open connection
_ADO_Connection_OpenConString($oConnection, $sConnectionString)
If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "Unable to open MSSQL connection.  @error = " & @error)
ConsoleWrite("Connection opened." & @CRLF)

;Begin transaction
_ADO_Execute($oConnection, "BEGIN TRANSACTION", True)
ConsoleWrite("BEGIN TRANSACTION successful." & @CRLF)

;Update table
_ADO_Execute($oConnection, "UPDATE HumanResources.Department SET GroupName = 'Research && Development' WHERE Name = 'Engineering'", True)
ConsoleWrite("UPDATE successful." & @CRLF)

;Query table
$aRecordset = _ADO_Execute($oConnection, "SELECT * FROM HumanResources.Department WHERE Name = 'Engineering'", True) ; setting $aRecordset with the result before rolling back
ConsoleWrite("SELECT successful." & @CRLF)

;Rollback the update
_ADO_Execute($oConnection, "ROLLBACK TRANSACTION", True)
ConsoleWrite("ROLLBACK TRANSACTION successful." & @CRLF)

;Display query result (This is result set captured before rollback)
_ADO_Recordset_Display($aRecordset, 'Query Result')

$aRecordset = _ADO_Execute($oConnection, "SELECT * FROM HumanResources.Department WHERE Name = 'Engineering'", True) ; setting $aRecordset with the original result after rolling back

;Display query result (This is result set captured after rollback)
_ADO_Recordset_Display($aRecordset, 'Query Result')

;Close connection
_ADO_Connection_Close($oConnection)
If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "Unable to close MSSQL connection.  @error = " & @error)
ConsoleWrite("Connetion closed." & @CRLF)

 

Edited by Nas
Link to comment
Share on other sites

Thanks!  I'm glad you were able to get it all sorted out.  :thumbsup:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...