[sqlserverdb]Driver=/quovadx/cis19.1/integrator/lib/Connect8.0/lib/CVsqls27.soDescription=DataDirect 8.0 SQL Server Wire ProtocolAddress=10.10.x.x, 1433AlternateServers=AnsiNPW=YesConnectionRetryCount=0ConnectionRetryDelay=3Database=DatabaseNameFetchTSWTZasTimestamp=0FetchTWFSasTime=0LoadBalancing=0LogonID=USERNAMEPassword=PASSWORDQuotedId=NoReportCodepageConversionErrors=0ReportDateTimeType=1QEWSD=2456728SnapshotSerializable=0
############################################################################## Name: sql_sp_query## Purpose: Executes a stored procedure on a remote SQL Server DB and returns the value.# This should be used when you are passing a single parameter and expecting a single value returned.# Works well when expecting a boolean (yes/no, 0/1, T/F) result. Stored procedures should be written# to return ‘0’ when false or no result found.### Arguments: dsn – The name of the ODBC connection in the $HCIROOT/lib/Connect7.0/odbc.ini file# (There is also a home/hci/odbc.ini file, but I don’t think we need to update this one manually.)# usr – Username to connect to the DB# pwd – Password# sp – Stored procedure# parm- Parameter for stored procedure *ONLY ONE PARAMETER MAY BE PASSED, OR BLANK STRING “”*# *Example, when calling from another proc:# “sql_sp_query sqlserverdb myusername mypassword sp_storedprocname parameter”*### History:# 1. 6/23/2016 – Mike Burrows – Created#proc sql_sp_query { dsn usr pwd sp parm } {if {[catch {package require odbc# Allocate an environment handle/pointer (henv)odbc SQLAllocHandle SQL_HANDLE_ENV SQL_NULL_HANDLE henv# Set up the environmentodbc SQLSetEnvAttr $henv SQL_ATTR_ODBC_VERSION SQL_OV_ODBC3 0# Allocate a DB handle / pointer (hdbc)odbc SQLAllocHandle SQL_HANDLE_DBC $henv hdbc# Connect to DB using hdbc, passing usr / pwodbc SQLConnect $hdbc $dsn SQL_NTS $usr SQL_NTS $pwd SQL_NTS# Allocate a Statement handle/pointer (hsmt)odbc SQLAllocHandle SQL_HANDLE_STMT $hdbc hstmt# Init variables for the returned value(s). Apparently length is going to be required for the query, so init a var for that too.set sqlret “POSSIBLE_SQL_ERROR”set sqlretlength “”# SQL INS CONT.# Command to send to DB# Syntax: set SQLCmd “sp_STORED_PROC_NAME \’$parameter\'”if {$parm != “”} {set SQLCmd “EXEC $sp \’$parm\'”} else {set SQLCmd “EXEC $sp”}# Execute commandodbc SQLExecDirect $hstmt $SQLCmd SQL_NTS# Bind the results to sqlret variable. Using the correct min. length is important here.odbc SQLBindCol $hstmt 1 SQL_NVARCHAR sqlret 90 sqlretlength# The SQLFetch actually retrieves the results for your vars… (Seems extraneous… not sure?)odbc SQLFetch $hstmt# The variables are now populated with the return value(s) and length#echo “sqlret variable: $sqlret”#echo “sqlretlength variable: $sqlretlength”set returnVal $sqlret# The following is to free the handles and disconnect from the database# Critically important for the OSodbc SQLFreehandle SQL_HANDLE_STMT $hstmtodbc SQLDisconnect $hdbcodbc SQLFreeHandle SQL_HANDLE_DBC $hdbcodbc SQLFreeHandle SQL_HANDLE_ENV $henv} sqlErr]} {set returnVal “POSSIBLE_SQL_ERROR”}#echo $returnVal#echo $sqlErrreturn $returnVal}
#First, set all your parameters in variables such as par1, par2, etc.set comma “,”#Set name of server in ODBC connection here:set serv “server”#Set DB username hereset uname “username”#Set DB passsword hereset pw “password”#Set name of stored procedure hereset spName “storedprocedure”# Put previously defined parameter variables into a list. Note that the first and last parameter are missing their first and last quote marks, respectively.set pList#Join the list into a single stringset p [join $pList $comma]#echo “Parameter list: $p”#Call the sql_sp_query proc and set the result to the vDone variableset vDone [sql_sp_query $serv $uName $pw $spName $p]