日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

SQL語句集錦

 芳草小集 2006-03-24

INSERT INTO TABLENAME(FIELD1,FIELD2) VALUES(‘‘,‘‘)
INSERT INTO Security (Username,Sys,UserType,ModuleName) SELECT ?,‘workflow‘,‘G‘,ModuleName FROM Module

UPDATE TABLENAME SET FIELD1=?,FIELD2=?

DELETE FROM TABLENAME WHERE ID=?


DECLARE @PAR1 Varchar(30) DECLARE @PAR2 Varchar(100) DECLARE @PAR3 Varchar(30)
SET @PAR1=? SET @PAR2=? SET @PAR3=?
UPDATE UserGroup SET GroupName=@PAR1,GroupDesc=@PAR2,RevisiedBy=‘$username$‘,T_Stamp=getdate() WHERE GroupName=@PAR3
UPDATE Security SET UserName=@PAR1 WHERE UserName=@PAR3
UPDATE Users SET GroupName=@PAR1 WHERE GroupName=@PAR3

 


-=SQL語句集錦=-

  -=SQL語句集錦=-


--語 句--           --功 能--

--數(shù)據(jù)操作
select             --從數(shù)據(jù)庫表中檢索數(shù)據(jù)行和列
insert             --向數(shù)據(jù)庫表添加新數(shù)據(jù)行
delete             --從數(shù)據(jù)庫表中刪除數(shù)據(jù)行
update             --更新數(shù)據(jù)庫表中的數(shù)據(jù)

--數(shù)據(jù)定義
create table       --創(chuàng)建一個數(shù)據(jù)庫表
drop table         --從數(shù)據(jù)庫中刪除表
alter table        --修改數(shù)據(jù)庫表結(jié)構(gòu)
create view        --創(chuàng)建一個視圖
drop view          --從數(shù)據(jù)庫中刪除視圖
create index       --為數(shù)據(jù)庫表創(chuàng)建一個索引
drop index         --從數(shù)據(jù)庫中刪除索引
create procedure   --創(chuàng)建一個存儲過程
drop procedure     --從數(shù)據(jù)庫中刪除存儲過程
create trigger     --創(chuàng)建一個觸發(fā)器
drop trigger       --從數(shù)據(jù)庫中刪除觸發(fā)器
create schema      --向數(shù)據(jù)庫添加一個新模式
drop schema        --從數(shù)據(jù)庫中刪除一個模式
create domain      --創(chuàng)建一個數(shù)據(jù)值域
alter domain       --改變域定義
drop domain        --從數(shù)據(jù)庫中刪除一個域

--數(shù)據(jù)控制
grant              --授予用戶訪問權(quán)限
deny               --拒絕用戶訪問
revoke             --解除用戶訪問權(quán)限

--事務(wù)控制
commit             --結(jié)束當(dāng)前事務(wù)
rollback           --中止當(dāng)前事務(wù)
set transaction    --定義當(dāng)前事務(wù)數(shù)據(jù)訪問特征

--程序化sql
declare            --為查詢設(shè)定游標(biāo)
explan             --為查詢描述數(shù)據(jù)訪問計劃
open               --檢索查詢結(jié)果打開一個游標(biāo)
fetch              --檢索一行查詢結(jié)果
close              --關(guān)閉游標(biāo)
prepare            --為動態(tài)執(zhí)行準(zhǔn)備sql 語句
execute            --動態(tài)地執(zhí)行sql 語句
describe           --描述準(zhǔn)備好的查詢

---局部變量
declare @id char(10)
--set @id = ‘10010001‘
select @id = ‘10010001‘

---全局變量
---必須以@@開頭

--IF ELSE
declare @x int @y int @z int
select @x = 1 @y = 2 @z=3
if @x > @y
print ‘x > y‘ --打印字符串‘x > y‘
else if @y > @z
print ‘y > z‘
else print ‘z > y‘

--CASE
use pangu
update employee
set e_wage =
case
  when job_level = ’1’ then e_wage*1.08
  when job_level = ’2’ then e_wage*1.07
  when job_level = ’3’ then e_wage*1.06
  else e_wage*1.05
end

--while continue break
declare @x int @y int @c int
select @x = 1 @y=1
while @x < 3
begin
  print @x --打印變量x 的值
  while @y < 3
   begin
    select @c = 100*@x + @y
    print @c --打印變量c 的值
    select @y = @y + 1
   end
  select @x = @x + 1
  select @y = 1
