maniootek Posted April 4, 2024 Posted April 4, 2024 (edited) I just discovered very weird behavior with data return when I query MS SQL server using ADODB.Connection object in Autoit. When database table cell contains xml code as varchar and other cell contains text like '+OK' it simply does not show the result. Here is sample code: #include <Debug.au3> MsSqlQueryTest() Func MsSqlQueryTest() Local $sDatabase = 'YourBASENAME' ; change this string to YourDatabaseName Local $sServer = 'localhost\SQLExpress' ; change this string to YourServerLocation Local $sUser = 'sa' ; change this string to YourUserName Local $sPassword = 'AutoIt' ; change this string to YourPassword Local $sConnectionString = 'DRIVER={' & 'SQL Server' & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';' Local $oConnection = ObjCreate("ADODB.Connection") $oConnection.ConnectionString = $sConnectionString $oConnection.Open Local $sQuery $sQuery &= "SET NOCOUNT ON" & @CRLF $sQuery &= "DECLARE @MyTable TABLE (id INT, myValue varchar(max), pdata varchar(max))" & @CRLF $sQuery &= "INSERT INTO @MyTable VALUES (123, '+OK', '<root><receipts><receipt><command name=" & '"REPORTDAY"' & " /></receipt></receipts></root>')" & @CRLF $sQuery &= "SELECT id, myValue, pdata FROM @MyTable" Local $oRecordSet = $oConnection.Execute($sQuery) Local $array = $oRecordSet.GetRows _DebugArrayDisplay($array) EndFunc this is the result: Col1 is empty but there is '+OK' value and this is the result fromย Microsoft SQL Server Management Studio 17: The wierd thing is if I change in query Quote SELECT id, myValue, pdata FROM @MyTable to Quote SELECT id, myValue FROM @MyTable then it works properly Any idea what is wrong? Edited April 4, 2024 by maniootek
SOLVE-SMART Posted April 4, 2024 Posted April 4, 2024 Hi @maniootekย ๐ , what happens when you use nvarchar for the "pdata" column? Maybe this could help, otherwise I totally agree, it's a strange behavior. Best regards Sven ==> AutoIt related: ๐ GitHub, ๐ Discord Server, ๐ย Cheat Sheet,ย ๐ย autoit-webdriver-boilerplate Spoiler ๐ย Au3Forums ๐ฒ AutoIt (en) Cheat Sheet ๐ AutoIt limits/defaults ๐ Code Katas: [...] (comming soon) ๐ญ Collection of GitHub users with AutoIt projects ๐ย False-Positives ๐ฎย Me on GitHub ๐ฌย Opinion about new forum sub category ๐ย UDF wiki list โย VSCode-AutoItSnippets ๐ย WebDriver FAQs ๐จโ๐ซย WebDriver Tutorial (coming soon)
maniootek Posted April 4, 2024 Author Posted April 4, 2024 8 minutes ago, SOLVE-SMART said: what happens when you use nvarchar for the "pdata" column? Maybe this could help, otherwise I totally agree, it's a strange behavior. no change but I just started to play with the query and I even changed to this: Local $sQuery $sQuery &= "SET NOCOUNT ON" & @CRLF $sQuery &= "DECLARE @MyTable TABLE (id INT, string1 varchar(max), string2 varchar(max), string3 varchar(max))" & @CRLF $sQuery &= "INSERT INTO @MyTable VALUES (1, 'some string 1', 'some string 2', 'some string 3'), (2, 'some string 12', 'some string 22', 'some string 32')" & @CRLF $sQuery &= "SELECT id, string1, string2, string3 FROM @MyTable" and this is result:
SOLVE-SMART Posted April 4, 2024 Posted April 4, 2024 (edited) That's even more confusing to me ๐ต , because the SQL looks totally valid and understandable to me. I cannot reproduce it until next week or so. But maybe you will get someone who can help earlier. Best regards Sven Edited April 4, 2024 by SOLVE-SMART ==> AutoIt related: ๐ GitHub, ๐ Discord Server, ๐ย Cheat Sheet,ย ๐ย autoit-webdriver-boilerplate Spoiler ๐ย Au3Forums ๐ฒ AutoIt (en) Cheat Sheet ๐ AutoIt limits/defaults ๐ Code Katas: [...] (comming soon) ๐ญ Collection of GitHub users with AutoIt projects ๐ย False-Positives ๐ฎย Me on GitHub ๐ฌย Opinion about new forum sub category ๐ย UDF wiki list โย VSCode-AutoItSnippets ๐ย WebDriver FAQs ๐จโ๐ซย WebDriver Tutorial (coming soon)
jchd Posted April 4, 2024 Posted April 4, 2024 Does this work? "SELECT * FROM @MyTable" 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)
Solution Andreik Posted April 4, 2024 Solution Posted April 4, 2024 (edited) If I remember well some drivers don't handle well varchar(max) so if you have control over table definition just use varchar(8000). I tested with ODBC Driver 17 for SQL Server and works well. #include <Debug.au3> MsSqlQueryTest() Func MsSqlQueryTest() Local $sDatabase = 'master' ; change this string to YourDatabaseName Local $sServer = '(local)' ; change this string to YourServerLocation Local $sUser = 'sa' ; change this string to YourUserName Local $sPassword = 'test' ; change this string to YourPassword Local $sDriver = 'ODBC Driver 17 for SQL Server' Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';' Local $oConnection = ObjCreate("ADODB.Connection") $oConnection.ConnectionString = $sConnectionString $oConnection.Open Local $sQuery $sQuery &= "SET NOCOUNT ON" & @CRLF $sQuery &= "DECLARE @MyTable TABLE (id INT, myValue varchar(8000), pdata varchar(8000))" & @CRLF $sQuery &= "INSERT INTO @MyTable VALUES (123, '+OK', '<root><receipts><receipt><command name=" & '"REPORTDAY"' & " /></receipt></receipts></root>')" & @CRLF $sQuery &= "SELECT id, myValue, pdata FROM @MyTable" Local $oRecordSet = $oConnection.Execute($sQuery) Local $array = $oRecordSet.GetRows _DebugArrayDisplay($array) EndFunc Alternatively some drivers support DataTypeCompatibility in connection string, so you might give a try. If you don't have control over table definition, you can cast data in the query: #include <Debug.au3> MsSqlQueryTest() Func MsSqlQueryTest() Local $sDatabase = 'master' ; change this string to YourDatabaseName Local $sServer = '(local)' ; change this string to YourServerLocation Local $sUser = 'sa' ; change this string to YourUserName Local $sPassword = 'test' ; change this string to YourPassword Local $sDriver = 'ODBC Driver 17 for SQL Server' Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';' Local $oConnection = ObjCreate("ADODB.Connection") $oConnection.ConnectionString = $sConnectionString $oConnection.Open Local $sQuery $sQuery &= "SET NOCOUNT ON" & @CRLF $sQuery &= "DECLARE @MyTable TABLE (id INT, myValue varchar(max), pdata varchar(max))" & @CRLF $sQuery &= "INSERT INTO @MyTable VALUES (123, '+OK', '<root><receipts><receipt><command name=" & '"REPORTDAY"' & " /></receipt></receipts></root>')" & @CRLF $sQuery &= "SELECT id, CAST(myValue as varchar(8000)), CAST(pdata as varchar(8000)) FROM @MyTable" Local $oRecordSet = $oConnection.Execute($sQuery) Local $array = $oRecordSet.GetRows _DebugArrayDisplay($array) EndFunc ย Edited April 4, 2024 by Andreik maniootek and robertocm 2
Zedna Posted April 4, 2024 Posted April 4, 2024 Also have in mind ListView limits: ย Resources UDF ย ResourcesEx UDF ย AutoIt Forum Search
maniootek Posted April 4, 2024 Author Posted April 4, 2024 1 hour ago, Andreik said: If you don't have control over table definition, you can cast data in the query: thank you, casting data in the select query as varchar(1000) fix the problem argumentum 1
jchd Posted April 4, 2024 Posted April 4, 2024 Still weird! 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)
Andreik Posted April 4, 2024 Posted April 4, 2024 15 minutes ago, jchd said: Still weird! Indeed. I remembered this because I encountered this problem with the old SQLNCLI11 driver but it's unexpected to see the same stupid behavior with the newer drivers. argumentum 1
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