24
卢宇森([email protected]) 雷中华([email protected]) 1 目录 一 初识 SQL .......................................................................................................... 2 1 常用 SQL 语句............................................................................................................................................................. 2 2 几个高级查询运算符 .................................................................................................................................................. 2 3 连接 .............................................................................................................................................................................. 3 4 经典 SQL 语句示例..................................................................................................................................................... 4 二 SQL EXPRESSION.......................................................................................... 6 1 SQL EXPRESSION 在ARCMAP 中的应用 ................................................................................................................ 6 1.1 数据管理 ......................................................................................................................................................... 6 1.1.1 提取(Extract) ................................................................................................................................................................ 6 1.1.2 转换(Conversion) ....................................................................................................................................................... 6 1.1.3 更新 ............................................................................................................................................................................... 7 1.2 地图制图 ......................................................................................................................................................... 7 1.2.1 显示 ............................................................................................................................................................................... 7 1.2.2 标注 ............................................................................................................................................................................... 8 1.3 地理处理 ......................................................................................................................................................... 8 1.3.1 Layer 相关 ................................................................................................................................................................... 8 1.3.2 Model Builder............................................................................................................................................................. 9 1.3.3 线性参考(Linear Referencing) .................................................................................................................................. 9 2 SQL EXPRESSION 概览........................................................................................................................................... 10 2.1 超简单语句-直接查询.................................................................................................................................. 10 2.2 简单语句-简单函数、运算符的使用 .......................................................................................................... 11 2.3 "and" 和 "or" ............................................................................................................................................. 11 2.4 "in" 和 "not in" .......................................................................................................................................... 11 2.5 嵌套 SQL 语句.............................................................................................................................................. 12 2.6 Null................................................................................................................................................................ 12 2.7 通配符(From MS Access Help)................................................................................................................ 13 2.8 复杂 sql 语句 ................................................................................................................................................ 14 三 FIELD CALCULATOR 中使用 SQL 语句 ...................................................... 15 1 利用 SQL 语句把 DLBM 的值赋给 DLBM1 ............................................................................................................... 15 2 表间计算-已有 DLBM1 根据按照代码名称对照表赋 DLMC1 ................................................................................. 17 3 土地利用现状数据库地类图斑层扣除线状地物面积字段计算............................................................................. 18 四 VBE 环境下 ADO 方式执行 SQL .................................................................. 21 1 创建 ADO CONNECTION....................................................................................................................................... 21 2 执行 SQL 更新语句(UPDATE) ............................................................................................................................... 22 3 查询某查询条件返回记录数 ................................................................................................................................ 23

ESRI中国社区学习心得之ArcMap中的SQL语句

Embed Size (px)

DESCRIPTION

syntax

