SQL 語法備忘

2022-07-04 04:36:08 字數 2614 閱讀 7108

**********  原創作品

sqlserver

--

判斷表是否存在

ifnot

exists (select

*from sysobjects where id =

object_id(n'

tablename

') and

objectproperty(id, n'

isusertable

') =

1)

--

查詢xml欄位中所有tablename節點的值

select a.fid,scope.tbname.value('

.[1]

','varchar(200)

') as

ftablename

from

t_meta_objecttype a

cross

a.fkernelxml.nodes(

'//tablename

') as scope(tbname)

--

使用游標遍歷所有使用者表

declare

@table_name

varchar(128)--

定義游標

declare acur cursor

forselect name from sysobjects where

objectproperty(id, n'

isusertable

') =

1and name not

like n'#%'

order

byname

--開啟游標

open

acur

fetch

next

from acur into

@table_name

while

@@fetch_status=0

begin

--列印表名

print(@table_name

)

--讀取下一條記錄

fetch

next

from acur into

@table_name

end--

關閉釋放游標

close

acur

deallocate acur

--

使用with-as遍歷樹節點及其所有子節點

with

tempas(

select

*from tablename where fid=

'rootid

'union

allselect a.*

from tablename a inner

join

temp b on a.parentid =

b.fid

)select

*from

temp

--

用merge into實現存在修改不存在新增,mysql不支援

merge into

目標表 a

using(

select

2 id,'

yokeqi'[

name

]) tmp --

資料來源,既用來跟目標表關聯,也是後面語句的資料**

on a.id=

tmp.id

when matched then

update

set a.[

name

]= tmp.[

name

]when

not matched then

insert (id,[

name

]) values(tmp.id, tmp.[

name

]);

--

收集資料庫當前大小,增加作業每天執行一次,查詢的時候就能查到一定時間的增長量

create

proc

sp_sizegrowthlog

asif

notexists (select

*from sysobjects where id =

object_id(n'

z_jih_sizelog

') and

objectproperty(id, n'

isusertable

') =1)

create

table z_jih_sizelog([

name

]nvarchar(255),size decimal(18,3),[

date

]datetime)--

記錄當前資料庫的大小,單位(m)

insert

into

z_jih_sizelog

select

[name

], size*

8.0/

1024, getdate() from sysfiles

Markdown語法備忘

markdown 是一種輕量級標記語言,它允許人們使用易讀易寫的純文字格式編寫文件,然後轉換成格式豐富的html頁面。維基百科 windows mac linux 1.標題 注意 號和文字之間要有乙個空格 2.文字格式 3.無序列表 在列表項前面加上 號,號和文字之間有空格 4.有序列表 有數字的列...

Markdown語法備忘

markdown編輯器 windows 1.markdownpad 2.markpad mac mou linux retext markdown語法 1.標題 複製 如下 h1 一級標題 h2二級標題 依次類推,直到 h6六級標題 注意 號和文字之間要有乙個空格 2.文字格式 複製 如下 文字粗體...

python基礎語法備忘

x input 請輸入乙個數字 x 請輸入乙個數字 4 4 輸出是乙個字元,是帶引號的 eval 去掉引號 x eval input 請輸入乙個數字 x請輸入乙個數字 4 4 輸出是乙個數字,不帶引號的print 預設換行,若不想換行 換行控制 end print 123 end print 456...