使用sp_addmessage添加自定义消息

本文介绍如何在SQL Server中使用sp_addmessage过程添加自定义错误消息,并提供了多个示例来展示不同场景下如何设置消息的文本、语言及严重级别等。

将新的用户定义错误消息存储在 SQL Server Database Engine 实例中。使用 sp_addmessage 存储的消息可以使用 sys.messages 目录视图进行查看。

语法:

sp_addmessage [ @msgnum = ] msg_id ,     [ @severity = ] severity , [ @msgtext = ] 'msg'       [ , [ @lang = ] 'language' ]  
     [ , [ @with_log = ] 'with_log' ]       [ , [ @replace = ] 'replace' ] 

备注:

对于非英语版本的 SQL Server,必须已经存在美国英语版本的消息,然后才能使用另一种语言添加消息。两种消息版本的严重性必须匹配。  当本地化包含参数的消息时,使用与原始消息中的参数相应的参数。在每个参数后都插入感叹号 (!)。

参数:

[ @msgnum = ] msg_id 
消息的 ID。msg_id 的数据类型为 int,默认值为 NULL。用户定义错误消息的 msg_id 可以是 50,001 和 
2,147,483,647 之间的整数。msg_id 和 language 的组合必须是唯一的;如果特定语言的 ID 已经存在,则返回错误。

[ @severity = ] severity 
错误的严重级别。severity 的数据类型为 smallint,默认值为 NULL。有效级别从 1 到 25。有关严重性的详细信息,请参阅数据库引擎错误严重性。

[ @msgtext = ] 'msg' 
错误消息的文本。msg 的数据类型为 nvarchar(255),默认值为 NULL。  [ @lang = ] 'language'该消息的语言。language 的数据类型为 sysname,默认值为 NULL。由于可以在同一个服务器上安装多种语言,因此,language 指定编写每个消息时所用的语言。如果省略 language,则会话语言为默认语言。

[ @with_log = ] { 'TRUE' | 'FALSE' ] } 
是否在消息发生时将其写入 Windows 应用程序日志。@with_log 的数据类型为 varchar(5),默认值为 FALSE。如果为 TRUE,则错误始终写入 Windows 应用程序日志。如果为 FALSE,则错误不会始终写入 Windows 应用程序日志,但仍然可以写入,具体取决于错误是如何引发的。只有 sysadmin 服务器角色的成员才能使用该选项。

如果消息写入了 Windows 应用程序日志,那么它也将被写入数据库引擎 错误日志文件。

[ @replace = ] 'replace' 
如果指定为字符串 replace,则会以新的消息正文和严重级别覆盖现有错误消息。@replace 为 varchar(7),默认值为 NULL。如果 msg_id 已经存在,则必须指定该选项。如果替换了美国英语消息,则会以具有相同 msg_id 的所有其他语言替换所有消息的严重级别。

返回代码值:

0(成功)或 1(失败)

结果集:

权限:

要求具有 sysadmin 和 serveradmin 固定服务器角色的成员身份。 

示例:

A. 定义自定义的消息 
以下示例将自定义的消息添加到 sys.messages。

USE master GO 
EXEC sp_addmessage 50001, 16,  
   N'Percentage expects a value between 20 and 100.     Please reexecute with a more appropriate value.'; GO

B. 用两种语言添加消息

以下示例首先用美国英语添加一条消息,然后用法语添加相同的消息。

USE master 
GO 

EXEC sp_addmessage @msgnum = 60000, @severity = 16,     @msgtext = N'The item named %s already exists in %s.',     @lang = 'us_english';  
EXEC sp_addmessage @msgnum = 60000, @severity = 16,     @msgtext = N'L''élément nomm?%1! existe déj?dans %2!',  
   @lang = 'French'; GO

C. 更改参数顺序

以下示例首先添加美国英语的消息,然后添加更改了其参数顺序的本地化消息。

USEmaster; GO  
EXEC sp_addmessage      @msgnum = 60000,      @severity = 16,     @msgtext =  
        N'This is a test message with one numeric         parameter (%d), one string parameter (%s),          and another string parameter (%s).',     @lang = 'us_english';  
