Creating Custom Error Messages in SQL Server and using with VFP



Similar documents
SQL Tuning and Maintenance for the Altiris Deployment Server express database.

Configuring Heterogeneous Replication server for MSSQL

Chapter Replication in SQL Server

SQL Server An Overview

SQL Server Database Coding Standards and Guidelines

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

Setting Up ALERE with Client/Server Data

USING MYWEBSQL FIGURE 1: FIRST AUTHENTICATION LAYER (ENTER YOUR REGULAR SIMMONS USERNAME AND PASSWORD)

Using the Query Analyzer

A basic create statement for a simple student table would look like the following.

DB Administration COMOS. Platform DB Administration. Trademarks 1. Prerequisites. MS SQL Server 2005/ Oracle. Operating Manual 09/2011

EVENT LOG MANAGEMENT...

CIMHT_006 How to Configure the Database Logger Proficy HMI/SCADA CIMPLICITY

Sync your schedule and work orders with SME & Microsoft Outlook

Tutorial: How to Use SQL Server Management Studio from Home

Darshan Institute of Engineering & Technology PL_SQL

How To Use The Correlog With The Cpl Powerpoint Powerpoint Cpl.Org Powerpoint.Org (Powerpoint) Powerpoint (Powerplst) And Powerpoint 2 (Powerstation) (Powerpoints) (Operations

Release Notes For Versant/ODBC On Windows. Release

Implementing and Maintaining Microsoft SQL Server 2008 Integration Services

Converting InfoPlus.21 Data to a Microsoft SQL Server 2000 Database

Connect to a SQL Database with Monitouch

CA Clarity Project & Portfolio Manager

Migrate Topaz databases from One Server to Another

David Dye. Extract, Transform, Load

5. CHANGING STRUCTURE AND DATA

Getting to Know the SQL Server Management Studio

Chancery SMS Database Split

Accessing a Microsoft SQL Server Database from SAS on Microsoft Windows

Jolly Server Getting Started Guide

Troubleshooting CallManager Problems with Windows NT and Internet Information Server (IIS)

Tech Note 663 HMI Reports: Creating Alarm Database (WWALMDB) Reports

FileMaker 12. ODBC and JDBC Guide

Persistent Stored Modules (Stored Procedures) : PSM

Using SQL Server Management Studio

for Networks Installation Guide for the application on a server September 2015 (GUIDE 2) Memory Booster version 1.3-N and later

Module 15: Monitoring

Installing the BlackBerry Enterprise Server Management console with a remote database

MOC 20461C: Querying Microsoft SQL Server. Course Overview

INTRODUCTION: SQL SERVER ACCESS / LOGIN ACCOUNT INFO:


Automation Engine 14. Troubleshooting

Configuring SQL Server Lock (Block) Monitoring With Sentry-go Quick & Plus! monitors

Migrating helpdesk to a new server

Microsoft SQL Server OLTP Best Practice

Database Concepts (3 rd Edition) APPENDIX D Getting Started with Microsoft Access 2007

Capturing & Processing Incoming s

Database Assistant. Once Database Assistant is installed you must login to gain access to the database. Copyright 2009

Setting Up Your Team-SQL Database for ORACLE 8.05

Oracle Database 10g Express

Dynamics NAV/SQL Server Configuration Recommendations

DataFlex Connectivity Kit For ODBC User's Guide. Version 2.2

Moving the Web Security Log Database

Search help. More on Office.com: images templates

for Networks Installation Guide for the application on the server July 2014 (GUIDE 2) Lucid Rapid Version 6.05-N and later

Guidelines for Installing SQL Server and Client (SQL Server Management Studio)

ASP.NET Programming with C# and SQL Server

Using ELM Reports in WhatsUp Gold. This guide provides information about configuring ELM reports in WhatsUp Gold v15.0

SQL NULL s, Constraints, Triggers

Moving the TRITON Reporting Databases

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

How to Install SQL to the Local Machine from the Server

CONTACTS SYNCHRONIZER FOR IPAD USER GUIDE

1) How To Make Private Server With Detail.

Using Temporary Tables to Improve Performance for SQL Data Services

FileMaker 14. ODBC and JDBC Guide

CHAPTER 23: USING ODBC

Module 1: Getting Started with Databases and Transact-SQL in SQL Server 2008

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

