Jump to content

MS SQL Server query via ADODB.Connection object doest not show result when specified combination of returned values


Go to solution Solved by Andreik,

Recommended Posts

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:

image.png.eb6f88ba6e99ed1d4244f1cffb63c81f.png

Col1 is empty but there is '+OK' value

and this is the result from Microsoft SQL Server Management Studio 17:

image.png.95ae79ad30a00280f64fe3768f3e8ac5.png

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

image.png.97eaff7ff2422882e0b5b772f2ce9bf6.png

Any idea what is wrong?

Edited by maniootek
Link to comment
Share on other sites

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

Stay innovative!

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)

Link to comment
Share on other sites

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:

image.png.c5a3366c8c8ddc64836baf6a99242827.png

Link to comment
Share on other sites

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 by SOLVE-SMART

Stay innovative!

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)

Link to comment
Share on other sites

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 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

  • Solution
Posted (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 by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

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 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

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.

When the words fail... music speaks.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...