How to Create a Trigger in SQL Server for Use with CKLast reviewed: December 18, 1997Article ID: Q115113 |
2.50 2.50a 2.50b 2.60 | 2.50 2.50a 2.50b 2.60
MS-DOS | WINDOWSkbinterop kbtool kbprg kbcode The information in this article applies to:
SUMMARYA trigger is a special kind of stored procedure that is used to enforce integrity constraints. It is executed automatically whenever the user tries to modify data that the trigger has been designed to protect. Using the FoxPro Connectivity Kit's DBExec() function, you can cause a trigger to return an error message back to FoxPro using SQL Server's RAISERROR command. Below are the procedures for setting up a trigger in SQL Server and returning an error to FoxPro using the Connectivity Kit.
MORE INFORMATIONWARNING: This code sample is provided on an "as-is" basis. This code sample was produced to answer commonly asked questions about usage of FoxPro commands, functions, and features to solve particular types of problems by illustrating a general approach that can be used to solve the problem. This code sample is not intended to be used within an application and is not supported by Microsoft Product Support Services. We make no warranty, either expressed or implied, including but not limited to implied warranties of merchantability and fitness for a particular purpose, with regard to this code sample or associated documentation. For questions concerning the creation, syntax of, or functionality of a trigger, refer to the CREATE TRIGGER command in the SQL Server "Language Reference" or contact Microsoft SQL Server Product Support Services.
Setting Up a Trigger in SQL Server
Setting Up FoxPro to Cause the Trigger to Be InvokedType in and run the following program:
*****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' **********Try to delete an author that has a title. **********Using the DBExec() Function. **********This will activate that SQL Server's trigger **********which will cause an error to be returned. sqlcomm= "delete authors where au_id='409-56-7008'" 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 RETURNNote that an error occurs and the trigger returns the error specified with the RAISERROR command.
|
Additional reference words: FoxDos FoxWin 2.50 2.50a 2.50b 2.60 ODBC CK
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |