Difference between revisions of "SQLPREPARE()"

From Lianjapedia
Jump to: navigation, search
m (1 revision: SQL)
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
==Purpose==
 
==Purpose==
 
Prepare an SQL statement that will be executed by the SQLEXEC() function
 
Prepare an SQL statement that will be executed by the SQLEXEC() function
 
  
 
==Syntax==
 
==Syntax==
 
SQLPREPARE(<nStatementHandle>, <cSQLCommand> [, <cCursorName>])
 
SQLPREPARE(<nStatementHandle>, <cSQLCommand> [, <cCursorName>])
 
  
 
==See Also==
 
==See Also==
[[CREATE CONNECTION]], [[SQLCANCEL()]], [[SQLCOLUMNS()]], [[SQLCOMMIT()]], [[SQLCONNECT()]], [[SQLDISCONNECT()]], [[SQLERROR()]], [[SQLEXEC()]], [[SQLGETPROP()]], [[SQLMORERESULTS()]], [[SQLROLLBACK()]], [[SQLSETPROP()]], [[SQLSTRINGCONNECT()]], [[SQLTABLES()]]
+
[[SQLCANCEL()]], [[SQLCOLUMNS()]], [[SQLCOMMIT()]], [[SQLCONNECT()]], [[SQLDISCONNECT()]], [[SQLERROR()]], [[SQLEXEC()]], [[SQLGETPROP()]], [[SQLMORERESULTS()]], [[SQLPARAMS()]], [[SQLROLLBACK()]], [[SQLSETPROP()]], [[SQLSTRINGCONNECT()]], [[SQLTABLES()]], [[SQLTRANSACTION()]]
 
+
  
 
==Description==
 
==Description==
Line 16: Line 13:
 
The SQLPREPARE() function operates on the data source specified by <nStatementHandle>.
 
The SQLPREPARE() function operates on the data source specified by <nStatementHandle>.
  
 
+
{| class="wikitable" width="100%"
{| class="wikitable"
+
!width="30%"|Keywords||width="70%"|Description
!Keywords||Description
+
 
|-
 
|-
|nStatementHandle||The workarea in which the gateway data source is open.
+
|nStatementHandle||Statement handle to the connection.
 
|-
 
|-
 
|cSQLCommand||The SQL statement to be passed to the data source.
 
|cSQLCommand||The SQL statement to be passed to the data source.
 
|-
 
|-
|cCursorName||The name of the cursor to use.  If cCursorName is not specified, the default name SQLRESULT is used.
+
|valign="top"|cCursorName||The name of the cursor to use.  If cCursorName is not specified, the default name SQLRESULT is used.
 
|-
 
|-
 
|}
 
|}
 
  
 
====Return values:====
 
====Return values:====
  
 
+
{| class="wikitable" width="100%"
{| class="wikitable"
+
!width="30%"|Return Value||width="70%"|Description
!Return Value||Description
+
 
|-
 
|-
 
|1||SQLPREPARE() successful
 
|1||SQLPREPARE() successful
Line 40: Line 34:
 
|-
 
|-
 
|}
 
|}
 
  
 
==Example==
 
==Example==
 
<code lang="recital">
 
<code lang="recital">
nStatHand=SQLSTRINGCONNECT("rec@rec1:user1/pass1-/usr/recital/uas/data/southwind.tcpip",.T.)
+
nStatHand = sqlconnect("awhr")
 
if nStatHand < 1
 
if nStatHand < 1
  messagebox('Cannot make connection', 16, 'SQL Connect Error')
+
messagebox("Cannot make connection", 16, "SQL Connect Error")
 
else
 
else
  messagebox('Connection made', 48, 'SQL Connect Message')
+
messagebox("Connection made", 48, "SQL Connect Message")
  store "00010" to myVar
+
sqlexec(nStatHand,"create table doc1 (account_no char(5), lastname char(25), balance dec(10,2))")
  SQLPREPARE(nStatHand, "SELECT * FROM example WHERE account_no = ?myVar", "restab")
