如果在insert语句中给出了要插入记录的各个字段名,那么各字段值的顺序只需与各个字段名的顺序一致,与表中的顺序(desc命令查看)可以不同。但是当使用缩写格式时,各字段值的顺序必须与表中的顺序一致。例如,用缩写格式完成上例记录的插入,则使用“insertintostudentvalues(?0213010?,?zhanghan?,default,19870202,?math?”命令。
建议你建一个触发器,在插入数据之前触发即可实现你要的功能。
示例代码如下:
mysql> CREATE TABLE tb(BH CHAR(16),content VARCHAR(20),`date` DATETIME,val INT)Query OK, 0 rows affected (0.05 sec)
mysql>
mysql>
mysql> DELIMITER $$
mysql> DROP TRIGGER IF EXISTS tri_NewBH $$
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
mysql> CREATE TRIGGER tri_NewBH BEFORE INSERT ON tb
-> FOR EACH ROW
-> BEGIN
-> DECLARE dt CHAR(8)
-> DECLARE bh_id CHAR(16)
-> DECLARE number INT
-> DECLARE new_bh VARCHAR(16)
->
-> SET dt = DATE_FORMAT(CURDATE(),'%Y%m%d')
->
-> SELECT
-> MAX(BH) INTO bh_id
-> FROM tb
-> WHERE BH LIKE CONCAT(dt,'%')
->
-> IF bh_id = '' OR bh_id IS NULL THEN
-> SET new_bh = CONCAT(dt,'00000001')
-> ELSE
-> SET number = RIGHT(bh_id,8) + 1
-> SET new_bh = RIGHT(CONCAT('00000000',number),8)
-> SET new_bh=CONCAT(dt,new_bh)
-> END IF
->
-> SET NEW.BH = new_bh
-> END$$
Query OK, 0 rows affected (0.09 sec)
mysql>
mysql> DELIMITER
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20)
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20)
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20)
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20)
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20)
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20)
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20)
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM tb
+------------------+---------+---------------------+------+
| BH | content | date | val |
+------------------+---------+---------------------+------+
| 2009051100000001 | LiangCK | 2009-05-11 00:00:00 | 20 |
| 2009051100000002 | LiangCK | 2009-05-11 00:00:00 | 20 |
| 2009051100000003 | LiangCK | 2009-05-11 00:00:00 | 20 |
| 2009051100000004 | LiangCK | 2009-05-11 00:00:00 | 20 |
| 2011051200000001 | LiangCK | 2009-05-11 00:00:00 | 20 |
| 2011051200000002 | LiangCK | 2009-05-11 00:00:00 | 20 |
| 2011051200000003 | LiangCK | 2009-05-11 00:00:00 | 20 |
+------------------+---------+---------------------+------+
7 rows in set (0.00 sec)
系统报错应该不是GetCollect()来读SELECT出来的字段信息为空的时候报错,而应该是将读取出来的值赋给变量的时候出错。可以这样做:
_variant_t vUsername = m_pRecordset->GetCollect("username")///取得username字段的值
if(vUsername.vt != VT_NULL){
CString szUsername = (LPCSTR)_bstr_t(vUsername)
}
这样应该就可以了的,我都是这样处理的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)