SQL 문구 모음

24837 단어 ql 문장
--                                      

--    

SELECT      --             

INSERT      --           

DELETE      --           

UPDATE      --          

--    

CREATE TABLE    --        

DROP TABLE     --        

ALTER TABLE     --        

CREATE VIEW     --      

DROP VIEW     --         

CREATE INDEX    --           

DROP INDEX     --         

CREATE PROCEDURE   --        

DROP PROCEDURE    --           

CREATE TRIGGER    --       

DROP TRIGGER    --          

CREATE SCHEMA    --           

DROP SCHEMA     --           

CREATE DOMAIN    --        

ALTER DOMAIN    --     

DROP DOMAIN     --          

--    

GRANT      --        

DENY      --      

REVOKE      --        

--    

COMMIT      --      

ROLLBACK     --      

SET TRANSACTION    --            

--   SQL

DECLARE      --       

EXPLAN      --           

OPEN      --            

FETCH      --        

CLOSE      --    

PREPARE      --       SQL   

EXECUTE      --     SQL   

DESCRIBE     --        





---    

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      SELECT   

waitfor delay ’01:02:03’

select * from employee

--      11   8      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    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)

     ---------    

     ---------        select        ,

     ---------       where      in   

  select distinct column_name form table_name --------- distinct         ,   

  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       ,all-     

  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*** ---         





  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 ......... -----         (    )

  alter table table_name drop constraint Stockname_default ----   Stockname default  

    

***function(/*    */)***





----    ----

AVG    --    

COUNT   --    

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()                





--STDEVP()

--STDEVP()         





--VAR()

--VAR()                 





--VARP()

--VARP()         





----    ----





/***    ***/

SIN(float_expression) --            

COS(float_expression) --            

TAN(float_expression) --            

COT(float_expression) --            

/***     ***/

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)

                       --                       

        --INTEGER/MONEY/REAL/FLOAT   

RADIANS(numeric_expression) --                       

        --INTEGER/MONEY/REAL/FLOAT   

EXP(float_expression)  --         

LOG(float_expression)  --           

LOG10(float_expression)--       10       

SQRT(float_expression) --         

/***      ***/

CEILING(numeric_expression)  --  >=                       

        --INTEGER/MONEY/REAL/FLOAT   

FLOOR(numeric_expression)    --  <=                       

        --INTEGER/MONEY/REAL/FLOAT   

ROUND(numeric_expression)    --   integer_expression               

        --          INTEGER/MONEY/REAL/FLOAT   

ABS(numeric_expression)      --                        

        --INTEGER/MONEY/REAL/FLOAT   

SIGN(numeric_expression)     --          0   1    -1          

        --        INTEGER/MONEY/REAL/FLOAT   

PI()       --    π  3.1415926535897936

RAND([integer_expression])   --    [integer_expression]      0-1        





----     ----

ASCII()         --               ASCII   

CHAR()   --     ASCII       

    --      0 ~ 255    ASCII   CHAR        NULL  

LOWER()   --             

UPPER()   --             

STR()   --                

LTRIM()   --             

RTRIM()   --             

LEFT(),RIGHT(),SUBSTRING()  --         

CHARINDEX(),PATINDEX()  --                      

SOUNDEX()  --            

    --SOUNDEX                SOUNDEX            0       

DIFFERENCE()    --     SOUNDEX                  

    --0   SOUNDEX              

    --1   SOUNDEX              

    --2   SOUNDEX               

    --3   SOUNDEX                

    --4   SOUNDEX          





QUOTENAME()  --                

/*select quotename('abc', '{') quotename('abc')

      

----------------------------------{

{abc} [abc]*/





REPLICATE()     --        character_expression         

/*select replicate('abc', 3) replicate( 'abc', -2)

      

----------- -----------

abcabcabc NULL*/





REVERSE()       --                  

REPLACE()       --                

/*select replace('abc123g', '123', 'def')

      

----------- -----------

abcdefg*/





SPACE()   --                 

STUFF()   --                     





----        ----

CAST()       

CAST() (<expression> AS <data_ type>[ length ])

CONVERT()       

CONVERT() (<data_ type>[ length ], <expression> [, style])





select cast(100+99 as char) convert(varchar(12), getdate())

      

------------------------------ ------------

199   Jan 15 2000





