Nas Posted June 25, 2020 Posted June 25, 2020 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 : expandcollapse popup#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.
Danp2 Posted June 25, 2020 Posted June 25, 2020 You shouldn't need to wrap a basic SQL Select in a transaction. What's the point in doing that? When you perform a rollback, your changes get wiped out. So what exactly are you expecting to happen? Maybe you should check @error following the call to _ADO_Execute Latest Webdriver UDF Release Webdriver Wiki FAQs
Nas Posted June 25, 2020 Author Posted June 25, 2020 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.
Danp2 Posted June 25, 2020 Posted June 25, 2020 1 - Yes... just drop the BEGIN TRAN and ROLLBACK TRAN from your SQL statement 2 - This may be possible, but you would need to split your SQL statement into multiple requests. Move the ROLLBACK TRAN to it's own statement that gets executed after your reviewed / displayed the results. Latest Webdriver UDF Release Webdriver Wiki FAQs
Nas Posted June 25, 2020 Author Posted June 25, 2020 (edited) 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 June 25, 2020 by Nas
jchd Posted June 25, 2020 Posted June 25, 2020 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 hereRegExp tutorial: enough to get startedPCRE 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)
Nas Posted June 25, 2020 Author Posted June 25, 2020 I need the query inside the BEGIN TRAN if not it will actually execute before I can see the result. MS doesn't need the semi column.
Danp2 Posted June 25, 2020 Posted June 25, 2020 Did you try what I suggested above under #2? Latest Webdriver UDF Release Webdriver Wiki FAQs
jchd Posted June 25, 2020 Posted June 25, 2020 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 hereRegExp tutorial: enough to get startedPCRE 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)
Nas Posted June 26, 2020 Author Posted June 26, 2020 $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.
jchd Posted June 26, 2020 Posted June 26, 2020 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 hereRegExp tutorial: enough to get startedPCRE 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)
TheXman Posted June 26, 2020 Posted June 26, 2020 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. CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
jchd Posted June 26, 2020 Posted June 26, 2020 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 hereRegExp tutorial: enough to get startedPCRE 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)
Nas Posted June 26, 2020 Author Posted June 26, 2020 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'.
TheXman Posted June 26, 2020 Posted June 26, 2020 (edited) 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 June 26, 2020 by TheXman mLipok 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
Nas Posted June 26, 2020 Author Posted June 26, 2020 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.
TheXman Posted June 26, 2020 Posted June 26, 2020 (edited) 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. expandcollapse popup#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 June 26, 2020 by TheXman Nas 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
jchd Posted June 26, 2020 Posted June 26, 2020 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. Nas 1 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 hereRegExp tutorial: enough to get startedPCRE 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)
Nas Posted June 27, 2020 Author Posted June 27, 2020 (edited) 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. expandcollapse popup#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 June 27, 2020 by Nas TheXman 1
TheXman Posted June 27, 2020 Posted June 27, 2020 Thanks! I'm glad you were able to get it all sorted out. Nas and Skysnake 2 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now