---
title: "部分sql语句记录"
author: "simple_hui"
date: "2009-12-07T15:20:37Z"
tags: []
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2012-09-09T15:30:30Z"
id: 543348
url: "https://simple-hui.iteye.com/blog/543348"
---

记录下最近用到的一些SQL语句
1.用dao查询两个表时，sql语句如下下划线的地方不要带上*
select <u>subt</u> from CdmsChemwateranaSubt subt,CdmsChemwateranaMaint mait where ……
<span style="color:red">【注】2012.9.9已经不记得上面的语句为什么不带*。现在使用的时候是可以的。</span>


2.dao只能根据对象查，对复杂的SQL语句还是原始的sql语句
但是原始的sql语句是不能直接用dao.find()之类的方法查询的需取到session


```java
String sql= "insert into OLMS_HS_TOITCATSDSTATUSLOG(HSTS_ID,SHIFT_ID,HSTS_DATE,UNIT_ID,HSTS_INPUT_TIME,USER_ID,HSTS_DESC) "
				+ "select OLMS_SEQUENCE.Nextval,'"
				+ new_shiftId
				+ "',to_date(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss'),UNIT_ID,to_date(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss'),'"
				+ userid_hs
				+ "',HSTS_DESC "
				+ "From OLMS_HS_TOITCATSDSTATUSLOG "
				+ "where SHIFT_ID='"
				+ old_shiftId + "'";

		// 批量执行sql
		Connection conn = dao.getConnection();
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
                            stmt.addBatch(sql);
                            stmt.executeBatch();
			conn.commit();
		} catch (SQLException ex) {
			ex.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
```


3.查询最大的序列号，在字段是string类型时，因为string类型原本的排序是字符串排序

```java
dao.find("select max(menuNo+0) from OmsMenu");
```