sqlserver 轉 postgresql


 pgsql
 1. 沒有isnull 用 coalesce
 2. 字符串拼接用 || 
 3. 字符串類型和int類型不會自動轉換(用作條件時)
 4. 多行轉一列 string_agg(distinct(字段名),'分隔符') distinct是為了去重可以不要
 5. unnest(string_to_array (par_LoadingNos, ','))  //string_to_array 以 , 分隔字符串  unnest 把數據變為一列返回
 6. 沒有charindex,用strpos (原字符串,需要查找的)
 7. 沒有getdate() 用 LOCALTIMESTAMP(0) 代替 參數指秒以下取幾位
 8. 聯表更新方法 update a set value = 'test' from b,c where a.b_id = b.id and b.c_id = c.id and a.key = 'test' and c.value = 'test';
 9. 查詢表結構 SELECT a.attnum,a.attname AS field,t.typname AS type,a.attlen AS length,a.atttypmod AS lengthvar,a.attnotnull AS notnull
FROM pg_class c,pg_attribute a,pg_type t
WHERE c.relname = 'cms_qq' and a.attnum > 0 and a.attrelid = c.oid and a.atttypid = t.oid
ORDER BY a.attnum;
 10. 創建臨時表     create TEMPORARY table 表名 , select * into temp 表名  from  ,函數不能select into  里只能  create TEMPORARY table 表名(...) as select * From .
 11.條件寫法: IF 表達試  THEN  ELSIF  表達試  THEN  ELSE  END IF; // IF EXISTS() THEN ....
 12.     1、增加一列ALTER TABLE table_name ADD column_name datatype;

 

        2、刪除一列
        ALTER TABLE table_name DROP column_name;

 

        3、更改列的數據類型
        ALTER TABLE table_name ALTER column_name TYPE datatype;

 

        4、表的重命名
        ALTER TABLE table_name RENAME TO new_name;

 

        5、更改列的名字
        ALTER TABLE table_name RENAME column_name to new_column_name;

 

        6、字段的not null設置
        ALTER TABLE table_name ALTER column_name {SET|DROP} NOT NULL;

 

        7、給列添加default
        ALTER TABLE table_name ALTER column_name SET DEFAULT expression;
        
 13. LASTVAL() 方法類型於sqlserver里的@@IDENTITY        
 14. 去兩邊空格  trim()         
 15. 正則(https://www.cnblogs.com/qiyebao/p/4980648.html)
     1. select * from user where email ~* '^[a-h]'
     2. 'abc' LIKE 'abc'    true
        'abc' LIKE 'a%'     true
        'abc' LIKE '_b_'    true
        'abc' LIKE 'c'      false
        
        'abc' SIMILAR TO 'abc'      true
        'abc' SIMILAR TO 'a'    false
        'abc' SIMILAR TO '%(b|d)%'  true
        'abc' SIMILAR TO '(b|c)%'   false
        
 16. 設置自增列啟始值
    ALTER TABLE public.sc_users3
ALTER COLUMN userid ADD
GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 2000 MINVALUE 2000 MAXVALUE 9223372036854775807 CACHE 1 ) 
 
  17. INNER JOIN LATERAL與CROSS APPLY
    並且LEFT JOIN LATERAL相同OUTER APPLY
    
  18. 創建索引    CREATE INDEX Form_ASN_QIndex ON Form_ASN (FormID,VendorID);
  
  19. 分頁查詢 order by lastname limit 5 offset 0;(order by 不是必須的)
  
  20. 輸出文字 到輸出界面 raise notice 'My name is %, I am a %.', 'Lewis', 'coder';
  
  21. json和jsonb 或 Array 里取值出來 請參考操作符
  
  22. 插入自增列  insert into test (id, info) OVERRIDING SYSTEM VALUE values (1,'test'); 
  
  23.生成GUID需要添加擴展. 以下為一種(還有其它的方式)
   create extension "uuid-ossp" ;
   select uuid_generate_v4() 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM