Jump to content
rudi

Query one single value from MSSQL Server 11.0.7469.6

Recommended Posts

Hi.

For retrieving the data on the Server itself this command is working fine, but I'd like to do the query over LAN:

 

Tool, included in the MS SQL Managment Studio:

 

C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\bcp.exe

 

I've searched and found several approaches (_SQL.UDF, MSSQL.UDF, the sqlite stuff), but are not really familiar with SQL.

 

What direction is best to take to do this query from a remote WS with sa auth against a mssql Server?

 

bcp.exe  "SELECT max([FI_FileIterationID]) from [MuM_PSJobserver].[dbo].[FileIterationBuffer]" queryout "c:\temp\LastIterationID-JobSrv.TXT"  -d -Sgoe-vault01\autodeskvault -Usa -PMyPlainTextPassword -c -C1252


The Server Hostname is "Goe-Vault01"
The Database is "AutodeskVault"

 

bildchen.jpg


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites
....
_ADO_Execute("SELECT max([FI_FileIterationID]) from [MuM_PSJobserver].[dbo].[FileIterationBuffer]")
.....

 


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API Forum Rules *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

11

Signature last update: 2018-10-31

Share this post


Link to post
Share on other sites

Thanks to both for your reply.

In the ADO_EXAMPLE.au3 I found _Example_4_MSSQL(). I try and failed to use it with this modifications:

 

#include "H:\DATEN\PRIVATE\SYSOP\NETZ\Batch\autoit3\Snippets\ADO-2.1.15-BETA\ADO.au3"


$Server="Goe-Vault01"
$Database="GoeVault01\AUTODESKVAULT"
$User="sa"
$Pass="MyPlainTextPassword"


; Jobserver
$Query="SELECT max([FI_FileIterationID]) from [MuM_PSJobserver].[dbo].[FileIterationBuffer]"
$LastIterationJobSrv=MSSQL_Query($Server,$Database,$User,$Pass,$Query)



; Vault
$Query="SELECT max([FileIterationID]) from [Vault].[dbo].[FileIteration]"
$LastIterationVault=MSSQL_Query($Server,$Database,$User,$Pass,$Query)



Func MSSQL_Query($sServer, $sDatabase, $sUser, $sPassword, $sQUERY)

    ; Create connection object
    Local $oConnection = _ADO_Connection_Create()

    ; Open connection with $sConnectionString
    _ADO_Connection_OpenMSSQL($oConnection, $sServer, $sDatabase, $sUser, $sPassword, 'YourAppName', @ComputerName & '_' & 'YourProgram_UID')
    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, 'Recordset content')
EndFunc

 

Maybe because I didn't use the correct 'YourAppName' and 'YourProgram_UID', what values might be appropriate?

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "H:\DATEN\PRIVATE\SYSOP\NETZ\Batch\autoit3\Snippets\ADO-2.1.15-BETA\LastIterationID_Jobserver_Vault_Query.au3" /UserParams    
+>12:19:57 Starting AutoIt3Wrapper v.17.224.935.0 SciTE v.3.7.3.0   Keyboard:00000407  OS:WIN_7/Service Pack 1  CPU:X64 OS:X64  Environment(Language:0407)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\admin\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\admin\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.5)  from:C:\Program Files (x86)\AutoIt3  input:H:\DATEN\PRIVATE\SYSOP\NETZ\Batch\autoit3\Snippets\ADO-2.1.15-BETA\LastIterationID_Jobserver_Vault_Query.au3
"H:\DATEN\PRIVATE\SYSOP\NETZ\Batch\autoit3\Snippets\ADO-2.1.15-BETA\ADO.au3"(381,89) : error: _ArrayDisplay() called with wrong number of args.
        _ArrayDisplay($aSelect, $sTitle, "", 0, '|', $sArrayHeader, Default, $iAlternateColors)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"C:\Program Files (x86)\AutoIt3\Include\Array.au3"(480,176) : REF: definition of _ArrayDisplay().
Func _ArrayDisplay(Const ByRef $aArray, $sTitle = Default, $sArrayRange = Default, $iFlags = Default, $vUser_Separator = Default, $sHeader = Default, $iMax_ColWidth = Default)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"H:\DATEN\PRIVATE\SYSOP\NETZ\Batch\autoit3\Snippets\ADO-2.1.15-BETA\ADO.au3"(385,90) : error: _ArrayDisplay() called with wrong number of args.
        _ArrayDisplay($aRecordset, $sTitle, "", 0, Default, Default, Default, $iAlternateColors)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"C:\Program Files (x86)\AutoIt3\Include\Array.au3"(480,176) : REF: definition of _ArrayDisplay().
