HOWTO: Executing a Stored Procedure on SQL ServerLast reviewed: February 21, 1997Article ID: Q114787 |
The information in this article applies to:
SUMMARYBy using the FoxPro Connectivity Kit's DBExec() function, you can execute a stored SQL Server procedure.
MORE INFORMATIONThe steps below describe how to create a stored procedure on Microsoft SQL Server; this information has been presented as a matter of convenience and is not supported by FoxPro Product Support. For questions concerning the creation, syntax, or functionality of stored procedures, refer to the CREATE PROCEDURE command in the SQL Server "Language Reference," or contact SQL Server Product Support. The commands to create the stored procedure can also be executed with the DBExec() function from FoxPro. Below are the steps to set up a stored procedure on SQL Server and how to execute it from FoxPro using the Connectivity Kit.
Setting Up a Stored Procedure on SQL Server
Setting Up FoxPro to Run a Stored ProcedureRun one of the following programs, depending on your version of FoxPro.
FoxPro 2.x Code:
*****SET THE LIBRARY AND INITIALIZE VARS
IF _DOS
SET LIBRARY TO SYS(2004)+"fpsql.plb"
ELSE
SET LIBRARY TO SYS(2004)+"fpsql.fll"
ENDIF
PUBLIC errval
PUBLIC errmsg
PUBLIC handle
errval=0
errmsg=' '
sourcename= 'test'
user= 'sa'
passwd=''
********CONNECT
handle=DBConnect(sourcename,user,passwd)
IF handle > 0
WAIT WINDOW 'Successfully Connected' NOWAIT
ELSE
error=DBError(0,@errmsg,@errval)
WAIT WINDOW STR(error)+' '+STR(errval)+' '+errmsg
ENDIF
=DBSetOpt(handle,'Asynchronous',0)
=DBSetOpt(handle,'BatchMode',1)
=DBSetOpt(handle,'ConnTimeout',0)
=DBSetOpt(handle,'Transact',1)
=DBSetOpt(handle,'UseTable',0)
err=DBExec(handle,'use pubs')
DO errhand WITH err,'USE PUBS'
**********THIS PROGRAM DEMOs HOW TO IMPLEMENT SQL WITH
**********THE DBExec() FUNCTION
sqlcomm= "execute showsales '7066'"
err=DBExec(handle,sqlcomm)
DO errhand WITH err,"DBExec(handle,"+sqlcomm+")"
IF err > 0
BROWSE
ENDIF
**********DISCONNECT
err=DBDisconn(handle)
DO errhand WITH err,"DBDisconn()"
SET LIBRARY TO
CLOSE ALL
**********Error Handler Program
PROCEDURE errhand
PARAMETERS err,command
IF err > 0
WAIT WINDOW ALLTRIM(UPPER(command))+"Completed Successfully";
NOWAIT
ELSE
WAIT WINDOW UPPER(command)+"NOT Completed Successfully"
error=DBError(handle,@errmsg,@errval)
WAIT WINDOW STR(error)+" "+STR(errval)+" "+errmsg
ENDIF
RETURN
Note that the program returns the two records that have 7066 as the
stor_id.
Visual FoxPro Code:
PUBLIC errval
PUBLIC errmsg
PUBLIC handle
errval=0
errmsg=' '
sourcename= 'test'
user= 'sa'
passwd=''
********CONNECT
* Turning on error display for connections
=SQLSetProp(0,"DispWarning",.t.)
handle=SQLConnect(sourcename,user,passwd)
IF handle > 0
WAIT WINDOW 'Successfully Connected' NOWAIT
ENDIF
********Set some defaults
=SQLSetProp(handle,'Asynchronous',.f.)
=SQLSetProp(handle,'BatchMode',.t.)
=SQLSetProp(handle,'ConnectTimeOut',0)
=SQLSetProp(handle,'Transactions',1)
err=SQLExec(handle,'use pubs')
DO errhand WITH err,'USE PUBS'
**********THIS PROGRAM DEMOs HOW TO IMPLEMENT SQL WITH
**********THE SQLExec() FUNCTION
sqlcomm= "execute showsales '7066'"
err=SQLExec(handle,sqlcomm)
DO errhand WITH err,"SQLExec(handle,"+sqlcomm+")"
IF err > 0
BROWSE
ENDIF
**********DISCONNECT
err=SQLDisconnect(handle)
DO errhand WITH err,"SQLDisconnect()"
CLOSE ALL
**********Error Handler Program
PROCEDURE errhand
PARAMETERS err,command
IF err > 0
WAIT WINDOW ALLTRIM(UPPER(command))+"Completed Successfully";
NOWAIT
ELSE
WAIT WINDOW UPPER(command)+"NOT Completed Successfully"
ENDIF
RETURN
Note that the program returns the two records that have 7066 as the
stor_id.
|
KBCategory: kbinterop kbprg kbcode
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |