訂單創建序列號規則

package com.people;

import java.text.SimpleDateFormat;

/**
 * @author zhaozhiqiang
 *
 */
public class CreateOrder {
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");

	public static void main(String[] args) {
		String createOrderCode = createOrderCode();
		System.out.println(createOrderCode);
	}
   /**
    *生成訂單編號
    *訂單編號規則:年月日+訂單數+業務類型+樓宇編號(12位) 年月日:171213(6位) 訂單數:4位(每天從0計數) 業務類型:1空間 2律師
    * @return
    */
	public static String createOrderCode() {
		// 獲取序列號
		// SELECT nextval('zzq') FROM DUAL  sql語句
		Integer code = 1;
		String codeStr = code.toString();
		if (codeStr.length() < 5) {
			codeStr = String.format("%05d", code);
			System.out.println(codeStr);
		}
		StringBuilder orderCode = new StringBuilder();
		// 訂單類型默認1
		String businessCode = "1";
		orderCode.append(dateFormat.format(DateUtil.getNow()));// 年月日:171213(6位)
		System.out.println(dateFormat.format(DateUtil.getNow()));
		orderCode.append(codeStr);// 訂單數:5位(每天從0計數)
		orderCode.append(businessCode);
		return orderCode.toString();
	}
}
package com.people;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 
 * @author zhaozhiqiang
 *
 */
public class DateUtil {
	public static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

	/**
	 * 獲取當前日期及時間
	 *
	 * @return 返回當前日期及時間
	 */
	public static Date getNow() {
		return Calendar.getInstance().getTime();
	}

	/**
	 * 獲取兩個日期中較小的日期
	 *
	 * @param date1
	 *            日期1
	 * @param date2
	 *            日期2
	 * @return 返回較小的日期
	 */
	public static Date getSmallDate(Date date1, Date date2) {
		return date1.compareTo(date2) < 0 ? date1 : date2;
	}

	/**
	 * 獲取兩個日期中較大的日期
	 *
	 * @param date1
	 *            日期1
	 * @param date2
	 *            日期2
	 * @return 返回較大的日期
	 */
	public static Date getBigDate(Date date1, Date date2) {
		return date1.compareTo(date2) > 0 ? date1 : date2;
	}

	/**
	 * 在指定的日期上增加年數
	 *
	 * @param yearAmount
	 *            年數
	 * @param date
	 *            指定日期
	 * @return 返回增加月數後的日期
	 */
	public static Date addYear2Date(int yearAmount, Date date) {
		Date newDate = null;
		if (date != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.YEAR, yearAmount);
			newDate = calendar.getTime();
		}
		return newDate;
	}

	/**
	 * 在指定的日期上增加月數
	 *
	 * @param monthAmount
	 *            月數
	 * @param date
	 *            指定日期
	 * @return 返回增加月數後的日期
	 */
	public static Date addMonth2Date(int monthAmount, Date date) {
		Date newDate = null;
		if (date != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, monthAmount);
			newDate = calendar.getTime();
		}
		return newDate;
	}

	/**
	 * 在指定的日期上增加天數
	 *
	 * @param dayAmount
	 *            天數
	 * @param date
	 *            指定日期
	 * @return 返回增加天數後的日期
	 */
	public static Date addDay2Date(int dayAmount, Date date) {
		Date newDate = null;
		if (date != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_MONTH, dayAmount);
			newDate = calendar.getTime();
		}
		return newDate;
	}

	/**
	 * 在指定的日期上增加小時數
	 *
	 * @param hourAmount
	 *            小時數
	 * @param date
	 *            指定日期
	 * @return 返回增加小時數後的日期
	 */
	public static Date addHour2Date(int hourAmount, Date date) {
		Date newDate = null;
		if (date != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.HOUR_OF_DAY, hourAmount);
			newDate = calendar.getTime();
		}
		return newDate;
	}

	/**
	 * 在指定的日期上增加分鐘數
	 *
	 * @param minuteAmount
	 *            分鐘數
	 * @param date
	 *            指定日期
	 * @return 返回增加分鐘數後的日期
	 */
	public static Date addMinute2Date(int minuteAmount, Date date) {
		Date newDate = null;
		if (date != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.MINUTE, minuteAmount);
			newDate = calendar.getTime();
		}
		return newDate;
	}

	/**
	 * 將日期轉換成指定格式的字符串
	 *
	 * @param format
	 *            時間表現形式,例如:"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"等
	 * @param date
	 *            待格式化的日期
	 * @return 返回格式化後的日期字符串
	 */
	public static String formatDate(String format, Date date) {
		return formatDate(format, date, "");
	}

	/**
	 * 將日期轉換成指定格式的字符串
	 *
	 * @param format
	 *            時間表現形式,例如:"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"等
	 * @param date
	 *            待格式化的日期
	 * @param nullString
	 *            空日期的替換字符,滿足特殊需要
	 * @return 返回格式化後的日期字符串
	 */
	public static String formatDate(String format, Date date, String nullString) {
		String formatStr = nullString;

		if (date != null) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			formatStr = simpleDateFormat.format(date);
		}

		return formatStr;
	}

	/**
	 * 將日期轉換成"yyyy-MM-dd HH:mm:ss"格式的字符串
	 *
	 * @param date
	 *            待格式化的日期
	 * @return 返回格式化後的日期字符串
	 */
	public static String formatDateTime(Date date) {
		String formatStr = "";

		if (date != null) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			formatStr = simpleDateFormat.format(date);
		}

		return formatStr;
	}
	/**
	 * 將日期轉換成"yyyy-MM-dd HH:mm"格式的字符串
	 *
	 * @param date
	 *            待格式化的日期
	 * @return 返回格式化後的日期字符串
	 */
	public static String formatDateTimeTwo(Date date) {
		String formatStr = "";

		if (date != null) {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm");
			formatStr = simpleDateFormat.format(date);
		}

		return formatStr;
	}
	/**
	 * 將字符串解析成年月日期類型,如果字符串含有/則按/分割,否則按-分割
	 *
	 * @param dateYMStr
	 *            待解析的字符串
	 * @return 返回解析後的日期
	 */
	public static Date getDateYM(String dateYMStr) {
		Date date = null;
		try {
			if (dateYMStr != null) {
				String separator = dateYMStr.indexOf('/') > 0 ? "/" : "-";
				DateFormat dateFormat = new SimpleDateFormat("yyyy" + separator
						+ "MM");
				date = dateFormat.parse(dateYMStr);
			}
		} catch (ParseException parse) {
		}
		return date;
	}

	/**
	 * 將字符串解析成年月日日期類型,如果字符串含有/則按/分割,否則按-分割
	 *
	 * @param dateStr
	 *            待解析的字符串
	 * @return 返回解析後的日期
	 */
	public static Date getDate(String dateStr) {
		Date date = null;
		try {
			if (dateStr != null) {
				String separator = dateStr.indexOf('/') > 0 ? "/" : "-";
				DateFormat dateFormat = new SimpleDateFormat("yyyy" + separator
						+ "MM" + separator + "dd");
				date = dateFormat.parse(dateStr);
			}
		} catch (ParseException parse) {
		}
		return date;
	}

	/**
	 * 將字符串解析成日期類型,格式自定
	 *
	 * @param dateStr
	 *            待解析的字符串
	 * @return 返回解析後的日期
	 */
	public static Date getDate(String dateStr, String formatStr) {
		Date date = null;
		try {
			if (dateStr != null) {
				DateFormat dateFormat = new SimpleDateFormat(formatStr);
				date = dateFormat.parse(dateStr);
			}
		} catch (ParseException parse) {
		}
		return date;
	}

	/**
	 * 將字符串解析成年月日時分秒日期時間類型,如果字符串含有/則按/分割,否則以-分
	 *
	 * @param dateTimeStr
	 *            待解析的字符串
	 * @return 返回解析後的日期
	 */
	public static Date getDateTime(String dateTimeStr) {
		Date date = null;
		try {
			String separator = dateTimeStr.indexOf('/') > 0 ? "/" : "-";
			DateFormat dateFormat = new SimpleDateFormat("yyyy" + separator
					+ "MM" + separator + "dd HH:mm:ss");
			date = dateFormat.parse(dateTimeStr);
		} catch (ParseException parse) {
		}
		return date;
	}

	/**
	 * 獲取指定日期是周幾
	 * 
	 * @param date
	 *            參數爲null時表示獲取當前日期是周幾
	 * @return
	 */
	public static String getWeekOfDate(Date date) {
		String[] weekOfDays = { "週日", "週一", "週二", "週三", "週四", "週五", "週六" };
		Calendar calendar = Calendar.getInstance();
		if (date != null)
			calendar.setTime(date);
		int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0) {
			w = 0;
		}
		return weekOfDays[w];
	}

	/**
	 * 獲取指定日期是星期幾
	 * 
	 * @param date
	 *            參數爲null時表示獲取當前日期是星期幾
	 * @return
	 */
	public static String getWeekOfDateTime(Date date) {
		String[] weekOfDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar calendar = Calendar.getInstance();
		if (date != null)
			calendar.setTime(date);
		int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0) {
			w = 0;
		}
		return weekOfDays[w];
	}

	/**
	 * 判斷日期是否是週末
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isWeekend(Date date) {
		Calendar calendar = Calendar.getInstance();
		if (date != null)
			calendar.setTime(date);
		int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (w == 0 || w == 6) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 獲取傳入日期的月份-日
	 *
	 * @param date
	 *            待解析的日期
	 * @return 月份-日
	 */
	public static String dateFormat(Date date) {
		String resultCode = "";
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DATE);
		if (month < 10) {
			resultCode += "0" + month;
		} else {
			resultCode += month;
		}
		resultCode += "-";
		if (day < 10) {
			resultCode += "0" + day;
		} else {
			resultCode += day;
		}
		return resultCode;
	}

	/**
	 * 獲取傳入日期的年份
	 *
	 * @param date
	 *            待解析的日期
	 * @return 返回該日期的年份
	 */
	public static int getYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.YEAR);
	}

	/**
	 * 獲取傳入日期的月份
	 *
	 * @param date
	 *            待解析的日期
	 * @return 返回該日期的月份
	 */
	public static int getMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MONTH) + 1;
	}

	/**
	 * 獲取傳入日期月份的日
	 *
	 * @param date
	 *            待解析的日期
	 * @return 返回該日期的日
	 */
	public static int getDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DATE);
	}

	/**
	 * 獲取傳入日期月份的小時 12小時制
	 *
	 * @return 返回該日期的小時
	 * @date 待解析的日期
	 */
	public static int getHour(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.HOUR);
	}

	/**
	 * 獲取傳入日期月份的小時 24小時制
	 *
	 * @return 返回該日期的小時
	 * @date 待解析的日期
	 */
	public static int getHourOfDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 獲取傳入日期月份的分鐘
	 *
	 * @return 返回該日期的分鐘
	 * @date 待解析的日期
	 */
	public static int getMinute(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MINUTE);
	}

	/**
	 * 兩個日期的年份差
	 *
	 * @param fromDate
	 *            起始日期
	 * @param toDate
	 *            結束日期
	 * @return 返回兩個日期的年份差,例1998-4-21~1999-6-21 相差1年2個月,返回1,
	 */
	public static int getDiffYears(Date fromDate, Date toDate) {
		int diffMonths = getDiffMonths(fromDate, toDate);
		int diffYears = diffMonths / 12;
		return diffYears;
	}

	/**
	 * 兩個日期的月份差
	 *
	 * @param fromDate
	 *            起始日期
	 * @param toDate
	 *            結束日期
	 * @return 返回兩個日期的月份差,例1998-4-21~1998-6-21 相差2個月,返回2
	 */
	public static int getDiffMonths(Date fromDate, Date toDate) {
		Calendar c = Calendar.getInstance();
		c.setTime(fromDate);
		int fromYear = c.get(Calendar.YEAR);
		int fromMonth = c.get(Calendar.MONTH) + 1;
		c.setTime(toDate);
		int toYear = c.get(Calendar.YEAR);
		int toMonth = c.get(Calendar.MONTH) + 1;
		int monthCount;

		if (toYear == fromYear) {
			monthCount = toMonth - fromMonth;
		} else if (toYear - fromYear == 1) {
			monthCount = 12 - fromMonth + toMonth;
		} else {
			monthCount = 12 - fromMonth + 12 * (toYear - fromYear - 1)
					+ toMonth;
		}
		return monthCount;
	}

	/**
	 * 兩個日期的天數差
	 *
	 * @param fromDate
	 *            起始日期
	 * @param toDate
	 *            結束日期
	 * @return 返回兩個日期的天數差,例1998-4-21~1998-4-25 相差4天,返回4
	 */
	public static int getDiffDays(Date fromDate, Date toDate) {
		return (int) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));
	}

	/**
	 * 兩個日期的小時差,自定義精度和舍入方式
	 *
	 * @param fromDate
	 *            起始日期
	 * @param toDate
	 *            結束日期
	 * @return 返回兩個日期的小時數
	 */
	public static double getDiffHours(Date fromDate, Date toDate) {
		// return Tools.numberDivide(toDate.getTime() - fromDate.getTime(),
		// (1000 * 60 * 60 ), scale, roundType);
		return (int) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60));
	}

	/**
	 * 兩個日期的分鐘差,自定義精度和舍入方式
	 *
	 * @param fromDate
	 *            起始日期
	 * @param toDate
	 *            結束日期
	 * @return 返回兩個日期的小時數
	 */
	public static double getDiffMinutes(Date fromDate, Date toDate) {
		// return Tools.numberDivide(toDate.getTime() - fromDate.getTime(),
		// (1000 * 60 * 60 ), scale, roundType);
		return (int) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60));
	}

	/**
	 * 兩個日期的秒數差
	 *
	 * @param fromDate
	 *            起始日期
	 * @param toDate
	 *            結束日期
	 * @return 返回兩個日期的秒數差,例1998-4-21 10:00:00~1998-4-21 10:00:50 相差50秒,返回50
	 */
	public static Long getDiffSeconds(Date fromDate, Date toDate) {
		Calendar fromCal = Calendar.getInstance();
		fromCal.setTime(fromDate);
		fromCal.set(Calendar.MILLISECOND, 0);

		Calendar toCal = Calendar.getInstance();
		toCal.setTime(toDate);
		toCal.set(Calendar.MILLISECOND, 0);
		return (toCal.getTime().getTime() - fromCal.getTime().getTime()) / 1000;
	}

	/**
	 * 獲取一個星期中的第幾天,週日算第一天
	 *
	 * @param date
	 *            待解析的日期
	 * @return 返回一個星期中的第幾天
	 */
	public static int getDayOfWeek(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 獲取一個星期中的第幾天,週一算第一天
	 *
	 * @param date
	 *            待解析的日期
	 * @return 返回一個星期中的第幾天
	 */
	public static int getChinaDayOfWeek(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		if (1 == dayOfWeek) {
			dayOfWeek = 8;
		}
		return dayOfWeek - 1;
	}

	/**
	 * 獲取一個月中的第幾天,一個月中第一天的值爲1
	 *
	 * @param date
	 *            待解析的日期
	 * @return 返回一個月中的第幾天
	 */
	public static int getDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 獲取當前時間的時間戳,精確到毫秒
	 *
	 * @return 返回當前時間的時間戳
	 */
	public static Long getTimestamp() {
		return System.currentTimeMillis();
	}

	/**
	 * 獲取某日的0時0分0秒的Date對象
	 *
	 * @param datetime
	 *            待解析的日期
	 * @return 傳入日期的0時0分0秒的Date對象
	 */
	public static Date getDayStart(Date datetime) {
		if (null == datetime) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(datetime);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 根據傳入的日期獲取當前日期的一月一日
	 *
	 * @param datetime
	 * @return
	 * @Time 2015年3月13日 上午11:24:54 create
	 * @author xiehaibin
	 */
	public static Date getFirstDayOfYear(Date datetime) {
		if (null == datetime) {
			return null;
		}
		int year = getYear(datetime);
		Calendar calendar = new GregorianCalendar(year, 0, 1);
		return calendar.getTime();
	}

	/**
	 * 根據傳入的日期獲取當前日期的一日
	 *
	 * @param datetime
	 * @return
	 * @Time 2015年3月13日 上午11:24:54 create
	 * @author xiehaibin
	 */
	public static Date getFirstDayOfMonth(Date datetime) {
		if (null == datetime) {
			return null;
		}
		int year = getYear(datetime);
		int month = getMonth(datetime);
		Calendar calendar = new GregorianCalendar(year, month - 1, 1);
		return calendar.getTime();
	}

	/**
	 * 獲取某日的23時59分59秒的Date對象
	 *
	 * @param datetime
	 *            待解析的日期
	 * @return Date 傳入日期的23時59分59秒的Date對象
	 */
	public static Date getDayEnd(Date datetime) {
		if (null == datetime) {
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(datetime);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * Format data to string with specified style.
	 *
	 * @param dtmDate
	 *            Date
	 * @param num
	 *            Default Style 3 1:yyyy.MM.dd 2:hh:mm:ss 3:yyyy.MM.dd
	 *            HH:mm:ss(Default) 4:yyyy-MM-dd 5:yyyy 6:MM 7:dd 8:yyyy-MM-dd
	 *            HH:mm 9:HH:mm 10:yyyy-MM-dd HH:mm:ss 11:yyyyMMdd
	 * @return dateString String
	 */
	public static String dateToStr(java.util.Date dtmDate, int num) {
		if (dtmDate == null) {
			return "";
		}
		String f = getDateFormet(num);
		SimpleDateFormat sdf = new SimpleDateFormat(f);
		return sdf.format(dtmDate);
	}

	public static Date strToDate(String dateString, int num) {
		if (dateString == null || dateString.length() == 0) {
			return null;
		}
		String formatstring = getDateFormet(num);
		Date parseDate = null;
		SimpleDateFormat sdf = new SimpleDateFormat();

		try {
			sdf.applyPattern(formatstring);
			parseDate = sdf.parse(dateString);
		} catch (ParseException e) {
			// ignore exception
		}
		return parseDate;
	}

	private static String getDateFormet(int num) {
		String f;
		switch (num) {
		case 1:
			f = "yyyy.MM.dd";
			break;
		case 2:
			f = "kk:mm:ss";
			break;
		case 3:
			f = "yyyy.MM.dd kk:mm:ss";
			break;
		case 4:
			f = "yyyy-MM-dd";
			break;
		case 5:
			f = "yyyy";
			break;
		case 6:
			f = "MM";
			break;
		case 7:
			f = "dd";
			break;
		case 8:
			f = "yyyy-MM-dd HH:mm";
			break;
		case 9:
			f = "HH:mm";
			break;
		case 10:
			f = "yyyy-MM-dd HH:mm:ss";
			break;
		case 11:
			f = "yyyyMMdd";
			break;
		case 12:
			f = "yyyyMMddHHmmssSSS";
			break;
		case 13:
			f = "yyyyMM";
			break;
		case 14:
			f = "HH:mm:ss";
			break;
		case 15:
			f = "yyyy/MM/dd";
			break;
		default:
			f = "yyyy.MM.dd kk:mm:ss";
		}
		return f;
	}

	/**
	 * Normal data format.
	 *
	 * @return String[]
	 */
	private static String[] getdateformat() {
		return new String[] { "yyyy-MM-dd", "yyyy/MM/dd", "yyyy.MM.dd" };
	}

	/**
	 * Parse String to Date.
	 *
	 * @param dateString
	 *            String of Date, the format is yyyy-MM-dd or yyyy/MM/dd or
	 *            yyyy.MM.dd
	 * @return Date
	 * @throws ParseException
	 */
	public static Date stringToDate(String dateString) throws ParseException {
		if (dateString == null || dateString.length() == 0) {
			return null;
		}
		String[] formatstring = getdateformat();
		int index = 0;
		Date parseDate = null;
		ParseException throwe = null;
		SimpleDateFormat sdf = new SimpleDateFormat();

		while (index < formatstring.length) {
			try {
				sdf.applyPattern(formatstring[index]);
				index++;
				parseDate = sdf.parse(dateString);
				break;
			} catch (ParseException gete) {
				throwe = gete;
				continue;
			}
		}
		if (parseDate == null)
			throw throwe;
		return parseDate;
	}
}

創建表和函數



CREATE TABLE `sys_sequence` (
  `NAME` varchar(50) NOT NULL,
  `CURRENT_VALUE` int(11) NOT NULL DEFAULT '0',
  `INCREMENT` int(11) NOT NULL DEFAULT '1',
  PRIMARY KEY (`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



insert  into `sys_sequence`(`NAME`,`CURRENT_VALUE`,`INCREMENT`) values 
('zzq',22,1);



DROP FUNCTION IF EXISTS currval; 
DELIMITER $ 
CREATE FUNCTION currval (seq_name VARCHAR(50)) 
     RETURNS INTEGER
     LANGUAGE SQL 
     DETERMINISTIC 
     CONTAINS SQL 
     SQL SECURITY DEFINER 
     COMMENT ''
BEGIN
     DECLARE value INTEGER; 
     SET value = 0; 
     SELECT current_value INTO value 
          FROM sequence
          WHERE name = seq_name; 
     RETURN value; 
END
$ 
DELIMITER ; 


DROP FUNCTION IF EXISTS nextval; 
DELIMITER $ 
CREATE FUNCTION nextval (seq_name VARCHAR(50)) 
     RETURNS INTEGER
     LANGUAGE SQL 
     DETERMINISTIC 
     CONTAINS SQL 
     SQL SECURITY DEFINER 
     COMMENT ''
BEGIN
     UPDATE sequence
          SET current_value = current_value + increment 
          WHERE name = seq_name; 
     RETURN currval(seq_name); 
END
$ 
DELIMITER ; 



DROP FUNCTION IF EXISTS setval; 
DELIMITER $ 
CREATE FUNCTION setval (NAMEVARCHAR(50), value INTEGER) 
     RETURNS INTEGER
     LANGUAGE SQL 
     DETERMINISTIC 
     CONTAINS SQL 
     SQL SECURITY DEFINER 
     COMMENT ''
BEGIN
     UPDATE sys_sequence
          SET current_value = value 
          WHERE name = NAME; 
     RETURN currval(NAME); 
END
$ 
DELIMITER ; 

測試

1獲取下一個值   SELECT nextval('zzq') FROM DUAL

2 獲取當前值     SELECT currval('zzq') FROM DUAL

3 設置值  :

SELECT setval('zzq',100) FROM DUAL

   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章