Home > Language Reference > Database Functions > DbSelect

DbSelect

The DbSelect function creates a recordset by selecting the desired records in the specified Data Source Name.

Usage

Used to select the records in the Data Source Name which are used in subsequent Database functions.

Syntax

ret=DbSelect(<SQL_command> [,DYNASET|SNAPSHOT])

Parameters

<SQL_command>, string, a valid SQL statement. If the <SQL_command> is not accepted, verify it is a valid one using Msgbox function to display it first before using it in DbSelect.

DYNASET|SNAPSHOT, optional keyword. It specifies how the database must be read, SNAPSHOT (default value) for ODBC static mode, DYNASET for ODBC dynamic mode.

Remarks

The selection is a Unicode string, so instead of such a selection:
sql$ = "select 'XYZ' as typeref"
use:
sql$ = "select Cast('XYZ' as nvarchar) as typeref"

Return value

ret, optional numeric return value.  If the selection has been performed successfully, the return value is 0. If the function fails, the return value is a nonzero value (see Error Codes for Database Functions).

See also

DbBof
DbClose
DbConnect
DbDisconnect
DbEof
DbExecute
DbGetFieldNumeric
DbGetFieldString
DbMove
DbMoveFirst
DbMoveLast
DbMoveNext
DbMovePrev
DbRecordCount

Examples

'The example uses a data source name called DBTest which contains those records in a table called DATA:
' a,1, 1.23, 02/01/2011
' b,2, 2.34, 03/02/2012
' c,3, 3.45, 04/12/2009
DbConnect("DSN=DBTest") ' connection to ODBC database called DBTest
DbSelect("SELECT * FROM DATA",DYNASET) ' select all records in table DATA


'With a complex SQL syntax:
DbConnect("DSN=gpao")
my_request$="SELECT CodeArticle.ARTICLE,Designation1.ARTICLE FROM LCTC,ARTICLE WHERE CodeRubrique.LCTC=CodeArticle.ARTICLE AND CodeLancement.LCTC='10000' AND TypeRubrique.LCTC='A'"
'Display the sql command before using it in order to check it
msgbox(my_request$)

'With a variable in the SQL command
lct$="10000"
my_request1$="SELECT CodeArticle.ARTICLE,Designation1.ARTICLE FROM LCTC,ARTICLE WHERE CodeRubrique.LCTC=CodeArticle.ARTICLE AND CodeLancement.LCTC= '"+lct$+"' AND TypeRubrique.LCTC='A'"
msgbox(my_request1$)
DbSelect(my_request1$,DYNASET)

ount=DbRecordCount()
msgbox(count) 'display the number of records
bConnect("DSN=DBTest") ' connection to ODBC database called DBTest
DbConnect("DSN=SQLTEST ;UID=MICHAEL ;PWD=ABC") ' connection to an ODBC database called SQLTEST for the user Michael using the password ABC