Func _ArrayDisplay(Const ByRef $aArray, $sTitle = Default, $sArrayRange = Default, $iFlags = Default, $vUser_Separator = Default, $sHeader = Default, $iMax_ColWidth = Default)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
H:\DATEN\PRIVATE\SYSOP\NETZ\Batch\autoit3\Snippets\ADO-2.1.15-BETA\LastIterationID_Jobserver_Vault_Query.au3 - 2 error(s), 0 warning(s)
!>12:19:57 AU3Check ended. Press F4 to jump to next error.rc:2
+>12:19:57 AutoIt3Wrapper Finished.
>Exit code: 2    Time: 0.9177

 

 

 

ado-error-scite.jpg


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites
Posted (edited)

Hi again.

I had to modify the ADO.AU3 UDF this way to get rid of this two Errors:

 

ado-AU3-modifications.jpg

 

Then this script ...

#include <Debug.au3>
#include "h:\daten\private\sysop\netz\batch\autoit3\snippets\ADO-2.1.15-BETA\ado.au3"
#Tidy_Parameters=/sort_funcs /reel
#AutoIt3Wrapper_Run_AU3Check=Y
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7

#AutoIt3Wrapper_Run_Au3Stripper=Y
#Au3Stripper_Parameters=/RM

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


    Local $sDatabase = 'MuM_PSJobserver' ; change this string to YourDatabaseName
    Local $sServer = 'goe-vault01\AUTODESKVAULT' ; change this string to YourServerLocation
    Local $sUser = 'sa' ; change this string to YourUserName
    Local $sPassword = 'MyPlainTextPasswOrD' ; change this string to YourPassword
    Local $Query="SELECT max([FI_FileIterationID]) from [MuM_PSJobserver].[dbo].[FileIterationBuffer]"
    _MSSQL_Query($sServer, $sDatabase, $sUser, $sPassword, $Query)

    Local $QueryTop10="SELECT TOP (10) [FI_FileIterationID] from [MuM_PSJobserver].[dbo].[FileIterationBuffer]"
    _MSSQL_Query($sServer, $sDatabase, $sUser, $sPassword, $QueryTop10)


Func _MSSQL_Query($sServer, $sDatabase, $sUser, $sPassword, $sQUERY)

    ; Create connection object
    Local $oConnection = _ADO_Connection_Create()   ; Open connection with $sConnectionString
    _ADO_Connection_OpenMSSQL($oConnection, $sServer, $sDatabase, $sUser, $sPassword) ; not specified:  , 'YourAppName', @ComputerName & '_' & 'YourProgram_UID')   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
    _DebugArrayDisplay($aRecordset,UBound($aRecordset))
    for $i = 0 to UBound($aRecordset) - 1
        if IsArray($aRecordset[$i]) Then
            _DebugArrayDisplay($aRecordset[$i],"Array Element " & $i)
        Else
            MsgBox(0,"Array Element " & $i,$aRecordset[$i])
        EndIf
    Next

    ; Display Array Content with column names as headers
    _ADO_Recordset_Display($aRecordset, 'Recordset content')
EndFunc   ;==>_Example_4_MSSQL


Func _ErrDescription($sDescription = Default)
    Local Static $sDescription_static = ''
    If $sDescription <> Default Then $sDescription_static = $sDescription
    Return $sDescription_static
EndFunc   ;==>_ErrDescription

Func _ErrFunc($oError)
    ConsoleWrite( _
            @ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & _
            "$oError.description is: " & @TAB & $oError.description & @CRLF & _
            "$oError.windescription: " & @TAB & $oError.windescription & @CRLF & _
            "$oError.number is: " & @TAB & Hex($oError.number, 8) & @CRLF & _
            "$oError.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            "$oError.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            "$oError.source is : " & @TAB & $oError.source & @CRLF & _
            "$oError.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            "$oError.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF _
            )
    _ErrDescription($oError.description) ; store description to use it outsided UDF in your own function
EndFunc   ;==>_ErrFunc

...  is working, but I don't get all of its Output:

For the first query, that is "asking" for just the max value, the results are this:

SQL-Result-Pic1.jpg

 

for the 2nd query, that is "asking" for the top ten values, the result is that one:

 

SQL-Result-Pic2.jpg

Questions:

 

  1. What do I miss to receive the column Name as element 2 (Row 1) in the original result Array, when doing a query just for the (single) max value?
  2. Why do I have to "comment out" parts of the line ...

_ArrayDisplay($aSelect, $sTitle, "", 0, '|', $sArrayHeader, Default, $iAlternateColors)

... to ...

_ArrayDisplay($aSelect, $sTitle, "", 0, '|', $sArrayHeader); , Default, $iAlternateColors)

... do I specify wrong Parameters?

 

CU, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
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

×
×
  • Create New...