+
messagebox("Table created")
  SQLEXEC(nStatHand)
+
m_accno =  "00699"
  browse
+
m_lastname = "Smith"
  SQLDISCONNECT(nStatHand)
+
m_balance = 12345.67
 +
sqlexec(nStatHand,"insert into dbo.doc1 (account_no,lastname,balance) VALUES ('&m_accno','&m_lastname',&m_balance)")
 +
messagebox("Record inserted")
 +
nRet = sqlexec(nStatHand,"select * from dbo.doc1","mycursor")
 +
if nRet = -1
 +
messagebox(sqlerror(nstatHand))
 +
else
 +
messagebox("Record selected")
 +
list
 +
endif
 +
// Using prepared statement
 +
sqlprepare(nStatHand,"select * from dbo.doc1 where account_no = ?m_accno","mycursor")
 +
nRet = sqlexec(nStatHand)
 +
if nRet < 1
 +
messagebox(sqlerror(nstatHand))
 +
else
 +
messagebox("Record selected using prepared statement")
 +
list
 +
endif
 +
sqldisconnect(nStatHand)
 
endif
 
endif
 
</code>
 
</code>
  
==Products==
 
Recital, Recital Server
 
 
[[Category:Documentation]]
 
[[Category:Documentation]]
 
[[Category:Functions]]
 
[[Category:Functions]]
 
[[Category:SQL]]
 
[[Category:SQL]]
[[Category:Remote Data Connectivity Functions]]
+
[[Category:ODBC Functions]]
 +
[[Category:SQL Functions]]

Latest revision as of 10:23, 30 January 2017

Purpose

Prepare an SQL statement that will be executed by the SQLEXEC() function

Syntax

SQLPREPARE(<nStatementHandle>, <cSQLCommand> [, <cCursorName>])

See Also

SQLCANCEL(), SQLCOLUMNS(), SQLCOMMIT(), SQLCONNECT(), SQLDISCONNECT(), SQLERROR(), SQLEXEC(), SQLGETPROP(), SQLMORERESULTS(), SQLPARAMS(), SQLROLLBACK(), SQLSETPROP(), SQLSTRINGCONNECT(), SQLTABLES(), SQLTRANSACTION()

Description

The SQLPREPARE() function is used to prepare an SQL statement which will subsequently be executed by the SQLEXEC() function on the specified data source.

The SQLPREPARE() function operates on the data source specified by <nStatementHandle>.

Keywords Description
nStatementHandle Statement handle to the connection.
cSQLCommand The SQL statement to be passed to the data source.
cCursorName The name of the cursor to use. If cCursorName is not specified, the default name SQLRESULT is used.

Return values:

Return Value Description
1 SQLPREPARE() successful
–1 Connection error

Example

nStatHand = sqlconnect("awhr")
if nStatHand < 1
	messagebox("Cannot make connection", 16, "SQL Connect Error")
else
	messagebox("Connection made", 48, "SQL Connect Message")
	sqlexec(nStatHand,"create table doc1 (account_no char(5), lastname char(25), balance dec(10,2))")
	messagebox("Table created")
	m_accno =  "00699"
	m_lastname = "Smith"
	m_balance = 12345.67
	sqlexec(nStatHand,"insert into dbo.doc1 (account_no,lastname,balance) VALUES ('&m_accno','&m_lastname',&m_balance)")
	messagebox("Record inserted")
	nRet = sqlexec(nStatHand,"select * from dbo.doc1","mycursor")
	if nRet = -1
		messagebox(sqlerror(nstatHand))
	else
		messagebox("Record selected")
		list
	endif
	// Using prepared statement
	sqlprepare(nStatHand,"select * from dbo.doc1 where account_no = ?m_accno","mycursor")
	nRet = sqlexec(nStatHand)
	if nRet < 1
		messagebox(sqlerror(nstatHand))
	else
		messagebox("Record selected using prepared statement")
		list
	endif
	sqldisconnect(nStatHand)
endif