SQL 随笔
if not exists () insert into table value else update talbe set //若存在更新不存在插入
select isnull(max( cast( substring(" + colName + ",patindex('%[0-9]%'," + colName + "),50) as float)),0) + 1 as num from " + tableName + " //用来获取某一列得自证值。可处理字母加自增数字得/
我写得帮助类:
1 public class DBCreateWareId 2 { 3 public string CreateId(string tableName, string colName) //tableName表名/colName列名/返回id加1;如2 4 { 5 return GetId(tableName, colName); 6 } 7 public string CreateId(string tableName, string colName, string str)//tableName表名/colName列名/str字段前面的字母如DBC001中的DBC/返回id加1,如BB2; 8 { 9 return str + GetId(tableName, colName); 10 } 11 public string CreateId(string tableName, string colName, string str, bool date)//tableName表名/colName列名/str字段前面的字母如DBC001中的DBC/返回id加1,如BB2017010011122; 12 { 13 return str + DateTime.Now.ToString("yyyyMMdd") + GetId(tableName, colName); 14 } 15 16 private string GetId(string tableName, string colName) 17 { 18 string id = ""; 19 string Sql = "select isnull(max( cast( substring(" + colName + ",patindex('%[0-9]%'," + colName + "),50) as float)),0) + 1 as num from " + tableName + ""; 20 21 using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, Sql)) 22 { 23 while (rdr.Read()) 24 { 25 id = rdr["num"].ToString(); 26 } 27 if (id == "") 28 { 29 id = "0"; 30 } 31 } 32 return id; 33 } 34 }