----    ----

DAY()   --    date_expression      

MONTH()   --    date_expression      

YEAR()   --    date_expression      

DATEADD(<datepart> ,<number> ,<date>) 

    --        date            number       

DATEDIFF(<datepart> ,<number> ,<date>)

    --           datepart        

DATENAME(<datepart> , <date>)  --                  

DATEPART(<datepart> , <date>)  --                  

GETDATE()  --   DATETIME                  





----    ----

APP_NAME()      --                

COALESCE()  --              NULL      

COL_LENGTH(<'table_name'>, <'column_name'>) --              

COL_NAME(<table_id>, <column_id>)   --                

DATALENGTH() --                 

DB_ID(['database_name']) --          

DB_NAME(database_id)  --          

HOST_ID()     --              

HOST_NAME()     --              

IDENTITY(<data_type>[, seed increment]) [AS column_name])

--IDENTITY()     SELECT INTO            identity column     

/*select identity(int, 1, 1) as column_name

into newtable

from oldtable*/

ISDATE()  --                  

ISNULL(<check_expression>, <replacement_value>) --        NULL        

ISNUMERIC()  --                   

NEWID()   --      UNIQUEIDENTIFIER      

NULLIF(<expression1>, <expression2>)

--NULLIF    expression1  expression2      NULL          expression1   

  SQL  

  :   (     ,   :a    :b)

SQL: select * into b from a where 1<>1

  :   (    ,   :a     :b)

SQL: insert into b(a, b, c) select d,e,f from b;

  :    、          

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



  :     ,                

SQL: 

delete from info where not exists ( select * from infobz where info.infid=infobz.infid ) 

  :--

SQL: 

SELECT A.NUM, A.NAME, B.UPD_DATE, B.PREV_UPD_DATE

FROM TABLE1, 

(SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE PREV_UPD_DATE

FROM (SELECT NUM, UPD_DATE, INBOUND_QTY, STOCK_ONHAND

FROM TABLE2

WHERE TO_CHAR(UPD_DATE,'YYYY/MM') = TO_CHAR(SYSDATE, 'YYYY/MM')) X, 

(SELECT NUM, UPD_DATE, STOCK_ONHAND

FROM TABLE2

WHERE TO_CHAR(UPD_DATE,'YYYY/MM') = 

TO_CHAR(TO_DATE(TO_CHAR(SYSDATE, 'YYYY/MM') || '/01','YYYY/MM/DD') - 1, 'YYYY/MM') ) Y, 

WHERE X.NUM = Y.NUM (+)

AND X.INBOUND_QTY + NVL(Y.STOCK_ONHAND,0) <> X.STOCK_ONHAND ) B

WHERE A.NUM = B.NUM

  :--

SQL: 

select * from studentinfo where not exists(select * from student where studentinfo.id=student.id) and    ='"&strdepartmentname&"' and     ='"&strprofessionname&"' order by   ,   ,     

  :

                 (                )

SQL: 

SELECT a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy') AS telyear,

SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '01', a.factration)) AS JAN,

SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '02', a.factration)) AS FRI,

SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '03', a.factration)) AS MAR,

SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '04', a.factration)) AS APR,

SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '05', a.factration)) AS MAY,

SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '06', a.factration)) AS JUE,

SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '07', a.factration)) AS JUL,

SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '08', a.factration)) AS AGU,

SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '09', a.factration)) AS SEP,

SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '10', a.factration)) AS OCT,

SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '11', a.factration)) AS NOV,

SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '12', a.factration)) AS DEC

FROM (SELECT a.userper, a.tel, a.standfee, b.telfeedate, b.factration

FROM TELFEESTAND a, TELFEE b

WHERE a.tel = b.telfax) a

GROUP BY a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy')

  :      :

SQL: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....

  :           ID 

SQL:

SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THEN MIN(HandleID) + 1 ELSE 1 END) as HandleID

FROM Handle

WHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a)



:   ms sql      ,              

:          。

:            ,            。

:   database       oracle ,





select distinct * into #table_name from table_name

delete from table_name

select * into table_name from #table_name

drop table #table_name



      “select into”  ,        

    ,     ,   Query Analyzer   

execute sp_dboption 'db_name','select into','true'

  。       。

좋은 웹페이지 즐겨찾기