WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Sample Code

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Get Row Count From Resultset


Here are two options....

Option 1:

dsn = "Sample" 			; Change to fit your needs
tablename = "MyTable"	; Change to fit your needs

AddExtender("wwodb34I.dll")

;general section to set up handles
henv = qAllocEnv()
If henv == -1
  retcode = qLastCode()  
  Message("Could Not Create ODBC Environment", retcode)
  Exit
Endif

hdbc = qAllocConnect(henv) 
If hdbc == -1
  retcode = qLastCode()
  Message("qAllocConnect failed", retcode)
  Exit
Endif

;Now, connect to DSN
retcode = qConnect(hdbc, dsn, "", "")
If (retcode != @qSuccess) && (retcode != @qSuccessInfo)
  Message("Connection To Data Source Failed", retcode)
  Exit
Endif

hstmt = qAllocStmt(hdbc)
If hstmt != 0
  retcode = qLastCode()
  Message("qAllocStmt failed", retcode)
  Exit
Endif

cSQL1 =strcat("SELECT COUNT(*) FROM ",tablename)
retcode = qExecDirect(hstmt,cSQL1)
If (retcode != @qSuccess) && (retcode != @qSuccessInfo)
	ret=qError(hstmt,2)
	Message("Error", ret)
	Exit
Else
	rc = BinaryAlloc(20)
	retcode = qFetch(hstmt)
	retcode = qGetData(hstmt,1,"rc",20)
	n = Int( BinaryPeekStr(rc,0,BinaryEodGet(rc) ) )
	binaryfree(rc)
	If n<1
		Display(2,"Abandoning Query","Will Produce Empty RowSet")
		exit
	Else
		Display(2,"Row Count",StrCat(n," rows should be returned"))
	Endif
Endif

exit

Option 2:

dsn = "Sample" 			; Change to fit your needs
tablename = "MyTable"	; Change to fit your needs

AddExtender("wwodb34I.dll")

;general section to set up handles
henv = qAllocEnv()
If henv == -1
  retcode = qLastCode()  
  Message("Could Not Create ODBC Environment", retcode)
  Exit
Endif

hdbc = qAllocConnect(henv) 
If hdbc == -1
  retcode = qLastCode()
  Message("qAllocConnect failed", retcode)
  Exit
Endif

;Now, connect to DSN
retcode = qConnect(hdbc, dsn, "", "")
If (retcode != @qSuccess) && (retcode != @qSuccessInfo)
  Message("Connection To Data Source Failed", retcode)
  Exit
Endif

hstmt = qAllocStmt(hdbc)
If hstmt != 0
  retcode = qLastCode()
  Message("qAllocStmt failed", retcode)
  Exit
Endif

retcode = qExecDirect(hstmt, StrCat("SELECT * FROM ",tablename))
If (retcode != @qSuccess) && (retcode != @qSuccessInfo)
  ret=qError(hstmt,2)
  Message("Error", ret)
  Exit
Endif

counter=0
While @TRUE
  ;FETCHES A ROW OF DATA FROM A RESULT SET
  retcode = qFetch(hstmt)  
  If retcode == @qNoData Then Break
  counter=counter+1
EndWhile

Message("NUMBER OF ROWS IN RESULT SET",counter)

Article ID:   W15850
File Created: 2004:03:30:15:41:08
Last Updated: 2004:03:30:15:41:08