Question heng a · Aug 10, 2023

hi, I followed the example of the system and wrote a method to connect to an external database to obtain a result set, but I couldn't get the result. can anyone please help on this. image

/// correct
ClassMethod PersonSets(name As %String = "", state As %String = "MA") As %Integer [ ReturnResultsets, SqlName = PersonSets, SqlProc ]
{
		// %sqlcontext is automatically created for a method that defines SQLPROC

		// SQL result set classes can be easily prepared using dynamic SQL. %Prepare returns a
		// status value. The statement's prepare() method can also be called directly. prepare() throws
		// an exception if something goes wrong instead of returning a status value.
	set tStatement = ##class(%SQL.Statement).%New()
	try {
		do tStatement.prepare("select name,dob,spouse from sample.person where name %STARTSWITH ? order by 1")
		set tResult = tStatement.%Execute(name)
		do %sqlcontext.AddResultSet(tResult)
		do tStatement.prepare("select name,age,home_city,home_state from sample.person where home_state = ? order by 4, 1")
		set tResult = tStatement.%Execute(state)
		do %sqlcontext.AddResultSet(tResult)
		set tReturn = 1
	}
	catch tException {
		#dim tException as %Exception.AbstractException
		set %sqlcontext.%SQLCODE = tException.AsSQLCODE(), %sqlcontext.%Message = tException.SQLMessageString()
		set tReturn = 0
	}
	quit tReturn
}

/// error
ClassMethod odbcTest() As %Integer [ ReturnResultsets, SqlName = PersonSets2, SqlProc ]
{
	if '$isobject($Get(%sqlcontext)) { set %sqlcontext = ##class(%ProcedureContext).%New() } 
	s conn=##class(%SQLGatewayConnection).%New()
	s sc=conn.Connect("samples","_system","sys") //datasource
	if $$$ISERR(sc) do $System.Status.DisplayError(sc) quit sc
	s rs=##class(%ResultSet).%New("%DynamicQueryGW:SQLGW")
	try {
		s sql = "select * from Sample.Person"
		d rs.Prepare(sql,,conn)
		d rs.Execute()
		s ^tmp("%ROWCOUNT")=rs.%ROWCOUNT
		d %sqlcontext.AddResultSet(rs)
		s tReturn = 1
	}catch{
		#dim tException as %Exception.AbstractException
  		s %sqlcontext.%SQLCODE = tException.AsSQLCODE(), %sqlcontext.%Message = tException.SQLMessageString()
  		s sc=conn.Disconnect() 
  		s tReturn = 0
	}
	;d conn.Disconnect() 
	q tReturn
}
4
0 324