end

--WAITFOR--
--例 等待1 小時2 分零3 秒后才執(zhí)行SELECT 語句
waitfor delay ’01:02:03’
select * from employee
--例 等到晚上11 點零8 分后才執(zhí)行SELECT 語句
waitfor time ’23:08:00’
select * from employee


***SELECT***

   select *(列名) from table_name(表名) where column_name operator value
   ex:(宿主)
  select * from stock_information where stockid   = str(nid)
     stockname = ‘str_name‘
     stockname like ‘% find this %‘
     stockname like ‘[a-zA-Z]%‘ --------- ([]指定值的范圍)
     stockname like ‘[^F-M]%‘   --------- (^排除指定范圍)
                                --------- 只能在使用like關(guān)鍵字的where子句中使用通配符)
     or stockpath = ‘stock_path‘
     or stocknumber < 1000
     and stockindex = 24
     not stock*** = ‘man‘
     stocknumber between 20 and 100
     stocknumber in(10,20,30)
     order by stockid desc(asc) --------- 排序,desc-降序,asc-升序
     order by 1,2               --------- by列號
     stockname = (select stockname from stock_information  where stockid  = 4)
                                --------- 子查詢
                                --------- 除非能確保內(nèi)層select只返回一個行的值,
                                --------- 否則應(yīng)在外層where子句中用一個in限定符
  select distinct column_name form table_name
                                --------- distinct指定檢索獨有的列值,不重復(fù)
  select stocknumber ,"stocknumber + 10" = stocknumber + 10 from table_name
  select stockname , "stocknumber" = count(*) from table_name group by stockname
                                --------- group by 將表按行分組,指定列中有相同的值
          having count(*) = 2   ---------  having選定指定的組
       
  select *
  from table1, table2                 
  where table1.id *= table2.id  -------- 左外部連接,table1中有的而table2中沒有得以null表示
     table1.id =* table2.id     -------- 右外部連接


  select stockname from table1
  union [all]                   ---------  union合并查詢結(jié)果集,all-保留重復(fù)行
  select stockname from table2

 

***insert***


  insert into table_name (Stock_name,Stock_number) value ("xxx","xxxx")
              value (select Stockname , Stocknumber from Stock_table2)
                         -------- value為select語句


***update***

  update table_name set Stockname = "xxx" [where Stockid = 3]
         Stockname = default
         Stockname = null
         Stocknumber = Stockname + 4
 

***delete***

  delete from table_name where Stockid = 3
  truncate table_name            --------- 刪除表中所有行,仍保持表的完整性
  drop table table_name          --------- 完全刪除表

***alter table***                --------- 修改數(shù)據(jù)庫表結(jié)構(gòu)

  alter table database.owner.table_name add column_name char(2) null .....
  sp_help table_name             --------- 顯示表已有特征
  create table table_name (name char(20), age smallint, lname varchar(30))
  insert into table_name select .........
                                 --------- 實現(xiàn)刪除列的方法(創(chuàng)建新表)
  alter table table_name drop constraint Stockname_default
                                 --------- 刪除Stockname的default約束

   
***function(/*常用函數(shù)*/)***


----統(tǒng)計函數(shù)----
AVG    --求平均值
COUNT   --統(tǒng)計數(shù)目
MAX    --求最大值
MIN    --求最小值
SUM    --求和


--AVG
use pangu
select avg(e_wage) as dept_avgWage
from employee
group by dept_id


--MAX
--求工資最高的員工姓名
use pangu
select e_name
from employee
where e_wage =
(select max(e_wage)
  from employee)


--stdev()
--stdev()函數(shù)返回表達式中所有數(shù)據(jù)的標(biāo)準(zhǔn)差


--stdevp()
--stdevp()函數(shù)返回總體標(biāo)準(zhǔn)差

--var()
--var()函數(shù)返回表達式中所有值的統(tǒng)計變異數(shù)


--varp()
--varp()函數(shù)返回總體變異數(shù)

----算術(shù)函數(shù)----

/***三角函數(shù)***/
sin(float_expression) --返回以弧度表示的角的正弦
cos(float_expression) --返回以弧度表示的角的余弦
tan(float_expression) --返回以弧度表示的角的正切
cot(float_expression) --返回以弧度表示的角的余切

