Sybase數據庫常用sql語言
Sybase數據庫查詢所有表及字段
Sybase數據庫常用sql語言
select
d.name AS 表名,
a.name as 字段名,
UPPER(b.name) as 字段類型,
a.length as 長度,
(case when a.status =8 then 'NULL' else 'NOT NULL' end) as 是否為空
FROM syscolumns a left join systypes b
on a.usertype=b.usertype
inner join sysobjects d
on a.id=d.id and d.type='U' and d.name<>'dtproperties'
left join syscomments e
on a.cdefault=e.id
where d.name LIKE 'TKT%'
ORDER BY d.name
查詢sybase DB中占用空間最多的前20張表
按照數據行數查詢
Select top 20 name, row_count(db_id(), id) from sysobjects order by row_count(db_id(),id) desc
按照分配的空間查詢
Select top 20 name, reserved_pages(db_id(),id)/(1024.0 / (@@maxpagesize/1024.0) ) as "Allocated MB" from sysobjects order by reserved_pages(db_id(),id) desc
按照已使用空間查詢
Select top 20 name, used_pages(db_id(),id)/(1024.0 / (@@maxpagesize/1024.0) ) as "Used MB" from sysobjects order by used_pages(db_id(),id) desc
1,表備份:
--table_name1:需要備份的表; table_name2:備份后的表 SELECT * into table_name2 from table_name1
2,刪除列:
ALTER TABLE table_name DELETE column_name;
3,增加列:
ALTER TABLE table_name ADD (column_name DATA_TYPE [NOT] NULL);
4,修改列的空與非空:
ALTER TABLE table_name MODIFY column_name [NOT] NULL;
5,修改列名:
ALTER TABLE table_name RENAME old_column_name TO new_column_name;
6,快速建立臨時表:
SELECT * INTO table_name_new FROM table_name_old;
7,修改表名:
ALTER TABLE old_table_name RENAME new_table_name
8,增加主鍵約束:
ALTER TABLE tb_name ADD CONSTRAINT pk_name PRIMARY KEY(col_name,..)
9,刪除主鍵約束:
ALTER TABLE tb_name DROP CONSTRAINT pk_name;
10,建立自增長字段,與Oracle的SEQUENCE類似:
CREATE TABLE TMP_001 (RES_ID INTEGER IDENTITY NOT NULL);
11,添加表注釋:
COMMENT ON TABLE table_name IS '....';
12,創建索引:
CREATE INDEX index_name ON table_name(column_name);
13,查詢表結構:
select * from systable a left join syscolumn b on a.table_id = b.table_id where UPPER(a.table_name)=UPPER('table_name') order by column_name;
14,查看所有表
select name from sysobjects where type='U'
15,創建表
#字段前面不允許有空格,不然列的名稱前面會用空格 create table t_wlpc_shrjj( id INTEGER IDENTITY NOT NULL, rpname varchar(500) null, rpdate varchar(50) null, jjzt varchar(255) null, fbsjj varchar(255) null, etf varchar(255) null, lof varchar(255) null, fjlof varchar(255) null, create_date Datetime null, update_date Datetime null )
16,刪除表
if exists (select 1 from sysobjects
where id = object_id('users') and type = 'U')
drop table users
17,查詢指定數據庫的表
select * from sybaseiq195..T_x27_user;
18、日期函數
--1,獲取當前日期時間 getdate() select getdate() --2,取指定時間的某一部分 datepart(日期部分,日期) --取時間的某一個部分 select datepart(yy,getdate()) --year select datepart(mm,getdate()) --month select datepart(dd,getdate()) --day select datepart(hh,getdate()) --hour select datepart(mi,getdate()) --min select datepart(ss,getdate()) --sec --取星期幾 set datefirst 1 select datepart(weekday,getdate()) --weekday --3,日期2-日期1,單位為日期部分指定 datediff(日期部分,日期1,日期2) SELECT datediff(yy,'2012/12/01',getdate()) SELECT datediff(mm,'2012/12/01',getdate()) SELECT datediff(dd,'2012/12/01',getdate()) SELECT datediff(hh,'2012/12/01',getdate()) SELECT datediff(mi,'2012/12/01',getdate()) SELECT datediff(ss,'2012/12/01',getdate()) --4,計算指定時間,再加上表達式指定的時間長度 dateadd(日期部分,數值,日期) SELECT dateadd(yy,1,getdate()) SELECT dateadd(mm,1,getdate()) SELECT dateadd(dd,1,getdate()) SELECT dateadd(hh,1,getdate()) SELECT dateadd(mi,1,getdate()) SELECT dateadd(ss,1,getdate()) --5,字符串時間 select getdate() -- '2012/12/4 10:48:07.540' select convert(char,getdate(),101) -- '12/04/2012' select convert(char,getdate(),102) -- '2012.12.04' select convert(char,getdate(),103) -- '04/12/2012' select convert(char,getdate(),104) -- '04.12.2012' select convert(char,getdate(),105) -- '04-12-2012 select convert(char,getdate(),106) -- '04 Dec 2012' select convert(char,getdate(),107) --'Dec 04, 2012' select convert(char,getdate(),108) --'11:16:06' select convert(char,getdate(),109) --'Dec 4 2012 10:50:28:146AM' select convert(char,getdate(),110) --'12-04-2012' select convert(char,getdate(),111) --'2012/12/04' select convert(char,getdate(),112) --'20121204' --6,以時間為維度的趨勢查詢 --查詢一個月內的每一天 select convert(varchar(10), dateadd(dd, number + 1, convert(char(10), dateadd(mm, -1, getdate()), 111)), 111) dt from master .. spt_values where type = 'p' and number < 31 --查詢一年內的每一月 select convert(varchar(7),dateadd(mm,number + 1, convert(char(10), dateadd(yy, -1, getdate()), 111)), 111) dt from master .. spt_values where type = 'p' and number < 12 --7 空處理函數 isnull(val1,val2) val1為空,值為val2
19,獲取當前時間
select substr(convert(varchar,getdate(*),25),0,19) -- 結果:
20,刪除表數據
TRUNCATE TABLE:刪除內容、釋放空間但不刪除定義。 DELETE TABLE:刪除內容不刪除定義,不釋放空間。 DROP TABLE:刪除內容和定義,釋放空間。 區別: truncate刪除的更徹底,所有的日志記錄都會沒有,並且是一把全清,不會有where條件。 還有個比較大的區別就是truncate后自增長的ID列也會歸零,以后插入記錄ID從1開始。 但是delete后你再插入記錄,ID會從上次最大的數字開始。 delete 可以配合where條件。
21,查看過程
sp_helptext 存儲過程名
22,刪除存儲過程
--P_add_user存儲過程名
if exists (select 1 from sysobjects
where id = object_id('P_add_user')
and type = 'P')
drop procedure P_add_user
go
22,存儲過程調用
--存儲過程帶一個參數@applyid
declare @c_applyid varchar(50)
select @c_applyid='0135'
execute DBA.p_process @applyid=@c_applyid
23,commit work 與 commit work and wait區別
COMMIT WORK是異步的 COMMIT WORK AND WAIT是同步的 由於系統中COMMIT WORK的數量是有限的,比如說我們系統是4000個,同時只能提交4000個更新進程,對於COMMIT WORK來說,執行完就釋放了,可以為后面的COMMIT WLRK繼續使用;而對於COMMIT WORK AND WAIT是要執行完才釋放的,所以對於沒有必要同步更新的,也盡快使用異步,使得資源得到釋放。
24,PATINDEX用法
--語法 PATINDEX ( '%pattern%' , expression ) --返回指定表達式中某模式第一次出現的起始位置;如果在全部有效的文本和字符數據類型中沒有找到該模式,則返回零。 --參數 pattern 一個文字字符串。可以使用通配符,但 pattern 之前和之后必須有 % 字符(搜索第一個或最后一個字符時除外)。pattern 是字符串數據類型類別的表達式。 expression 一個表達式,通常為要在其中搜索指定模式的列,expression 為字符串數據類型類別。 --返回類型 如果 expression 的數據類型為 varchar(max) 或 nvarchar(max),則為 bigint,否則為 int。
25,join用法
--1,Inner join:產生A和B的交集。 SELECT * FROM test_a INNER JOIN test_b ON test_a.name =test_b.name --2,Full outer join:產生A和B的並集。對於沒有匹配的記錄,則以null做為值。 SELECT * FROM test_a FULL OUTER JOIN test_b ON test_a.name = test_b.name --3,Left outer join:產生表A的完全集,而B表中匹配的則有值,沒匹配的以null值取代。 SELECT * FROM test_a LEFT OUTER JOIN test_b ON test_a.name = test_b.name --4,Left outer join on where:產生在A表中有而在B表中沒有的集合。 SELECT * FROM test_a LEFT OUTER JOIN test_b ON test_a.name = test_b.name WHERE test_b.name IS NULL --5,RIGHT OUTER JOIN:產生表B的完全集,而A表中匹配的則有值,沒匹配的以null值取代。 SELECT * FROM test_a RIGHT OUTER JOIN test_b ON test_a.name = test_b.name --6, right outer join on where:產生在B表中有而在A表中沒有的集合。 SELECT * FROM test_a RIGHT OUTER JOIN test_b ON test_a.name = test_b.name WHERE test_a.name IS NULL --7,FULL OUTER JOIN WHERE:產生(A表中有但B表沒有)和(B表中有但A表中沒有)的數據集。 SELECT * FROM test_a FULL OUTER JOIN test_b ON test_a.name = test_b.name WHERE est_a.name IS NULL OR test_b.name IS NULL
26,存儲過程采用臨時表存儲查詢結果
--無需定義臨時表,直接使用;自動釋放臨時表; select ... into #tmp from yourtable ... select name from #temp_tab;
26,插入數據
--插入一條記錄
insert into A select '1','2','3','4'
--批量插入記錄
insert into A
select '1','2','3','4'
union all
select '5','6','7','8'
union all
select '9','10','11','12
27,根據字段值變更多個字段值
update A set a.bbv=(case a.bby when '1' then '100' when '2' then '200' when '3' then '300' end), a.bbc=(case a.bby when '1' then 'a' when '2' then 'b' when '3' then 'c' end), a.bbf=(case a.bby when '1' then 'e' when '2' then 'd' when '3' then 'f' end) from A a
28,convert(datatype,變量[,顯示格式]) 用法
--數值型字段查詢條件用convert() select * from shrjj where id= convert(integer,23136) ;
29,獲取長度
char_length返回的是字符數 datalength返回的是字節數 --獲取字段最大值的最后一位值,null則取'0' select case when substring(max(t.pk_org),char_length(max(t.pk_org)) ,char_length(max(t.pk_org))) is null then '0' else substring(max(t.pk_org),char_length(max(t.pk_org)) ,char_length(max(t.pk_org))) end from dba.org t where t.pk_org like '#ZSSYB100%';
30,獲取第一條數據,並拆分、截取字符串
--獲取第一條記錄,並且截取字符串的X后的數字
--因為max()函數對字符的獲取,異常
select top 1 t.pk_org,
--獲取X字符的位置
charindex('X',t.pk_org),
--獲取X字符后的數字
char_length(t.pk_org),substring(t.pk_org, charindex('X',t.pk_org)+1 ,char_length(t.pk_org)) as x_zf,
--case ... when
case when substring(t.pk_org, charindex('X',t.pk_org)+1 ,char_length(t.pk_org)) is null then '0' else substring(t.pk_org, charindex('X',t.pk_org)+1 ,char_length(t.pk_org)) end as x_sz,
--字符轉數字
convert(int,case when substring(t.pk_org, charindex('X',t.pk_org)+1 ,char_length(t.pk_org)) is null then '0' else substring(t.pk_org, charindex('X',t.pk_org)+1 ,char_length(t.pk_org)) end)+1 as x_result
from dba.t_test t where t.pk_corp='1001' order by char_length(t.pk_org) desc,t.pk_org desc
結果:
![]()
31,查詢表對應的主鍵:
select o.name as 表名, i.column_name as 主鍵列名 from sysobjects o,syscolumn i where o.id=i.table_id and i.pkey = 'Y' and i.table_id = 'tablename';