Guide to Upsizing from Access to SQL Server

Consulting. Personal Attention, Expert Assistance

Database Administration with MySQL

"SQL Database Professional " module PRINTED MANUAL

How To Understand The Error Codes On A Crystal Reports Print Engine

Using SQL Database Mirroring to Improve Citrix XenApp Server Farm Disaster Recovery Capabilities

Microsoft Dynamics GP econnect Installation and Administration Guide

Oracle Forms Services Secure Web.Show_Document() calls to Oracle Reports

RSA Security Analytics

Web Security Log Server Error Reference

14 Triggers / Embedded SQL

Microsoft Office 2010

Abstract. For notes detailing the changes in each release, see the MySQL for Excel Release Notes. For legal information, see the Legal Notices.

Microsoft Lync Ignite. Microsoft Lync 2013

About This Document 3. About the Migration Process 4. Requirements and Prerequisites 5. Requirements... 5 Prerequisites... 5

Module 9: Implementing Stored Procedures

The manual contains complete instructions on 'converting' your data to version 4.21.

Jet Data Manager 2012 User Guide

Nortel Networks Symposium Call Center Server Symposium Database Integration User s Guide

When you publish data to a SharePoint site, you first

Oracle Database 10g: Introduction to SQL

Working with SQL Server Integration Services

How to Connect to CDL SQL Server Database via Internet

In this topic we will cover the security functionality provided with SAP Business One.

Optimizing the Performance of the Oracle BI Applications using Oracle Datawarehousing Features and Oracle DAC

Incremental Replication from SQL Server to Oracle. Kane McKenzie Liberty University

The MineSight Planning Database

Web UTAS. Common problems and solutions. Common problems with Java settings. Ensuring that you have the correct Java settings in place

Transcription:

Creating Custom Error Messages in SQL Server and using with VFP SQL Server enables us to create and call our own error messages. We use these messages to keep our business logic traceable against violations. Both system and user defined error messages store by SQL server in the sysmessages table within master database. Creating Custom Error Messages We use sp_addmessage stored procedure to add custom error messages as following: EXEC sp_addmessage @msgnum = number, @severity = severity_level, @msgtext = ' Text of error message.', @with_log = 'true' or 'false' We need to have attention to the following points when we are going to create a custom message: Custom message numbers can be considered from 50000 and higher Severity level is from 0 to 25 Only system administrators can create error messages with a severity level greater than 19 Use the @with_log option to control whether or not SQL Server records the error in the Windows Application log Severity Levels: 0 through 10 These messages are considered informational (Info or Warnings normally I use 10 for this purpose). 11 through 16 These errors are correctable by the user. 17 This error shows insufficient resources (such as locks or disk space). 18 This error number is Non fatal internal error. These errors usually indicate an internal software problem. 19 This one is refer to an internal non-configurable limit in SQL Server which was exceeded. 20 through 25 These are fatal errors!!! Error messages with a severity greater than 20 will be considered as fatal with SQL Server and terminates the client s connection to the server. We can use 15 as the severity level for warning messages and 16 and higher as the severity level for errors. Dropping Custom Error Messages To remove a custom error message we use the sp_dropmessage stored procedure. The syntax is as following:

EXEC sp_dropmessage custom_message_number Replace custom_message_number with the number of the custom error message you want to delete from the sysmessages table. When an error is raised, the error number is placed in the @@ERROR function, which stores the most recently generated error number. The @@ERROR system function returns 0 if the last Transact-SQL statement executed successfully; if the statement generated an error, @@ERROR returns the error number. Also, @@ERROR is raised only for errors, not for warnings. @@ERROR is set to 0 by default for messages with a severity from 1 through 10. Using Custom Error Messages We can raise custom error messages by RAISEERROR which its syntax is as following: RAISERROR ( msg_id msg_txt, severity_level, state) [WITH LOG] We will replace msg_id with the ID number of our created custom error message; remember that it s stored in sysmessages table in master database. On the other hand we can optionally call RAISERROR to display a new message by specifying a message text which we need at that moment (We must also specify the severity level and state.) but we know that the instant message will not store by SQL Server to reuse. The state is an arbitrary number from 1 to 127 that you can use It s optionally to use the WITH LOG keywords with the RAISERROR statement to log the message in Windows Application log. To view these messages in Windows Application log we call Event Viewer from Administrative tools in control panel or by running it at %SystemRoot%\system32\eventvwr.msc /s. To have automatic logging of error message by SQL server we can set the @with_log option to true when we defined the custom error message by using the sp_addmessage stored procedure. Let s try a practical example: We write the codes in two parts, 1- First part would be through using SQL Query Analyzer to create custom error message and necessary store procedure. Defaults: *** To have a common sample, we use titles, and publishers tables in pubs database in Microsoft SQL server We want to delete a publisher if there is no any book title available in our titles table from that publisher. /* to check availability of error messages with number higher than 50000, we check sysmessage table in master database. */

