sql 实用语句

1.select into 和 insert into select 区别及用法
select * into destTbl from srcTbl
insert into destTbl(fld1, fld2) select fld1, 5 from srcTbl
以上两句都是将 srcTbl 的数据插入到 destTbl,但两句又有区别的。
    第一句(select into from)要求目标表(destTbl)不存在,因为在插入时会自动创建。(insert into 下,是将选择好的结果集一个个插入一个已存在的表内
    第二句(insert into select from)要求目标表(destTbl)存在,由于目标表已经存在,所以我们除了插入源表(srcTbl)的字段外,还可以插入常量
select * into 是将一个选择出来的结果集保存到一个即将创建的表内。
2.使用insert into 配合 select from 语句进行复制,目标表和数据来源表都是已经存在的,待插的临时表的架构要和插入源一致,列对应列,数据类型对应数据类型.
INSERT INTO  title  (title_id, title, type, pub_id)
SELECT title_id, title, type, pub_id
FROM titles WHERE (pub_id = '0766')
3.IN的使用
  /// 删除选中的教室信息
  public void DeleteSelected(ArrayList selected)
  {  string delWhere = " id in (";
      foreach (int id in selected)
        {
            delWhere += id + " , ";
        }
        delWhere = delWhere.Substring(0, delWhere.LastIndexOf(","));
        delWhere += ")";
        dal.DeleteByWhere(delWhere);
  }
相当于:delete from student where id in ('1','2','3' )
4.substring(表达式,起始位置,截取长度)的用法(起始位置从1开始)
SELECT *
FROM bjsjk where jw_xybh='A' and jw_sszybh='100302' and  (cast(substring(bh,1,2) as int)+xz )=06


转载于:https://www.cnblogs.com/hubcarl/archive/2009/03/15/1412711.html

猜你喜欢

转载自blog.csdn.net/weixin_33984032/article/details/93817096