· 6 years ago · Jun 06, 2019, 10:22 AM
1use Mondial;
2
3-- select into tworzy baze z danymi
4
5select *
6into Miasta
7from city
8where country = 'PL';
9
10select * from Miasta
11
12drop table Miasta
13DROP TABLE IF EXISTS mIASTA;
14
15
16
17-- wstawianie nowych wierszy INSERT
18
19-- insert..values
20
21--specyfikacja do jakich tabel w nawiasie
22insert Miasta(Country, Province, Name, Population) values
23('PL', 'Plockie', 'Kutno', 50000),
24('PL', 'Katowickie', 'Bierun', 30000)
25
26select * from Miasta;
27
28
29insert Miasta values
30('Imielin', 'PL', 'Katowickie', 15000, null, null)
31
32-- insert..select
33
34insert Miasta
35select * from city where country = 'R'
36
37select * from Miasta;
38
39-- aktualizacja danych UPDATE
40
41update Miasta set
42 Population = 50000 where name = 'Ciechanow'
43
44-- transakcje
45
46begin transaction;
47
48select @@TRANCOUNT
49
50update Miasta set
51Population = 100000 -- error kurła ustawiliśmy wszędzie 50000
52where name = 'Ciechanow';
53select * from Miasta;
54
55rollback;
56commit;
57
58select * from Miasta;
59
60-- usuwanie wierszy DELETE
61
62delete Miasta
63where country = 'R';
64
65select * from Miasta
66
67-- truncate
68begin transaction;
69truncate table Miasta;
70select * from Miasta;
71rollback;
72select * from Miasta;
73
74-- synchronizacja tabel - MERGE
75
76select * from Miasta;
77
78select *
79into Miasta_Zmiany
80from Miasta
81where 1=2
82
83insert Miasta_Zmiany values
84('Pyskowice', 'PL', 'Katowickie', 40000, NULL, NULL),
85('Warsaw', 'PL', 'Warszawskie', 1800000, 21.0333, 52.2167);
86
87select * from Miasta_Zmiany
88-- upsert (u gory, kolokwialna nazwa)
89
90-- synchronizacja
91
92merge Miasta t
93 using Miasta_Zmiany s on t.Name = s.Name
94 when not matched then
95 insert(Name, Country, Province, Population)
96 values(s.Name, s.Country, s.Province, s.Population)
97 when matched then
98 update set
99 Country = s.Country,
100 Province = s.Province,
101 Population = s.Population
102output deleted.*, $action, inserted.*;
103
104--
105
106update Miasta set
107Population = Population * 0.9
108output deleted.*, inserted.*;
109
110-- tabele tymczasowe
111
112select Name, Population
113into #kraje
114from country;
115
116select * from #kraje;
117
118select @@SPID;
119
120select Name, Population
121into ##kraje
122from country;
123
124select * from ##kraje;
125
126select top(3) *
127into #top
128from country order by population;
129
130insert #top
131select top(3) *
132from country order by population desc;
133
134select * from #top;
135
136select * from #kraje;
137update #kraje set
138Population = Population * 1.1;
139
140select * from #kraje;
141
142delete #kraje where Population < 200000000;
143
144select * from #kraje;
145
146-- utworzenie nowej bazy danych
147
148create database TestSzkolenie;
149go
150use TestSzkolenie;
151
152select * from Arkusz1$;
153
154--
155
156select
157name Nazwa,
158Code Kod,
159Area Powierzchnia,
160Population Ludnosc
161into Kraje
162from Mondial.dbo.country
163
164select * from Kraje;