Use master Select * from sysmessages where error > 50000 /* if we found any message in the above query result, we will go to drop them */ Exec sp_dropmessage 50001 Exec sp_dropmessage 50002 /* to add our error messages to SQL Server system message table we us sp_addmessage store procedure, because message texts are clearly describe the purpose of our error messages, I don t make more headache for you with extra explanations! */ @msgnum=50001, @msgtext='publisher with name of %s is not available in Publishers table!', @msgnum=50002, @msgtext='not possible to delete %s because of availability of book(s) from this publisher in Titles table!', @msgnum=50003, @msgtext='%s successfully deleted from Publishers table!', /* to use pubs tables we switch there */ Use pubs /* to make a utility to delete a publisher with attention with its name, we create a store procedure. - First we check availability of name of the mentioned publisher in related table, if we don t have a publisher name in our table then we can t delete any one and we come up with an error message. - Second, now that we found the mentioned publisher ID, we will go to check between book titles to see if any of titles has been published with this publisher then we need to keep our integrity and we can t make it removed and just we alert it with a RAISERROR. - Finally we are ready to delete the publisher and we use an informative message for this purpose in Application log to make able for future traces. Note: @@ROWCOUNT function returns the number of rows affected by the last statement, @@ROWCOUNT is a system function. */ Create Procedure dbo.deletepublisher @PublisherName varchar(40) = NULL

AS Declare @PublisherID char(4) Select @PublisherID = pub_id from publishers where pub_name = @PublisherName IF @@ROWCOUNT = 0 BEGIN Raiserror(50001, 11, 1, @PublisherName) END IF EXISTS (SELECT pub_id FROM titles WHERE pub_id = @PublisherID) BEGIN Raiserror (50002, 11, 1, @PublisherName) END DELETE FROM publishers WHERE pub_id = @PublisherID Raiserror (50003, 11, 1, @PublisherName) VFP PART: 2- Now to use the store procedure in Visual FoxPro we make a connection with SQL server, then we change current database to Pubs and finally call the function in SQL pass through method. Defaults: *** Please consider that the DSN in ODBC data source is FBSQL, use your own DSN to connect to your SQL server. lnsqlconn = SQLCONNECT("FBSQL") && we use pubs database: SQLEXEC( lnsqlconn, "Use Pubs") && We need to add a new record to publishers table which don t have any reference && in titles table to make us able to check deleting. && We know pubs database, there is a constraint as relation between publishers && and titles tables, it will not allow us to add a record in publishers && then temporary we make it disable as following: SqlExec(lnSqlConn, "alter table publishers nocheck constraint all") && Now we add a record for VFP Books publishing virtual publisher SqlExec(lnSqlConn, "Insert into publishers values ( '1111', 'VFP Books Publishing', 'Richmond Hill', 'ON', 'Canada')") && Then we check the store procedure with 3 different publisher names && SqlExec command will bring -1 to show as error has been happened SqlExec(lnSqlConn, "Exec dbo.deletepublisher ' None exist publisher'") SqlExec(lnSqlConn, "Exec dbo.deletepublisher ' New Moon Books'") SqlExec(lnSqlConn, "Exec dbo.deletepublisher 'VFP Books Publishing'") && At last, we check our application log through Event Viewer && and we see the related error messages at there!

In real world we can use the custom message to log activities related to special happening by RAISERROR. In SQL Query Analyzer environment, when we encounter to RAISERROR, we can see the message in Messages tabs as an interactive message viewer part of the utility. Farhad Bayanati fbayanati@yahoo.com (from the VFUG Forum)