/***反三角函數(shù)***/
asin(float_expression) --返回正弦是float 值的以弧度表示的角
acos(float_expression) --返回余弦是float 值的以弧度表示的角
atan(float_expression) --返回正切是float 值的以弧度表示的角
atan2(float_expression1,float_expression2)
        --返回正切是float_expression1 /float_expres-sion2的以弧度表示的角
degrees(numeric_expression)
        --把弧度轉(zhuǎn)換為角度返回與表達式相同的數(shù)據(jù)類型可為
        --integer/money/real/float 類型
radians(numeric_expression)
        --把角度轉(zhuǎn)換為弧度返回與表達式相同的數(shù)據(jù)類型可為
        --integer/money/real/float 類型
exp(float_expression)  --返回表達式的指數(shù)值
log(float_expression)  --返回表達式的自然對數(shù)值
log10(float_expression)--返回表達式的以10 為底的對數(shù)值
sqrt(float_expression) --返回表達式的平方根

/***取近似值函數(shù)***/
ceiling(numeric_expression) 
        --返回>=表達式的最小整數(shù)返回的數(shù)據(jù)類型與表達式相同可為
        --integer/money/real/float 類型
floor(numeric_expression)   
        --返回<=表達式的最小整數(shù)返回的數(shù)據(jù)類型與表達式相同可為
        --integer/money/real/float 類型
round(numeric_expression)   
        --返回以integer_expression 為精度的四舍五入值返回的數(shù)據(jù)
        --類型與表達式相同可為integer/money/real/float 類型
abs(numeric_expression)     
        --返回表達式的絕對值返回的數(shù)據(jù)類型與表達式相同可為
        --integer/money/real/float 類型
sign(numeric_expression)    
        --測試參數(shù)的正負(fù)號返回0 零值1 正數(shù)或-1 負(fù)數(shù)返回的數(shù)據(jù)類型
        --與表達式相同可為integer/money/real/float 類型

pi()    --返回值為π 即3.1415926535897936
rand([integer_expression])  
        --用任選的[integer_expression]做種子值得出0-1 間的隨機浮點數(shù)


----字符串函數(shù)----
ascii()         --函數(shù)返回字符表達式最左端字符的ascii 碼值
char()          --函數(shù)用于將ascii 碼轉(zhuǎn)換為字符
                --如果沒有輸入0 ~ 255 之間的ascii 碼值char 函數(shù)會返回一個null 值
lower()         --函數(shù)把字符串全部轉(zhuǎn)換為小寫
upper()         --函數(shù)把字符串全部轉(zhuǎn)換為大寫
str()           --函數(shù)把數(shù)值型數(shù)據(jù)轉(zhuǎn)換為字符型數(shù)據(jù)
ltrim()         --函數(shù)把字符串頭部的空格去掉
rtrim()         --函數(shù)把字符串尾部的空格去掉
left(),right(),substring() 
                --函數(shù)返回部分字符串
charindex(),patindex() 
                --函數(shù)返回字符串中某個指定的子串出現(xiàn)的開始位置
soundex()       --函數(shù)返回一個四位字符碼
                --soundex函數(shù)可用來查找聲音相似的字符串
  --但soundex函數(shù)對數(shù)字和漢字均只返回0 值    
difference()   
                --函數(shù)返回由soundex 函數(shù)返回的兩個字符表達式的值的差異
                --0 兩個soundex 函數(shù)返回值的第一個字符不同
                --1 兩個soundex 函數(shù)返回值的第一個字符相同
                --2 兩個soundex 函數(shù)返回值的第一二個字符相同
                --3 兩個soundex 函數(shù)返回值的第一二三個字符相同
                --4 兩個soundex 函數(shù)返回值完全相同
                                      
quotename()     --函數(shù)返回被特定字符括起來的字符串

/*select quotename(‘a(chǎn)bc‘, ‘{‘) quotename(‘a(chǎn)bc‘)
運行結(jié)果如下
----------------------------------
{abc} [abc]*/


replicate()     --函數(shù)返回一個重復(fù)character_expression 指定次數(shù)的字符串
/*select replicate(‘a(chǎn)bc‘, 3) replicate( ‘a(chǎn)bc‘, -2)
運行結(jié)果如下
----------- -----------
abcabcabc NULL*/
 