Citation preview

  • ([email protected]) ([email protected]) 1

    SQL..........................................................................................................2

    1 SQL .............................................................................................................................................................22 ..................................................................................................................................................23 ..............................................................................................................................................................................34 SQL .....................................................................................................................................................4

    SQL EXPRESSION..........................................................................................61 SQL EXPRESSION ARCMAP ................................................................................................................6

    1.1 .........................................................................................................................................................61.1.1 (Extract)................................................................................................................................................................61.1.2 (Conversion).......................................................................................................................................................61.1.3 ...............................................................................................................................................................................7

    1.2 .........................................................................................................................................................71.2.1 ...............................................................................................................................................................................71.2.2 ...............................................................................................................................................................................8

    1.3 .........................................................................................................................................................81.3.1 Layer ...................................................................................................................................................................81.3.2 Model Builder.............................................................................................................................................................91.3.3 (Linear Referencing)..................................................................................................................................9

    2 SQL EXPRESSION ...........................................................................................................................................102.1 -..................................................................................................................................102.2 -..........................................................................................................112.3 "and" "or".............................................................................................................................................112.4 "in" "not in"..........................................................................................................................................112.5 SQL ..............................................................................................................................................122.6 Null................................................................................................................................................................122.7 (From MS Access Help)................................................................................................................132.8 sql ................................................................................................................................................14

    FIELD CALCULATOR SQL ......................................................151 SQL DLBM DLBM1...............................................................................................................152 - DLBM1 DLMC1.................................................................................173 .............................................................................18

    VBE ADO SQL..................................................................211 ADO CONNECTION.......................................................................................................................................212 SQL (UPDATE)...............................................................................................................................223 ................................................................................................................................23

  • ([email protected]) ([email protected]) 2

    ESRI --- ArcMap SQL

    SQL(Structured Query Language)

    SQL SQL

    ArcMap SQL

    SQL SQL SQL SQL 4 1 (DDL)CREATEDROPALTER 2 (DML)INSERTUPDATEDELETE 3 (DQL)SELECT 4 (DCL)GRANTREVOKECOMMITROLLBACK ArcMap 23 select

    1 SQL select * from table1 where insert into table1(field1,field2) values(value1,value2) delete from table1 where update table1 set field1=value1 where select * from table1 where field1 like %value1% ---like ! select * from table1 order by field1,field2 [desc] select count as totalcount from table1 select sum(field1) as sumvalue from table1 select avg(field1) as avgvalue from table1 select max(field1) as maxvalue from table1 select min(field1) as minvalue from table1

    2 1UNION UNION TABLE1 TABLE2

    ALL UNION UNION ALL TABLE1 TABLE2

  • ([email protected]) ([email protected]) 3

    2EXCEPT EXCEPT TABLE1 TABLE2

    ALL EXCEPT (EXCEPT ALL) 3 INTERSECT INTERSECT TABLE1 TABLE2

    ALL INTERSECT (INTERSECT ALL)

    3 1. INNER JOIN 2. LEFT JOIN 3. RIGHT JOIN 4.LEFT OUTER JOIN

    A B ID NAME ID CLASS 1 IBM 1 C1 2 SONY 3 C3 3 BMW 4 C4

    1 INNER JOIN: SELECT * FROM A INNER JOIN B ON A.ID=B.ID

    ID NAME CLASS 1 IBM C1 3 BMW C3

    ID 2 LEFT JOIN : SELECT * FROM A LEFT JOIN B ON A.ID=B.ID

    ID NAME CLASS1 IBM C1 2 SONY null 3 BMW C3

    A(left ) B 3 RIGHT JOIN : SELECT * FROM A RIGHT JOIN B ON A.ID=B.ID

    ID NAME CLASS 1 IBM C1 3 BMW C3 4 null C4

    RIGHT() B(right ) A 4 FULL OUTER JOIN: SELECT * FROM A FULL OUTER JOIN B ON A.ID=B.ID

    ID NAME CLASS 1 IBM C1

  • ([email protected]) ([email protected]) 4

    2 SONY null 3 BMW C3 4 null C4

    4 SQL mysql access 1(,a b) (Access ) select * into b from a where 11 select top 0 * into b from a 2(,a b) (Access ) insert into b(a, b, c) select d,e,f from b; 3() (Access ) insert into b(a, b, c) select d,e,f from b in where insert into b(a, b, c) select d,e,f from [].b where ..from b in "&Server.MapPath(".")&"\data.mdb" &" where.. 4( 1a 2b) select a,b,c from a where a IN (select d from b ) : select a,b,c from a where a IN (1,2,3) 5 select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table

    where table.title=a.title) b 6( 1a 2b) select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c 7( 1a ) select * from (Select a,b,c FROM a) T where t.a > 1; 8between ,between ,not between select * from table1 where time between time1 and time2 select a,b,c, from table1 where a not between 1 and 2 9in select * from table1 where a [not] in ( 1, 2, 4, 6) 10 delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 ) 11 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 ..... 12 select * from where datediff(minute,f ,getdate())>5 13 SQL select top 10 b.* from (select top 20 , from order by desc) a,

  • ([email protected]) ([email protected]) 5

    b where b. = a. order by a. 14 10 select top 10 * form table1 where 15 b a (

    ,,,.) select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b) 16 TableA TableB TableC

    (select a from tableA ) except (select a from tableB) except (select a from tableC) 17 10 select top 10 * from tablename order by rnd(id ) 18 select rnd( id ) 19 Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...) 20 select name from sysobjects where type=U 21 select name from syscolumns where id=object_id(TableName) 22 typevenderpcs type case

    select case select type,sum(case vender when A then pcs else 0 end),sum(case vender when C

    then pcs else 0 end),sum(case vender when B then pcs else 0 end) FROM tablename group by type

    type vender pcs A 1 A 1 B 2 A 2 B 3 C 3

    23 10 15 select top 5 * from (select top 15 * from table order by id asc) table_ order by id desc

    ArcMap ArcMap SQL

  • ([email protected]) ([email protected]) 6

    SQL Expression SQLArcMapSQL ExpressionESRI

    SQL Reference

    1 SQL ExpressionArcMap SQL Expression

    1.1

    1.1.1 (Extract)

    Query Builder

    Select Analysis\Extract Table Select Analysis\Extract Extract by Attributes Spatial\Analyst

    1.1.2 (Conversion) Feature Class To Feature Class Conversion\To Geodatabase Table To Table Conversion\To Geodatabase CADLinestoPolygonFeatures CAD Samples\Conversion\To From CAD

    A. Feature Class To FeatureClass ArcToolbox ArcCatalog import\Feature

    Class(Single) B. CADLinestoPolygonFeatures ESRI Model Builder Model Query

    Builder CAD CAD polyline annotation layer = xx

  • ([email protected])

    1.1.3 Select By Attribut Tools Field Calculator set Null Spatial Analyst\Conditional

    1.2 ArcMap SQL Expression

    ArcMap Drawing Errors

    [A] SQL Expression [A] = 1

    1.2.1

    Layer Properties\ Definition Query Distributed Geodatabase Query Definition Query Get Cout

    7 ([email protected])

  • ([email protected])

    1.2.2

    Layer Properties\ Labels \ classes labels SQL Expression

    Lable http://bbs.esrichina-bj.cn/esri/viewthread.php?tid=55808

    1.3

    1.3.1 Layer

    ArcGIS layer 2

    select layer by attribute Tools\Select by Attribute Layers and Table Views Make Feature Layer Make Query Table

    8 ([email protected])

  • ([email protected])

    1.3.2 ModelBuilder

    SQL Expression CAD polyline annotation CAD Lines to Polygon Features (Samples) 1.1.2 SQL Expression Create Variable

    1.3.3 (LinearReferencing)

    Diligentpig ArcGIS / http://bbs.esrichina-bj.cn/esri/viewthread.php?tid=56638

    9 ([email protected])

  • ([email protected])

    2 SQLExpression

    Select By Attribute Query Builder ArcGIS SQL

    SELECT * FROM layer WHERE SQL WHERE .1

    2.1 -

    1 dltb dlbm 011 [DLBM] =011 2 dltb dlbm dlbm1 [DLBM] [DLBM1] [] PGDB ArcGIS

    10 ([email protected])

  • ([email protected]) ([email protected]) 11

    2.2 -

    (1) dltb dlbm 01 Left(TRIM([dlbm]),2)=01 (2) dlmc [dlmc] like **

    Personal Geodatabase shapefile

    Function Examples Function Examples mod [] mod 2 = 0 mod mod("",2)=0 left left([],2)="Ts" substr substr("",1,2)=Ts 2 mid mid([],2,2)="so" substr substr("",2,2)=so 2 2 len len([])= 4 char_length char_length( "" ) = 4 instr instr([],"o")=2 position position(o in "") = 2 a A - []="tsonghua"

    lower lower("")=tsonghua pGDB lower() pGDB Lcase() lcase lcase([])="tsonghua"

    ucase ucase([])="TSONGHUA" upper upper("")=TSONGHUA

    - [] > 2 ; [] = "2" - "" > 2 : "" = 2 pGDB []fGDB ""

    #wildcard# [] like "2*" or [] like "?2" #wildcard# "" like 2% or "" like _2 * & ? VS. % & _

    #[]# [BIRTH] = #06-13-2001 19:30:00# date "BIRTH" = date 2001-06-13 19:30:00

    pGDB #fGDB date

    PGDB shp http://bbs.esrichina-bj.cn/esri/viewthread.php?tid=60118

    2.3 "and" "or"

    1 dlbm 011 dlmc [dlbm]=0110 and [dlmc] = 2 dlbm 011 dlmc [dlbm]=0110 or dlmc =

    2.4 "in" "not in"

    1 dlbm 011001200130

  • ([email protected]) ([email protected]) 12

    [dlbm]= 011 or [dlbm]= 012 or [dlbm]= 013 in [dlbm] in (011, 012,013) in not in

    2.5 SQL

    1 10 [OBJECTID] in (select top 10 [OBJECTID] from dltb order by rnd( [OBJECTID] ) ) 2 OBJECTID 11 15 [OBJECTID] in (select top 5 OBJECTID from (select top 15 * from dltb order by OBJECTID asc) b order by OBJECTID desc) 2 1 [OBJECTID] not in (select max(OBJECTID) from dltb group by dlbm) 2 [dlbm] in (select dlbm from dltb group by dlbm having count(*) >1) 3 mdb pgdb sql mdb mdb [dlbm] in (select dlbm from [c:\tempdata.mdb].) tempdata.mdb dlbm http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=55857 sql http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=53458 http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=54652

    2.6 Null

    "Null" ""0 Null Null Null [] is Null [] = Null

  • ([email protected]) ([email protected]) 13

    2.7 (From MS Access Help)

    Like ? _ * % # (0 - 9) [] [!]

    ([ ])

    ANSI ANSI Microsoft Windows 8 256 0 255ASCII ANSI ANSI Microsoft Windows 8 256 0 255ASCII ANSI ([ ) (?) (#) (*) ( ])

    (-) [A-Z] A Z [a-zA-Z0-9]

    ANSI SQL (%) (_) Microsoft Access Microsoft Office Access 2007 OLE DB Provider Microsoft Office Access 2007 DAO 1. (!) 2. (-) ANSI 3.A-Z 0-100[A-Z] [Z-A] 4. [ ] (" ") (" ") ("")

  • ([email protected]) ([email protected]) 14

    LikeFrom MS Access Help True False a*a aaaBaaBBBa aBC *ab* abcAABBXab aZbbac a[*]a a*a aaa ab* abcdefgabc cabaab a?a aaaa3aaBa aBBBa a#a a0aa1aa2a aaaa10a [a-z] fpj 2& [!a-z] 9&% ba [!0-9] Aa&~ 019 a[!b-m]# An9az0a99 abcaj0

    P A F Like P[A-F]###

    2.8 sql

    [OBJECTID] in (select objectid from (select objectid, xzqdm as ,tbh as Dltbbh,dltb_xzdwmj as ,xzdw_mj as , iif(abs((dltb_xzdwmj-xzdw_mj))>0.0001,abs(dltb_xzdwmj-xzdw_mj),0) as DIfferent from (select a.objectid, a.zldwdm as xzqdm,a.xzdwmj as dltb_xzdwmj,a.tbbh as tbh,b.xzdwmj as xzdw_mj from dltb a inner join (select sum(xwmj) as xzdwmj,kctbbh1,kctbdwdm1 from (SELECT sum(xzdwmj*kcbl) as xwmj,kctbbh1,kctbdwdm1 from xzdw group by kctbbh1,kctbdwdm1 union all SELECT sum(xzdwmj*(1-kcbl)) as xwmj,kctbbh2 as kctbbh1,kctbdwdm2 as kctbdwdm1 from xzdw group by kctbbh2,kctbdwdm2 )group by kctbbh1,kctbdwdm1 order by kctbbh1) b on a.zldwdm=b.kctbdwdm1 and a.tbbh=b.kctbbh1)) where abs(DIfferent)>0.0001 order by objectid) SQL Expression

  • ([email protected])

    Field CalculatorSQL ArcGIS

    SQL Field Calculator SQL ExpressionESRI SQL Field Calculator Field Calculator AO AO SQL Field Calculator

    1 SQL dlbm dlbm1 IWorkspace Interface ExecuteSQL Method SQL Field Calculator Field CalculatorSql Field Calculator Flag Model Builder Make Feature Layer SQL Expression Calculate Field

    15 ([email protected])

  • ([email protected])

    -------------------- @ lucy1114 20100625 -------------------- static flag as long if flag = 0 then Dim pMxDoc As IMxDocument Dim pDSName As IDatasetName Dim i As Integer Dim pdl2 As IDataLayer2

    16 ([email protected])

  • ([email protected]) ([email protected]) 17

    Dim scommand i = 0 0 Set pMxDoc = ThisDocument Set pdl2 = pMxDoc.FocusMap.Layer(i) Set pDSName = pdl2.DataSourceName Dim pWorkspaceFactory As IWorkspaceFactory Dim pWorkspace As IWorkspace Set pWorkspaceFactory = New AccessWorkspaceFactory Set pWorkspace = pWorkspaceFactory.OpenFromFile(pDSName.WorkspaceName.PathName, 0) ------------------------------------------------------- scommand = "update " & pDSName.name & " set dlbm1=dlbm" ------------------------------------------------------- pWorkspace.ExecuteSQL scommand flag = 1 end if Field Calculator SQL SQL

    2 - dlbm1 dlmc1 scommand

  • ([email protected])

    scommand = "update dltb left join on dltb.dlbm1=.dlbm set dltb.dlmc1=.dlmc"

    3 ArcGIS Desktop http://bbs.esrichina-bj.cn/ESRI/thread-67896-1-3.html SQL

    18 ([email protected])

  • ([email protected])

    -------------------- @ lucy1114 010625 -------------------- static flag as long if flag = 0 then Dim pMxDoc As IMxDocument Dim pDSName As IDatasetName Dim i As Integer Dim pdl2 As IDataLayer2 Dim scommand i = 0 0 Set pMxDoc = ThisDocument Set pdl2 = pMxDoc.FocusMap.Layer(i) Set pDSName = pdl2.DataSourceName Dim pWorkspaceFactory As IWorkspaceFactory Dim pWorkspace As IWorkspace Set pWorkspaceFactory = New AccessWorkspaceFactory

    19 ([email protected])

  • ([email protected]) ([email protected]) 20

    Set pWorkspace = pWorkspaceFactory.OpenFromFile(pDSName.WorkspaceName.PathName, 0) scommand ="Create TABLE temp_luys (fid_dltb long not null, mj double) " pWorkspace.ExecuteSQL scommand scommand = "Insert into temp_luys select fid_dltb,round(sum(iif(kcbl = 1,kd * Shape_Length, kd * Shape_Length/2)),2) as mj from XZDW_Intersect group by fid_dltb" pWorkspace.ExecuteSQL scommand scommand = "update dltb left join temp_luys b on dltb.OBJECTID_1=b.fid_dltb set dltb.xzdwmj=b.mj" pWorkspace.ExecuteSQL scommand scommand = "DROP TABLE temp_luys" pWorkspace.ExecuteSQL scommand flag = 1 end if Workspace scommand = "update dltb set xzdwmj = dsum(iif(kcbl = 1,kd * Shape_Length, kd * Shape_Length/2),XZDW_Intersect,fid_dltb= & dltb.OBJECTID)" Field Calculator SQL sql access sql sql Sql

  • ([email protected])

    VBEADOSQL personal Geodatabase MS Access personal Geodatabase Microsoft ActiveX Data Objects 2.x SQL

    1 ADO Connection

    ' --------------------------------------------------------------------------- 'ADO Connection 'ADO Recordset 'sql 'mdb Workspace ' --------------------------------------------------------------------------- Public Cn As New ADODB.Connection

    21 ([email protected])

  • ([email protected]) ([email protected]) 22

    Public Rs As ADODB.RecordSet Public strSql As String Public strMDBpath As String ' --------------------------------------------------------------------------- '* : Cn '* : mdbpath String '* : Tsonghua '* : 2010-06-26 ' --------------------------------------------------------------------------- Public Sub mdbConnection(mdbpath As String) If VBA.Trim(mdbpath) "" Then With Cn .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " & mdbpath & ";" .CommandTimeout = 30 .CursorLocation = adUseClient If Cn.State = 0 Then .Open End If End With Else strLog = strLog & vbCrLf & "- ..." Exit Sub End If End Sub

    2 SQL (update)

    ' --------------------------------------------------------------------------- '* : SQL Update '* : mdbpath String '* sql String SQL

    '* : Tsonghua '* : 2010-06-26 ' --------------------------------------------------------------------------- Sub ExecuteSqlUpdate(ByVal strmdbpath As String, ByVal sql As String) Call mdbConnection(strmdbpath) Cn.Execute sql

  • ([email protected]) ([email protected]) 23

    Cn.Close End Sub strSql = Update DLTB a , b set a.DLBM = b. where a.DLMC = b. call ExecuteSqlUpdate(strSql)

    3

    ' --------------------------------------------------------------------------- '* : SQLSelect '* : mdbpath String '* sql String SQL '* : Tsonghua '* : 2010-06-26 ' --------------------------------------------------------------------------- Function lngNull(ByVal sql As String, ByVal mdbpath As String) As Long Call mdbConnection(mdbpath) Set Rs = Cn.Execute(sql) lngNull = Rs.RecordCount Rs.Close Cn.Close End Function

    Dim lngNullBSM As Long strSql = select objectid from dltb where isnull(bsm) = true or bsm = 0 lngNullBSM= lngNull(strmdbpath,strSql)

  • ([email protected]) ([email protected]) 24

    @Shanghai 6/26/2010 6:08:01 PM

    SQL SQL Expression1 SQL ExpressionArcMap2 SQL Expression

    Field CalculatorSQL VBEADOSQL