zoukankan      html  css  js  c++  java
  • SQL 技巧总结 patIndex 查询 where decimal用法

     一些常用SQL的总结

        patIndex ,where ,decimal 等函数的巧用

    --1.charIndex 精确查找字符串的位置
    declare @charIndex varchar(500)
    set @charIndex='MicrosoftSQLServerManagement'
    --返回字符串中'soft'的位置
    select charIndex('soft',@charIndex)
    --2.patIndex  可以模糊查找字符串的位置(可以使用通配符)
    declare @patIndex varchar(500)
    set @patIndex='2000個'
    --patindex('%[^0-9]%',@patIndex)取字符串中第一个不是数字的位置
    --取字符的数字部分
    select substring(@patIndex,1,patindex('%[^0-9]%',@patIndex)-1)
    
    --3. ISNULL 多条件查询where 巧用
    --例如
    --传入参数
    declare @PCProductID int
    declare @CategoryID int
    select * from
     Demo P
    WHERE 
      --当 @PCProductID 为NUll表明该查询条件未输入,不执行该条件查询
      --用以下方法可以直接使用                           
      P.商品CD =isnull(@PCProductID,P.商品CD)                
    AND
      -- @CategoryID=''表示全选所有类别,查询所有的类型
      --以下用法兼顾了有个别类别和全选的情况                         
      (P.类别 =@CategoryID or @CategoryID='')   
      
    --4. decimal与round
    --round和decimal都可以固定位数四舍五入,round不能处理无限小数,decimal可以处理无限小数
    --处理金额,等精确数字是一定要用decimal保存小数位数要尽量的多,不要用money类型,money类型一旦进过二次计算会很不精确
    --5. nolock 和 rowlock

       nolock 即不加锁,可以快速读取表的数据,缺点可能会读取到脏数据(事务未回滚完的数据等),一般只用操作频率很高的表查询数据
       例如: select * from DataOnToday with (nolock)

       rowlock 即行级别加锁 如果该表数据量很大会加重负荷。可防止并发操作 一般用于比较重要的表的(产生序列号) Update Delete

       例如 :Update ProdutNo with (rowlock) Set ProuductID = ProductID +1

  • 相关阅读:
    max key length is 1000 bytes
    205 Reset Content
    The Path Attribute
    track message forwards, avoiding request loops, and identifying the protocol capabilities of all senders along the request/response chain
    test hypertext links for validity, accessibility, and recent modification
    ES6 will change the way you write JS code.
    ECMAScript Web APIs node.js
    Symbols
    HTML/Elements/base
    frag General URL components
  • 原文地址:https://www.cnblogs.com/jiangqiang/p/3467948.html
Copyright © 2011-2022 走看看