EXEC sp_addmessage      @msgnum = 60000,      @severity = 16,     @msgtext =  
        -- In the localized version of the message,         -- the parameter order has changed. The          -- string parameters are first and second         -- place in the message, and the numeric          -- parameter is third place. 
        N'Dies ist eine Testmeldung mit einem          Zeichenfolgenparameter (%3!), 
        einem weiteren Zeichenfolgenparameter (%2!),und einem numerischen Parameter (%1!).',     @lang = 'German'; GO    -- Changing the session language to use the U.S. English -- version of the error message. SET LANGUAGE us_english; GO  
RAISERROR(60000,1,1,15,'param1','param2') -- error, severity, state, 
GO                                       -- parameters.  
-- Changing the session language to use the German -- version of the error message. SET LANGUAGE German; GO  
RAISERROR(60000,1,1,15,'param1','param2') -- error, severity, state,  
GO       


/** * Copyright (c) 2019, Freqchip * * All rights reserved. * * */ /* * INCLUDES (包含头文件) */ #include <stdio.h> #include <string.h> #include "co_printf.h" #include "gap_api.h" #include "gatt_api.h" #include "gatt_sig_uuid.h" #include "simple_gatt_service.h" /* * MACROS (宏定义) */ /* * CONSTANTS (常量定义) */ // Simple GATT Profile Service UUID: 0xFFF0 const uint8_t sp_svc_uuid[] = UUID16_ARR(SP_SVC_UUID); /******************************* Characteristic 1 defination *******************************/ // Characteristic 1 UUID: 0xFFF1 // Characteristic 1 data #define SP_CHAR1_VALUE_LEN 20 uint8_t sp_char1_value[SP_CHAR1_VALUE_LEN] = {0}; // Characteristic 1 User Description #define SP_CHAR1_DESC_LEN 17 const uint8_t sp_char1_desc[SP_CHAR1_DESC_LEN] = "Characteristic 1"; /******************************* Characteristic 2 defination *******************************/ // Characteristic 2 UUID: 0xFFF2 // Characteristic 2 data #define SP_CHAR2_VALUE_LEN 20 uint8_t sp_char2_value[SP_CHAR2_VALUE_LEN] = {0}; // Characteristic 2 User Description #define SP_CHAR2_DESC_LEN 17 const uint8_t sp_char2_desc[SP_CHAR2_DESC_LEN] = "Characteristic 2"; /******************************* Characteristic 3 defination *******************************/ // Characteristic 3 UUID: 0xFFF3 // Characteristic 3 data #define SP_CHAR3_VALUE_LEN 30 uint8_t sp_char3_value[SP_CHAR3_VALUE_LEN] = {0}; // Characteristic 3 User Description #define SP_CHAR3_DESC_LEN 17 const uint8_t sp_char3_desc[SP_CHAR3_DESC_LEN] = "Characteristic 3"; /******************************* Characteristic 4 defination *******************************/ // Characteristic 4 UUID: 0xFFF4 // Characteristic 4 data #define SP_CHAR4_VALUE_LEN 40 uint8_t sp_char4_value[SP_CHAR4_VALUE_LEN] = {0}; // Characteristic 4 client characteristic configuration #define SP_CHAR4_CCC_LEN 2 uint8_t sp_char4_ccc[SP_CHAR4_CCC_LEN] = {0}; // Characteristic 4 User Description #define SP_CHAR4_DESC_LEN 17 const uint8_t sp_char4_desc[SP_CHAR4_DESC_LEN] = "Characteristic 4"; /******************************* Characteristic 5 defination *******************************/ // Characteristic 5 UUID: 0xFFF5 uint8_t sp_char5_uuid[UUID_SIZE_2] = { LO_UINT16(SP_CHAR5_UUID), HI_UINT16(SP_CHAR5_UUID) }; // Characteristic 5 data #define SP_CHAR5_VALUE_LEN 50 uint8_t sp_char5_value[SP_CHAR5_VALUE_LEN] = {0}; // Characteristic 5 User Description #define SP_CHAR5_DESC_LEN 17 const uint8_t sp_char5_desc[SP_CHAR5_DESC_LEN] = "Characteristic 5"; /* * TYPEDEFS (类型定义) */ /* * GLOBAL VARIABLES (全局变量) */ uint8_t sp_svc_id = 0; uint8_t ntf_char1_enable = 0; /* * LOCAL VARIABLES (本地变量) */ static gatt_service_t simple_profile_svc; /********************************************************************* * Profile Attributes - Table * 每一项都是一个attribute的定义。 * 第一个attribute为Service 的的定义。 * 每一个特征值(characteristic)的定义,都至少包含三个attribute的定义; * 1. 特征值声明(Characteristic Declaration) * 2. 特征值的值(Characteristic value) * 3. 特征值描述符(Characteristic description) * 如果有notification 或者indication 的功能,则会包含四个attribute的定义,除了前面定义的三个,还会有一个特征值客户端配置(client characteristic configuration)。 * */ const gatt_attribute_t simple_profile_att_table[SP_IDX_NB] = { // Simple gatt Service Declaration [SP_IDX_SERVICE] = { { UUID_SIZE_2, UUID16_ARR(GATT_PRIMARY_SERVICE_UUID) }, /* UUID */ GATT_PROP_READ, /* Permissions */ UUID_SIZE_2, /* Max size of the value */ /* Service UUID size in service declaration */ (uint8_t*)sp_svc_uuid, /* Value of the attribute */ /* Service UUID value in service declaration */ }, // Characteristic 1 Declaration [SP_IDX_CHAR1_DECLARATION] = { { UUID_SIZE_2, UUID16_ARR(GATT_CHARACTER_UUID) }, /* UUID */ GATT_PROP_READ, /* Permissions */ 0, /* Max size of the value */ NULL, /* Value of the attribute */ }, // Characteristic 1 Value [SP_IDX_CHAR1_VALUE] = { { UUID_SIZE_16, SP_CHAR1_TX_UUID }, /* UUID */ GATT_PROP_READ | GATT_PROP_NOTI, /* Permissions */ SP_CHAR1_VALUE_LEN, /* Max size of the value */ NULL, /* Value of the attribute */ /* Can assign a buffer here, or can be assigned in the application by user */ }, // Characteristic 4 client characteristic configuration [SP_IDX_CHAR1_CFG] = { { UUID_SIZE_2, UUID16_ARR(GATT_CLIENT_CHAR_CFG_UUID) }, /* UUID */ GATT_PROP_READ | GATT_PROP_WRITE, /* Permissions */ 2, /* Max size of the value */ NULL, /* Value of the attribute */ /* Can assign a buffer here, or can be assigned in the application by user */ }, // Characteristic 1 User Description [SP_IDX_CHAR1_USER_DESCRIPTION] = { { UUID_SIZE_2, UUID16_ARR(GATT_CHAR_USER_DESC_UUID) }, /* UUID */ GATT_PROP_READ, /* Permissions */ SP_CHAR1_DESC_LEN, /* Max size of the value */ (uint8_t *)sp_char1_desc, /* Value of the attribute */ }, // Characteristic 2 Declaration [SP_IDX_CHAR2_DECLARATION] = { { UUID_SIZE_2, UUID16_ARR(GATT_CHARACTER_UUID) }, /* UUID */ GATT_PROP_READ, /* Permissions */ 0, /* Max size of the value */ NULL, /* Value of the attribute */ }, // Characteristic 2 Value [SP_IDX_CHAR2_VALUE] = { { UUID_SIZE_16, SP_CHAR2_RX_UUID }, /* UUID */ GATT_PROP_READ | GATT_PROP_WRITE, /* Permissions */ SP_CHAR2_VALUE_LEN, /* Max size of the value */ NULL, /* Value of the attribute */ /* Can assign a buffer here, or can be assigned in the application by user */ }, // Characteristic 2 User Description [SP_IDX_CHAR2_USER_DESCRIPTION] = { { UUID_SIZE_2, UUID16_ARR(GATT_CHAR_USER_DESC_UUID) }, /* UUID */ GATT_PROP_READ, /* Permissions */ SP_CHAR2_DESC_LEN, /* Max size of the value */ (uint8_t *)sp_char2_desc, /* Value of the attribute */ }, // Characteristic 3 Declaration [SP_IDX_CHAR3_DECLARATION] = { { UUID_SIZE_2, UUID16_ARR(GATT_CHARACTER_UUID) }, /* UUID */ GATT_PROP_READ, /* Permissions */ 0, /* Max size of the value */ NULL, /* Value of the attribute */ }, // Characteristic 3 Value [SP_IDX_CHAR3_VALUE] = { { UUID_SIZE_2, UUID16_ARR(SP_CHAR3_UUID) }, /* UUID */ GATT_PROP_WRITE, /* Permissions */ SP_CHAR3_VALUE_LEN, /* Max size of the value */ NULL, /* Value of the attribute */ /* Can assign a buffer here, or can be assigned in the application by user */ }, // Characteristic 3 User Description [SP_IDX_CHAR3_USER_DESCRIPTION] = { { UUID_SIZE_2, UUID16_ARR(GATT_CHAR_USER_DESC_UUID) }, /* UUID */ GATT_PROP_READ, /* Permissions */ SP_CHAR3_DESC_LEN, /* Max size of the value */ (uint8_t *)sp_char3_desc, /* Value of the attribute */ }, // Characteristic 4 Declaration [SP_IDX_CHAR4_DECLARATION] = { { UUID_SIZE_2, UUID16_ARR(GATT_CHARACTER_UUID) }, /* UUID */ GATT_PROP_READ, /* Permissions */ 0, /* Max size of the value */ NULL, /* Value of the attribute */ }, // Characteristic 4 Value [SP_IDX_CHAR4_VALUE] = { { UUID_SIZE_2, UUID16_ARR(SP_CHAR4_UUID) }, /* UUID */ GATT_PROP_WRITE | GATT_PROP_NOTI, /* Permissions */ SP_CHAR4_VALUE_LEN, /* Max size of the value */ NULL, /* Value of the attribute */ /* Can assign a buffer here, or can be assigned in the application by user */ }, // Characteristic 4 client characteristic configuration [SP_IDX_CHAR4_CFG] = { { UUID_SIZE_2, UUID16_ARR(GATT_CLIENT_CHAR_CFG_UUID) }, /* UUID */ GATT_PROP_READ | GATT_PROP_WRITE, /* Permissions */ SP_CHAR4_CCC_LEN, /* Max size of the value */ NULL, /* Value of the attribute */ /* Can assign a buffer here, or can be assigned in the application by user */ }, // Characteristic 4 User Description [SP_IDX_CHAR4_USER_DESCRIPTION] = { { UUID_SIZE_2, UUID16_ARR(GATT_CHAR_USER_DESC_UUID) }, /* UUID */ GATT_PROP_READ, /* Permissions */ SP_CHAR4_DESC_LEN, /* Max size of the value */ (uint8_t *)sp_char4_desc, /* Value of the attribute */ }, // Characteristic 5 Declaration [SP_IDX_CHAR5_DECLARATION] = { { UUID_SIZE_2, UUID16_ARR(GATT_CHARACTER_UUID) }, /* UUID */ GATT_PROP_READ, /* Permissions */ 0, /* Max size of the value */ NULL, /* Value of the attribute */ }, // Characteristic 5 Value [SP_IDX_CHAR5_VALUE] = { { UUID_SIZE_2, UUID16_ARR(SP_CHAR5_UUID) }, /* UUID */ GATT_PROP_READ | GATT_PROP_WRITE, /* Permissions */ SP_CHAR5_VALUE_LEN, /* Max size of the value */ NULL, /* Value of the attribute */ /* Can assign a buffer here, or can be assigned in the application by user */ }, // Characteristic 5 User Description [SP_IDX_CHAR5_USER_DESCRIPTION] = { { UUID_SIZE_2, UUID16_ARR(GATT_CHAR_USER_DESC_UUID) }, /* UUID */ GATT_PROP_READ, /* Permissions */ SP_CHAR5_DESC_LEN, /* Max size of the value */ (uint8_t *)sp_char5_desc, /* Value of the attribute */ }, }; void show_reg(uint8_t *data,uint32_t len,uint8_t dbg_on) { uint32_t i=0; if(len == 0 || (dbg_on==0)) return; for(; i<len; i++) { co_printf("0x%02X,",data[i]); } co_printf("\r\n"); } void ntf_data(uint8_t con_idx,uint8_t att_idx,uint8_t *data,uint16_t len) { gatt_ntf_t ntf_att; ntf_att.att_idx = att_idx; ntf_att.conidx = con_idx; ntf_att.svc_id = sp_svc_id; ntf_att.data_len = len; ntf_att.p_data = data; gatt_notification(ntf_att); } /********************************************************************* * @fn sp_gatt_msg_handler * * @brief Simple Profile callback funtion for GATT messages. GATT read/write * operations are handeled here. * * @param p_msg - GATT messages from GATT layer. * * @return uint16_t - Length of handled message. */ static uint16_t sp_gatt_msg_handler(gatt_msg_t *p_msg) { switch(p_msg->msg_evt) { case GATTC_MSG_READ_REQ: { if(p_msg->att_idx == SP_IDX_CHAR1_VALUE) { memcpy(p_msg->param.msg.p_msg_data, "CHAR1_VALUE", strlen("CHAR1_VALUE")); return strlen("CHAR1_VALUE"); } else if(p_msg->att_idx == SP_IDX_CHAR2_VALUE) { memcpy(p_msg->param.msg.p_msg_data, "CHAR2_VALUE", strlen("CHAR2_VALUE")); return strlen("CHAR2_VALUE"); } else if(p_msg->att_idx == SP_IDX_CHAR4_CFG) { memcpy(p_msg->param.msg.p_msg_data, sp_char4_ccc, 2); return 2; } else if(p_msg->att_idx == SP_IDX_CHAR5_VALUE) { memcpy(p_msg->param.msg.p_msg_data, "CHAR5_VALUE", strlen("CHAR5_VALUE")); return strlen("CHAR5_VALUE"); } } break; case GATTC_MSG_WRITE_REQ: { if (p_msg->att_idx == SP_IDX_CHAR1_VALUE) { co_printf("char1_recv:"); show_reg(p_msg->param.msg.p_msg_data,p_msg->param.msg.msg_len,1); } else if (p_msg->att_idx == SP_IDX_CHAR2_VALUE) { co_printf("char2_recv:"); show_reg(p_msg->param.msg.p_msg_data,p_msg->param.msg.msg_len,1); } else if (p_msg->att_idx == SP_IDX_CHAR3_VALUE) { co_printf("char3_recv:"); show_reg(p_msg->param.msg.p_msg_data,p_msg->param.msg.msg_len,1); } else if (p_msg->att_idx == SP_IDX_CHAR5_VALUE) { co_printf("char5_recv:"); show_reg(p_msg->param.msg.p_msg_data,p_msg->param.msg.msg_len,1); } else if (p_msg->att_idx == SP_IDX_CHAR1_CFG) { co_printf("char1_ntf_enable:"); show_reg(p_msg->param.msg.p_msg_data,p_msg->param.msg.msg_len,1); if(p_msg->param.msg.p_msg_data[0] & 0x1) ntf_char1_enable = 1; if(ntf_char1_enable) ntf_data(p_msg->conn_idx,SP_IDX_CHAR1_VALUE,(uint8_t *)"char1_ntf_data",strlen("char1_ntf_data")); } else if (p_msg->att_idx == SP_IDX_CHAR4_CFG) { co_printf("char4_ntf_enable:"); show_reg(p_msg->param.msg.p_msg_data,p_msg->param.msg.msg_len,1); memcpy(sp_char4_ccc,p_msg->param.msg.p_msg_data,2); } } break; case GATTC_MSG_LINK_CREATE: co_printf("link_created\r\n"); break; case GATTC_MSG_LINK_LOST: co_printf("link_lost\r\n"); ntf_char1_enable = 0; break; default: break; } return p_msg->param.msg.msg_len; } /********************************************************************* * @fn sp_gatt_add_service * * @brief Simple Profile add GATT service function. * 添加GATT service到ATT的数据库里面。 * * @param None. * * * @return None. */ void sp_gatt_add_service(void) { simple_profile_svc.p_att_tb = simple_profile_att_table; simple_profile_svc.att_nb = SP_IDX_NB; simple_profile_svc.gatt_msg_handler = sp_gatt_msg_handler; sp_svc_id = gatt_add_service(&simple_profile_svc); } 解释
最新发布
06-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值