reverse()       --函數(shù)將指定的字符串的字符排列順序顛倒
replace()       --函數(shù)返回被替換了指定子串的字符串
/*select replace(‘a(chǎn)bc123g‘, ‘123‘, ‘def‘)
運行結(jié)果如下
----------- -----------
abcdefg*/


space()         --函數(shù)返回一個有指定長度的空白字符串
stuff()         --函數(shù)用另一子串替換字符串指定位置長度的子串


----數(shù)據(jù)類型轉(zhuǎn)換函數(shù)----
cast() 函數(shù)語法如下
cast() (<expression> as <data_ type>[ length ])
convert() 函數(shù)語法如下
convert() (<data_ type>[ length ], <expression> [, style])

select cast(100+99 as char) convert(varchar(12), getdate())
運行結(jié)果如下
------------------------------------------
199   Jan 15 2000


----日期函數(shù)----
day()       --函數(shù)返回date_expression 中的日期值
month()     --函數(shù)返回date_expression 中的月份值
year()      --函數(shù)返回date_expression 中的年份值
dateadd(<datepart> ,<number> ,<date>)
            --函數(shù)返回指定日期date 加上指定的額外日期間隔number 產(chǎn)生的新日期
datediff(<datepart> ,<number> ,<date>)
            --函數(shù)返回兩個指定日期在datepart 方面的不同之處
datename(<datepart> , <date>) 
            --函數(shù)以字符串的形式返回日期的指定部分
datepart(<datepart> , <date>) 
            --函數(shù)以整數(shù)值的形式返回日期的指定部分
getdate()   --函數(shù)以datetime 的缺省格式返回系統(tǒng)當(dāng)前的日期和時間


----系統(tǒng)函數(shù)----
app_name()      --函數(shù)返回當(dāng)前執(zhí)行的應(yīng)用程序的名稱
coalesce()      --函數(shù)返回眾多表達式中第一個非null 表達式的值
col_length(<‘table_name‘>, <‘column_name‘>)
                --函數(shù)返回表中指定字段的長度值
col_name(<table_id>, <column_id>)  
                --函數(shù)返回表中指定字段的名稱即列名
datalength()    --函數(shù)返回數(shù)據(jù)表達式的數(shù)據(jù)的實際長度
db_id([‘database_name‘])
                --函數(shù)返回數(shù)據(jù)庫的編號
db_name(database_id) 
                --函數(shù)返回數(shù)據(jù)庫的名稱
host_id()       --函數(shù)返回服務(wù)器端計算機的名稱
host_name()     --函數(shù)返回服務(wù)器端計算機的名稱
identity(<data_type>[, seed increment]) [as column_name])
                --identity() 函數(shù)只在select into 語句中
  --使用用于插入一個identity column列到新表中

/*select identity(int, 1, 1) as column_name
into newtable
from oldtable*/

isdate()        --函數(shù)判斷所給定的表達式是否為合理日期
isnull(<check_expression>, <replacement_value>)
                --函數(shù)將表達式中的null 值用指定值替換
isnumeric()     --函數(shù)判斷所給定的表達式是否為合理的數(shù)值
newid()         --函數(shù)返回一個uniqueidentifier 類型的數(shù)值
nullif(<expression1>, <expression2>)
                --nullif 函數(shù)在expression1 與expression2 相等時
  --返回null 值若不相等時則返回expression1 的值


====精妙SQL語句====

說明:復(fù)制表(只復(fù)制結(jié)構(gòu),源表名:a 新表名:b)
SQL: select * into b from a where 1<>1

說明:拷貝表(拷貝數(shù)據(jù),源表名:a 目標(biāo)表名:b)
SQL: insert into b(a, b, c) select d,e,f from b;

說明:顯示文章、提交人和最后回復(fù)時間
SQL: select a.title,a.username,b.adddate
     from table a,(select max(adddate) adddate from table where table.title=a.title) b

說明:外連接查詢(表名1:a 表名2:b)
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

說明:日程安排提前五分鐘提醒
SQL: select * from 日程安排 where datediff(‘minute‘,f開始時間,getdate())>5

說明:兩張關(guān)聯(lián)表,刪除主表中已經(jīng)在副表中沒有的信息
SQL:
delete from info where not exists ( select * from infobz where info.infid=infobz.infid )

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多