1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 |
USE WideWorldImporters -- Create Transact-SQL SELECT queries -- 001 SELECT * FROM Orders -- 002 SELECT name FROM sys.servers WHERE server_id = 0 -- 003 SELECT * FROM [DESKTOP-LO5S40T].Wideworldimporters.sales.orders -- 004 SELECT d.name ,MAX(b.backup_finish_date) AS 'Most recent full backup' FROM master.sys.databases d LEFT OUTER JOIN msdb..backupset b ON d.name = b.database_name WHERE d.name <> 'tempdb' AND (b.type = 'D' OR b.type IS NULL) GROUP BY d.name ORDER BY d.name; -- 005 CREATE TABLE dbo.Orders (id int) SELECT * FROM dbo.Orders DROP TABLE dbo.Orders -- 006 SELECT name ,default_schema_name FROM sys.database_principals WHERE principal_id = user_id(); -- 007 CREATE SYNONYM [dbo].[Orders] FOR [WideWorldImporters].[Sales].[Orders] SELECT * FROM dbo.Orders -- 008 DROP SYNONYM [dbo].[Orders] -- 009 SELECT master.sys.databases.name -- 010 SELECT o.* FROM (SELECT * FROM Sales.Orders) o -- SELECT clause -- 011 SELECT OrderID as 'Order number' FROM Sales.Orders SELECT 'Order number' = OrderID FROM Sales.Orders -- 012 SELECT UPPER(FullName) FROM [Application].[People] --WHERE clause -- 013 SELECT * FROM [Application].[People] WHERE PersonID = 1 -- 014 SELECT * FROM [Application].[People] WHERE FullName = 'Kayla Woodcock' -- 015 SELECT * FROM [Application].[People] WHERE EmailAddress = NULL -- 016 SELECT * FROM [Application].[People] WHERE EmailAddress IS NULL -- 017 SELECT * FROM [Application].[People] WHERE FullName LIKE 'Daniel%' -- 018 SELECT * FROM [Application].[People] WHERE PreferredName LIKE 'Isabell_' -- 019 SELECT * FROM [Application].[People] WHERE PreferredName LIKE 'Isabell[a-z]' -- 020 SELECT * FROM [Application].[People] WHERE PreferredName IN ('Isabelle', 'Isabell') -- 021 SELECT * FROM [Application].[People] WHERE PreferredName IN ( SELECT CustomerName FROM sales.Customers ) -- 022 SELECT * FROM [Application].[People] WHERE PreferredName BETWEEN 'I' AND 'Isabelle' -- 023 SELECT * FROM [Application].[People] WHERE PersonID BETWEEN 1 AND 10 -- 024 SELECT GETDATE() -- 025 SELECT * FROM [Application].[People] WHERE ValidTo > GETDATE() -- 026 SELECT LEFT('Isabelle', 3) -- 027 SELECT * FROM [Application].[People] WHERE LEFT(PreferredName,3) = 'Isa' SELECT * FROM [Application].[People] WHERE PreferredName LIKE 'Isa%' -- 028 DECLARE @FirstName varchar(50) SET @FirstName = 'Isabelle' SELECT * FROM [Application].[People] WHERE PreferredName = @FirstName SELECT * FROM [Application].[People] WHERE PreferredName = 'Isabelle' -- 029 DECLARE @FullName varchar(50) SELECT @FullName = FullName FROM [Application].[People] WHERE PreferredName = 'Isabelle' SELECT @FullName -- 030 SELECT * FROM [Application].[People] WHERE 1 = 1 -- 031 SELECT * FROM [Application].[People] WHERE PersonID = 1 AND PersonID = 2 -- 032 SELECT * FROM [Application].[People] WHERE PersonID = 1 OR PersonID = 2 --033 SELECT * FROM [Application].[People] WHERE PersonID IN (1, 2) -- 034 SELECT * FROM [Application].[People] WHERE (PersonID = 1 OR PersonID = 2) AND PreferredName = 'Isabella' SELECT * FROM [Application].[People] WHERE PersonID = 1 OR (PersonID = 2 AND PreferredName = 'Isabella') /* ORDER BY clause */ -- 035 SELECT * FROM [Application].[People] ORDER BY PreferredName -- 036 SELECT * FROM [Application].[People] ORDER BY PreferredName DESC -- 037 SELECT * FROM [Application].[People] ORDER BY PreferredName, PersonID -- 038 SELECT PreferredName FROM [Application].[People] ORDER BY ValidFrom /* Set operators */ -- 039 SELECT 1, 'record 1' UNION SELECT 2, 'record 2' -- 040 SELECT 1, 'record 1' UNION SELECT 1, 'record 2' SELECT 1, 'record 1' UNION ALL SELECT 1, 'record 2' -- 041 SELECT 1, 'record 1' as Alias1 UNION SELECT 2 as Alias2, 'record 2' as Alias3 -- 042 SELECT 1, 'record 1' UNION SELECT 2 , GETDATE() -- 043 SELECT 1, 'record 1' EXCEPT SELECT 1, 'record 1' -- 044 SELECT 1, 'record 1' INTERSECT SELECT 1, 'record 1' -- 045 USE TestDB GO CREATE TABLE dbo.Customers ( CustomerID tinyint NOT NULL IDENTITY ,FirstName varchar(100) NOT NULL ,LastName varchar(100) NOT NULL ,[Address] varchar(100) NOT NULL ); -- 046 CREATE TABLE dbo.Employees ( EmployeeID tinyint NOT NULL IDENTITY ,FirstName varchar(100) NOT NULL ,LastName varchar(100) NOT NULL ,[Address] varchar(100) NOT NULL ); -- 047 INSERT dbo.Customers VALUES ('Bob', 'Jackson', 'Main street 1, Dallas'); INSERT dbo.Customers VALUES ('Frank', 'Smith', 'Second street 2, Miami'); INSERT dbo.Customers VALUES ('Joe', 'Johnson', 'Third Avenue 3, New York'); INSERT dbo.Employees VALUES ('Jack', 'Ford', 'Second street 2, Denver'); INSERT dbo.Employees VALUES ('Donald', 'Charleston', 'Times Square, New York'); INSERT dbo.Employees VALUES ('Bob', 'Jackson', 'Main street 1, Dallas'); -- 048 SELECT FirstName, LastName, Address FROM Customers UNION SELECT FirstName, LastName, Address FROM Employees -- 049 DROP TABLE dbo.Customers; DROP TABLE dbo.Employees; /* Section 2: Querying multiple tables by using joins */ -- 050 SELECT * FROM Sales.Orders ,Sales.OrderLines -- 051 SELECT * FROM Sales.Orders CROSS JOIN Sales.OrderLines -- 052 SELECT * FROM Sales.Orders ,Sales.OrderLines WHERE Sales.Orders.OrderID = Sales.OrderLines.OrderID -- 053 SELECT * FROM Sales.Orders o ,Sales.OrderLines ol WHERE o.OrderID = ol.OrderID -- 054 SELECT * FROM Sales.Orders o ,Sales.OrderLines ol WHERE o.OrderID = ol.OrderID AND o.OrderDate = '2014-07-17' -- 055 SELECT * FROM Sales.Orders o JOIN Sales.OrderLines ol ON o.OrderID = ol.OrderID WHERE o.OrderDate = '2014-07-17' -- 056 SELECT * FROM warehouse.StockItems WHERE StockItemID NOT IN ( SELECT StockItemID FROM sales.Orderlines) -- 057 CREATE TABLE dbo.Customers ( CustomerID tinyint NOT NULL IDENTITY PRIMARY KEY ,FirstName varchar(100) NOT NULL ,LastName varchar(100) NOT NULL ,[Address] varchar(100) NOT NULL ); CREATE TABLE dbo.Orders ( OrderID tinyint NOT NULL IDENTITY PRIMARY KEY ,CustomerID tinyint NOT NULL ,OrderDate datetime NOT NULL ,SalesAmount decimal(18,2) NOT NULL ); INSERT dbo.Customers VALUES ('Bob', 'Jackson', 'Main street 1, Dallas'); INSERT dbo.Customers VALUES ('Frank', 'Smith', 'Second street 2, Miami'); INSERT dbo.Customers VALUES ('Joe', 'Johnson', 'Third Avenue 3, New York'); INSERT dbo.Orders VALUES (1, '2011-01-01', 30.02); INSERT dbo.Orders VALUES (2, '2012-02-02', 15.99); INSERT dbo.Orders VALUES (4, '2014-04-04', 107.00); -- 058 SELECT * FROM dbo.Customers c LEFT OUTER JOIN dbo.Orders o ON c.CustomerID = o.CustomerID -- 059 SELECT * FROM dbo.Customers c RIGHT OUTER JOIN dbo.Orders o ON c.CustomerID = o.CustomerID -- 060 INSERT dbo.Orders VALUES (1, '2015-05-05', 230.02); -- 061 SELECT * FROM dbo.Customers c FULL OUTER JOIN dbo.Orders o ON c.CustomerID = o.CustomerID -- 062 CREATE TABLE dbo.OrderLines ( OrderLineID tinyint NOT NULL ,OrderID tinyint NOT NULL ,Product varchar(100) NOT NULL ,Units tinyint NOT NULL ,UnitPrice decimal(18,2) NOT NULL ); INSERT dbo.OrderLines VALUES (1, 1, 'ProductA', 1, 10.04); INSERT dbo.OrderLines VALUES (2, 1, 'ProductB', 2, 9.99); -- 063 SELECT * FROM dbo.Customers c INNER JOIN dbo.Orders o ON c.CustomerID = o.CustomerID LEFT OUTER JOIN dbo.OrderLines ol on ol.OrderID = o.OrderID; -- 064 SELECT c.FirstName ,c.LastName ,o.OrderDate ,o.SalesAmount ,ol.Product ,ol.Units ,ol.UnitPrice FROM dbo.Customers c INNER JOIN dbo.Orders o ON c.CustomerID = o.CustomerID LEFT OUTER JOIN dbo.OrderLines ol on ol.OrderID = o.OrderID ORDER BY c.CustomerID, o.OrderID, ol.OrderLineID; -- 065 DROP TABLE dbo.Customers; DROP TABLE dbo.Orders; DROP TABLE dbo.OrderLines; -- Construct multiple JOIN operators using AND and OR -- 066 CREATE TABLE dbo.Customers ( FirstName varchar(100) NOT NULL ,LastName varchar(100) NOT NULL ,[Address] varchar(100) NOT NULL ); ALTER TABLE dbo.Customers ADD CONSTRAINT PK_Customers PRIMARY KEY (FirstName, LastName) GO CREATE TABLE dbo.Orders ( OrderID tinyint NOT NULL IDENTITY PRIMARY KEY ,FirstName varchar(100) NOT NULL ,LastName varchar(100) NOT NULL ,OrderDate datetime NOT NULL ,SalesAmount decimal(18,2) NOT NULL ); INSERT dbo.Customers VALUES ('Bob', 'Jackson', 'Main street 1, Dallas'); INSERT dbo.Customers VALUES ('Frank', 'Smith', 'Second street 2, Miami'); INSERT dbo.Customers VALUES ('Joe', 'Johnson', 'Third Avenue 3, New York'); INSERT dbo.Orders VALUES ('Bob', 'Jackson', '2011-01-01', 30.02); INSERT dbo.Orders VALUES ('Frank', 'Smith', '2012-02-02', 15.99); INSERT dbo.Orders VALUES ('Bob', 'Smith', '2014-04-04', 107.00); -- 067 SELECT * FROM dbo.Customers c FULL OUTER JOIN dbo.Orders o ON c.FirstName = o.FirstName AND c.LastName = o.LastName -- 068 DROP TABLE dbo.Customers; DROP TABLE dbo.Orders; -- 069 CREATE TABLE dbo.Student ( CustomerID tinyint NOT NULL IDENTITY PRIMARY KEY ,FirstName varchar(100) NOT NULL ,LastName varchar(100) NOT NULL ,FatherID tinyint NOT NULL ,MotherID tinyint NOT NULL ,IsCurrent bit NOT NULL ); CREATE TABLE dbo.Parent ( ParentID tinyint NOT NULL IDENTITY PRIMARY KEY ,FirstName varchar(100) NOT NULL ,LastName varchar(100) NOT NULL ,EmailAddress varchar(100) NULL ); INSERT dbo.Student VALUES ('Bob jr', 'Jackson', 1,2, 0); INSERT dbo.Student VALUES ('Joe jr', 'Johnson', 3,4, 1); INSERT dbo.Parent VALUES ('Bob sr', 'Jackson', NULL); INSERT dbo.Parent VALUES ('Betty', 'Jackson', 'bettyjackson@someprovider.com'); INSERT dbo.Parent VALUES ('Joe sr', 'Johnson', 'joejohnson@someprovider.com'); INSERT dbo.Parent VALUES ('Jane', 'Johnson', 'janejohnson@someprovider.com'); -- 070 SELECT s.FirstName AS 'Student first name' ,s.LastName AS 'Student last name' ,p.FirstName AS 'Parent first name' ,p.LastName AS 'Parent last name' ,p.EmailAddress FROM dbo.Student s inner join Parent p on s.FatherID = p.ParentID OR s.MotherID = p.ParentID WHERE s.IsCurrent = 1 -- 071 DROP TABLE dbo.Student DROP TABLE dbo.Parent -- Write queries with NULLs on joins -- 072 CREATE TABLE dbo.Customers ( CustomerID tinyint NULL ,FirstName varchar(100) NOT NULL ,LastName varchar(100) NOT NULL ,[Address] varchar(100) NOT NULL ); CREATE TABLE dbo.Orders ( OrderID tinyint NOT NULL IDENTITY PRIMARY KEY ,CustomerID tinyint NULL ,OrderDate datetime NOT NULL ,SalesAmount decimal(18,2) NOT NULL ); INSERT dbo.Customers VALUES (1, 'Bob', 'Jackson', 'Main street 1, Dallas'); INSERT dbo.Customers VALUES (2, 'Frank', 'Smith', 'Second street 2, Miami'); INSERT dbo.Customers VALUES (NULL, 'Joe', 'Johnson', 'Third Avenue 3, New York'); INSERT dbo.Orders VALUES (1, '2011-01-01', 30.02); INSERT dbo.Orders VALUES (2, '2012-02-02', 15.99); INSERT dbo.Orders VALUES (NULL, '2014-04-04', 107.00); -- 073 SELECT * FROM dbo.Customers c INNER JOIN dbo.Orders o ON c.CustomerID = o.CustomerID -- 074 SELECT ISNULL(CustomerID, 99) FROM dbo.Customers -- 075 SELECT * FROM dbo.Customers c INNER JOIN dbo.Orders o ON ISNULL(c.CustomerID,99) = ISNULL(o.CustomerID,99) -- 076 SELECT * FROM dbo.Customers c INNER JOIN dbo.Orders o ON (c.CustomerID IS NULL and o.CustomerID IS NULL) OR c.CustomerID = o.CustomerID -- 077 DROP TABLE dbo.Customers; DROP TABLE dbo.Orders; /* Implement functions and aggregate data */ -- 078 SELECT UPPER(FullName) FROM [Application].[People] -- 079 SELECT LOWER('ABCDE') -- 080 SELECT UPPER(LOWER('ABCDE')) -- 081 SELECT * FROM [Application].[People] WHERE LOWER(PreferredName) = 'isabelle' -- 082 SELECT * FROM fn_virtualfilestats(NULL,NULL); -- 083 SELECT DB_ID('WideWorldImporters') -- 084 SELECT DB_NAME(vfs.Dbid) as 'Database' ,mf.name as 'File name' ,mf.physical_name ,CASE NumberReads WHEN 0 THEN 'none' ELSE CAST(IoStallReadMS/NumberReads AS varchar(100)) END as 'Read latency' ,CASE NumberWrites WHEN 0 THEN 'none' ELSE CAST(IoStallWriteMS/NumberWrites AS varchar(100)) END as 'Write latency' FROM fn_virtualfilestats(NULL,NULL) vfs INNER JOIN sys.master_files mf ON vfs.DbId = mf.database_id AND vfs.Fileid = mf.file_id ORDER BY DbId, file_id; /* Identify the impact of function usage to query performance and WHERE clause sargability */ -- 085 SELECT PreferredName FROM [Application].[People] WHERE LEFT(PreferredName,3) = 'Isa' SELECT PreferredName FROM [Application].[People] WHERE PreferredName LIKE 'Isa%' /* index hint CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>] ON [Application].[People] ([PreferredName]) */ -- 086 CREATE NONCLUSTERED INDEX [NonClusteredIndex-PerformanceDemo] ON [Application].[People] ( [PreferredName] ASC ) GO -- 087 DROP INDEX [NonClusteredIndex-PerformanceDemo] ON [Application].[People] -- 088 SELECT DATEADD(week, 1, '2017-08-25') -- 089 SELECT DATEADD(day, -1, GETDATE()) -- 090 CREATE NONCLUSTERED INDEX [NCIX-ExpectedDeliveryDate] ON [Sales].[Orders] ( [ExpectedDeliveryDate] ASC ) -- 091 SELECT OrderID FROM Sales.Orders WHERE ExpectedDeliveryDate = DATEADD(day, 1, GETDATE()) SELECT OrderID FROM Sales.Orders WHERE GETDATE() = DATEADD(day, -1, ExpectedDeliveryDate) -- 092 DECLARE @tomorrow date SET @tomorrow = DATEADD(day, 1, GETDATE()) SELECT OrderID FROM Sales.Orders WHERE ExpectedDeliveryDate = @tomorrow -- 093 DROP INDEX [NCIX-ExpectedDeliveryDate] ON [Sales].[Orders] /* Identify the differences between deterministic and non-deterministic functions */ -- 094 select RAND() /* Use built-in aggregate functions */ -- 095 SELECT MAX(preferredname) FROM Application.People -- 096 SELECT TOP 1 PreferredName FROM Application.People ORDER BY PreferredName DESC -- 097 SELECT count(*) FROM sales.orders -- 098 EXEC sp_spaceused 'sales.orders' -- 099 SELECT SalespersonPersonID ,count(*) AS 'Orders per salesperson' FROM sales.orders GROUP BY SalespersonPersonID ORDER BY SalespersonPersonID -- 100 SELECT 'Average' = AVG(quantity) ,'Average #2' = AVG(CAST(quantity as decimal(5,2))) ,'Number' = COUNT(*) ,'Maximum' = MAX(quantity) ,'Minimum' = MIN(quantity) ,'Total' = SUM(quantity) FROM Sales.Orderlines; -- 101 SELECT cast(9310904 as dec(18,2))/cast(231412 as dec(18,2)) SELECT 9310904/231412 /* Use arithmetic functions, date-related functions, and system functions */ -- 102 SELECT 'Year' = YEAR('2017-08-26') ,'Year #2' = DATEPART(YEAR,'2017-08-26') ,'Month' = MONTH('2017-08-26') ,'Name of the month' = DATENAME(MONTH,'2017-08-26') ,'Day' = DAY('2017-08-26') ,'Name of the day' = DATENAME(WEEKDAY,'2017-08-26') ,'Day of the week' = DATEPART(WEEKDAY,'2017-08-26'); -- 103 SELECT @@DATEFIRST -- 104 SET DATEFIRST 1; SELECT 'First day of the week' = CASE @@DATEFIRST WHEN 1 THEN 'Monday' WHEN 2 THEN 'Tuesday' WHEN 3 THEN 'Wednesday' WHEN 4 THEN 'Thursday' WHEN 5 THEN 'Friday' WHEN 6 THEN 'Saturday' WHEN 7 THEN 'Sunday' ELSE 'I do not know what went wrong here' END SET DATEFIRST 7; -- 105 SELECT 'Current time' = Current_Timestamp ,'Current time #2' = GETDATE() ,'Current time more precision' = Sysdatetime() SELECT 'Current UTC time' = Getutcdate() ,'Current time more precision' = Sysutcdatetime() ,'Current time + UTC offset' = Sysdatetimeoffset() -- 106 SELECT TODATETIMEOFFSET(GETDATE(), '+00:00') -- 107 SELECT 'Moscow time' = SWITCHOFFSET (Sysdatetimeoffset(), '+03:00') -- 108 SELECT ISDATE('2017-02-29') -- 109 SELECT DATEDIFF(day,'1971-09-03', GETDATE()) /* arithmetic functions */ -- 110 SELECT ABS (7), ABS(-7) -- 111 SELECT 7 % 3 -- 112 SELECT 'Floor' = FLOOR(12.345) ,'Ceiling' = CEILING(12.345) ,'Round to 100' = ROUND(12.345, -2) ,'Round to 10' = ROUND(12.345, -1) ,'Round to 1' = ROUND(12.345, 0) ,'Round to .1' = ROUND(12.345, 1) ,'Round to .01' = ROUND(12.345, 2) /* string functions */ -- 113 SELECT CONCAT('mon', 'day') -- 114 SELECT 'mon' + 'day' -- 115 SELECT firstname + ' ' + lastname FROM dbo.parent SELECT CONCAT(firstname, ' ', lastname) FROM dbo.parent -- 116 SET CONCAT_NULL_YIELDS_NULL OFF SELECT 'mon' + NULL + 'day' SELECT CONCAT('mon', NULL, 'day') SET CONCAT_NULL_YIELDS_NULL ON SELECT 'mon' + NULL + 'day' SELECT CONCAT('mon', NULL, 'day') -- 117 SELECT LEFT('monday',3) SELECT RIGHT('monday',3) -- 118 SELECT SUBSTRING('The middle part', 5, 6) -- 119 SELECT LEFT('monday', 3) -- 120 SELECT SUBSTRING('monday', 1, 3) -- 121 SELECT CHARINDEX('middle', 'the middle part') -- 122 SELECT CHARINDEX('t', 'the middle part') -- 123 SELECT CHARINDEX('t', 'the middle part', 2) -- 124 SELECT PATINDEX('%m%e%', 'the middle part') -- 125 SELECT REPLACE('the middle part', 'middle', 'best') -- 126 SELECT LEN('the middle part') -- 127 SELECT CHAR(65) SELECT ASCII('A') -- 128 DECLARE @strange_string VARCHAR(100) SET @strange_string = 'mon' + CHAR(27) + 'day' SELECT @strange_string SELECT ASCII(SUBSTRING(@strange_string,4,1)) -- 129 BACKUP DATABASE model TO DISK = 'd:\model.bak' -- 130 SELECT 'BACKUP DATABASE ' + name FROM sys.databases WHERE name <> 'tempdb' -- 131 BACKUP DATABASE master BACKUP DATABASE model BACKUP DATABASE msdb BACKUP DATABASE TestDB BACKUP DATABASE WideWorldImporters -- 132 SELECT 'BACKUP DATABASE ' + name + ' TO DISK = D:\' + name + '.bak' -- 133 SELECT 'BACKUP DATABASE ' + name + ' TO DISK = ''D:\' + name + '.bak''' -- 134 SELECT 'BACKUP DATABASE ' + name + ' TO DISK = ' + CHAR(39) + 'D:\' + name + '.bak' + CHAR(39) -- 135 SELECT CONCAT('BACKUP DATABASE ', name, ' TO DISK = ', CHAR(39), 'D:\', name, '.bak', CHAR(39)) -- 136 SELECT REVERSE('monday') /* system functions */ -- 136 SELECT HOST_NAME() -- 137 SELECT USER_NAME() -- 138 CREATE TABLE dbo.Employees ( EmployeeID tinyint NOT NULL IDENTITY ,FirstName varchar(100) NOT NULL ,LastName varchar(100) NOT NULL ,[Address] varchar(100) NOT NULL ); -- 139 INSERT dbo.Employees VALUES ('Jack', 'Ford', 'Second street 2, Denver'); -- 140 INSERT dbo.Employees VALUES (1, 'Jack', 'Ford', 'Second street 2, Denver'); -- 141 SET IDENTITY_INSERT dbo.Employees ON; INSERT dbo.Employees (EmployeeID, FirstName, LastName, Address) VALUES (1, 'Donald', 'Charleston', 'Times Square, New York'); SET IDENTITY_INSERT dbo.Employees OFF; -- 142 INSERT dbo.Empolyees (FirstName, LastName, Address) VALUES ('A.', 'Snowman' 'Mount Everest, Nepal'); SELECT @@IDENTITY -- 143 DROP TABLE IF EXISTS dbo.Orders DROP TABLE IF EXISTS dbo.OrderLines CREATE TABLE dbo.Orders ( OrderID tinyint NOT NULL IDENTITY PRIMARY KEY ,CustomerID tinyint NOT NULL ,OrderDate datetime NOT NULL ,SalesAmount decimal(18,2) NOT NULL ); CREATE TABLE dbo.OrderLines ( OrderLineID tinyint NOT NULL IDENTITY PRIMARY KEY ,OrderID tinyint NOT NULL ,Product varchar(100) NOT NULL ,Units tinyint NOT NULL ,UnitPrice decimal(18,2) NOT NULL ); DECLARE @OrderID tinyint INSERT dbo.Orders VALUES (1,'2015-05-05', 230.02); SELECT @OrderID = @@IDENTITY INSERT dbo.OrderLines VALUES ( @OrderID, 'ProductA', 1, 10.04); -- 144 /*Old syntax to drop a table only if it exists*/ IF EXISTS ( SELECT name FROM sys.tables WHERE name = 'Orders' and schema_id=SCHEMA_ID('dbo')) DROP TABLE dbo.Orders -- 145 SELECT IDENT_CURRENT('dbo.Orders') -- 146 SELECT 'Globally unique identifier' = NEWID() -- 147 CREATE TABLE tblGUID ( id uniqueidentifier DEFAULT NEWSEQUENTIALID() ,text varchar(100)); INSERT tblGUID (text) VALUES ('One') INSERT tblGUID (text) VALUES ('Two') INSERT tblGUID (text) VALUES ('Three') SELECT * FROM tblGUID -- 148 SELECT ISNUMERIC('12') as Number ,ISNUMERIC(12) as Number ,ISNUMERIC('12A') as Number -- 149 SELECT TRY_CAST('$20' as money) SELECT TRY_CAST('$20' as int) SELECT CAST('$20' as int) -- 150 SELECT CAST('20170914' AS datetime) ,CONVERT(datetime, '20170914') -- 151 SELECT CONVERT(varchar(100), GETDATE(), 100) AS 'Default' ,CONVERT(varchar(100), GETDATE(), 101) AS 'US' ,CONVERT(varchar(100), GETDATE(), 1) AS 'US (without century)' ,CONVERT(varchar(100), GETDATE(), 126) AS 'ISO8601' ,CONVERT(varchar(100), GETDATE(), 113) AS 'Europe default' -- 152 SELECT * FROM [Sales].[Invoices] SELECT @@ROWCOUNT AS 'Number of records affected' -- 153 SELECT * FROM [Sales].[Invoices] SELECT ROWCOUNT_BIG() AS 'Number of records affected' /* Modify data */ -- 154 DROP IF EXISTS TABLE dbo.Employees CREATE TABLE dbo.Employees ( EmployeeID tinyint NOT NULL IDENTITY ,FirstName varchar(100) NOT NULL ,LastName varchar(100) NOT NULL ,[Address] varchar(100) NOT NULL ); INSERT dbo.Customers VALUES ('Bob', 'Jackson', 'Main street 1, Dallas'); -- 155 INSERT dbo.Employees (FirstName, LastName) VALUES ('Bob', 'Jackson') INSERT dbo.Employees (FirstName, LastName, Address) VALUES ('Bob', 'Jackson', NULL) -- 156 CREATE TABLE dbo.Employees ( EmployeeID tinyint NOT NULL IDENTITY ,FirstName varchar(100) NOT NULL ,LastName varchar(100) NOT NULL ,[Address] varchar(100) NOT NULL DEFAULT 'Under the bridge' ) -- 157 ALTER TABLE dbo.Employees ADD DEFAULT ('Under the bridge') FOR [Address] INSERT dbo.Employees (FirstName, LastName) VALUES ('Bob', 'Jackson') -- 158 INSERT dbo.Employees (FirstName, LastName) VALUES ('Bob', 'Jackson') INSERT dbo.Employees (FirstName, LastName, Address) VALUES ('Bob', 'Jackson', DEFAULT) -- 159 INSERT dbo.Employees DEFAULT VALUES -- 160 INSERT dbo.Employees (FirstName, LastName) VALUES ('Bob', 'Jackson') ,('Bo', 'Jackson') ,('Bill', 'Jackson') -- 161 DROP TABLE IF EXISTS dbo.Employees_backup CREATE TABLE dbo.Employees_backup ( FirstName varchar(100) NOT NULL ,LastName varchar(100) NOT NULL ,[Address] varchar(100) NOT NULL ) INSERT dbo.Employees_backup SELECT FirstName, LastName, [Address] FROM dbo.Employees -- 162 INSERT dbo.Employees_backup SELECT FirstName, LastName, [Address] FROM dbo.Employees UNION SELECT 'Tim', 'Smith', 'Miami' -- 163 DROP TABLE IF EXISTS dbo.Employees_backup SELECT FirstName, LastName, [Address] INTO dbo.Employees_backup FROM dbo.Employees -- 164 CREATE PROC dbo.usp_get_employees AS BEGIN SELECT FirstName, LastName, Address FROM dbo.Employees END -- 165 EXEC usp_get_employees -- 166 INSERT dbo.Employees_backup EXEC usp_get_employees -- 167 DECLARE @sql nvarchar(1000) SET @sql = 'SELECT FirstName, LastName, Address FROM dbo.Employees' EXEC (@sql) -- 168 EXEC sp_executesql @statement = @sql -- 169 INSERT dbo.Employees_backup EXEC (@sql) -- 170 INSERT dbo.Employees_backup EXEC sp_executesql @statement = @sql /* In more detail: dynamic SQL */ -- 171 SELECT TOP (3) * FROM dbo.Employees -- 172 SELECT TOP 50 percent * FROM dbo.Employees -- 173 SELECT TOP 50 percent * FROM dbo.Employees ORDER BY LastName -- 174 DECLARE @sql varchar(1000) ,@top int SET @top = 3 SET @sql = 'SELECT TOP ' + CAST (@top as varchar(10)) + 'FirstName, LastName, Address FROM dbo.Employees' EXEC (@sql) -- 175 -- create the table CREATE TABLE Students ( FirstName varchar(100) ,LastName varchar(100)) INSERT INTO dbo.Students (FirstName, LastName) VALUES('Robert', 'Tables') -- fetch the student record DECLARE @sql nvarchar(1000) ,@Firstname nvarchar(1000) SET @Firstname = 'Robert' SET @sql = 'SELECT * FROM dbo.Students WHERE FirstName = ' + CHAR(39) + @Firstname + CHAR(39) EXEC (@sql) -- 176 SET @Firstname = 'Robert''; DROP TABLE dbo.Students--' -- 177 SET @sql = 'SELECT * FROM dbo.Students WHERE FirstName = @FirstName' EXECUTE sp_executesql @statement = @sql ,@parameters = N'@FirstName as varchar(1000)' ,@FirstName = @FirstName -- 178 DECLARE @sql nvarchar(1000) ,@top int SET @sql = 'SELECT TOP (@top) FirstName, LastName, Address FROM dbo.Employees' SET @top = 3 EXEC sp_executesql @statement = @sql ,@parameters = N'@top as int' ,@top = @top -- 179 SELECT TOP (@top) FirstName, LastName, Address FROM dbo.Employees /* Cursors */ -- 180 USE WideWorldImporters DECLARE crsLoop CURSOR FOR SELECT TOP (50) FullName, UserPreferences FROM [Application].[People] ORDER BY FullName DECLARE @FullName nvarchar(50) ,@UserPreferences nvarchar(max) OPEN crsLoop FETCH NEXT FROM crsLoop INTO @FullName, @UserPreferences WHILE @@FETCH_STATUS = 0 BEGIN SELECT @FullName AS [Full Name], @UserPreferences AS [User Preferences] FETCH NEXT FROM crsLoop INTO @FullName, @UserPreferences END CLOSE crsLoop DEALLOCATE crsLoop -- 181 SELECT @@SPID -- 182 KILL 58 -- 183 SELECT * FROM master..sysprocesses WHERE dbid= DB_ID('TestDB') -- 184 SELECT spid FROM master..sysprocesses WHERE dbid= DB_ID('TestDB') GROUP BY spid SELECT DISTINCT spid FROM master..sysprocesses WHERE dbid= DB_ID('TestDB') -- 185 SELECT 'KILL ' + CAST(spid AS VARCHAR(10)) -- 186 DECLARE @SQL NVARCHAR(100); DECLARE crsKill CURSOR LOCAL FAST_FORWARD FOR SELECT 'KILL ' + CAST(spid AS VARCHAR(10)) FROM master.dbo.sysprocesses WHERE dbid = DB_ID('TestDB') GROUP BY spid; OPEN crsKill; FETCH crsKill INTO @SQL; WHILE @@FETCH_STATUS = 0 BEGIN EXEC sp_executesql @SQL; FETCH crsKill INTO @SQL END; CLOSE crsKill; DEALLOCATE crsKill; /* update */ -- 187 UPDATE dbo.Employees SET LastName = 'Didley' WHERE FirstName = 'Bo' -- 188 UPDATE TOP (1) dbo.Employees SET LastName = 'Didley' WHERE FirstName = 'Bo' ORDER BY EmployeeID -- 189 UPDATE dbo.Employees SET LastName = 'Didley' FROM ( SELECT TOP (1) EmployeeID FROM dbo.Employees WHERE FirstName = 'Bo' ORDER BY EmployeeID) AS e WHERE e.EmployeeID = Employees.EmployeeID -- 190 UPDATE dbo.Employees SET LastName = LTRIM(RTRIM(LastName)) ,Address = DEFAULT WHERE FirstName = 'Bo' -- 191 UPDATE dbo.Employees SET LastName = LTRIM(RTRIM(LastName)) ,Address = 'Home of mr. ' + FirstName + ' ' + LastName WHERE FirstName = 'Bo' -- 192 ALTER TABLE dbo.Employees ADD Salary decimal(18,2) DEFAULT 1000.00 UPDATE dbo.Employees SET Salary = DEFAULT -- 193 UPDATE dbo.Employees SET Salary = Salary + 100 WHERE FirstName = 'Bo' UPDATE dbo.Employees SET Salary += 100 WHERE FirstName = 'Bo' -- 194 -- Bitwise comparison operators SELECT 1 & 1 AS 'Two ones', 1 & 0 AS 'Left one', 0 & 1 AS 'Right one', 0 & 0 AS 'No one' SELECT 1 | 1 AS 'Two ones', 1 | 0 AS 'Left one', 0 | 1 AS 'Right one', 0 | 0 AS 'No one' SELECT 1 ^ 1 AS 'Two ones', 1 ^ 0 AS 'Left one', 0 ^ 1 AS 'Right one', 0 ^ 0 AS 'No one' -- 195 SELECT 5 & 19, 5 | 19, 5 ^ 19 -- 196 -- Update on join UPDATE c SET c.StandardDiscountPercentage = 5 FROM sales.customers c INNER JOIN sales.Orders o on c.CustomerID = o.CustomerID WHERE o.OrderDate > '2016-01-01' -- 197 UPDATE Sales.Customers SET StandardDiscountPercentage = 0 WHERE StandardDiscountPercentage = 5 -- 198 -- Nondeterministic update on join UPDATE o SET comments = ol.Description FROM sales.orders o INNER JOIN sales.orderlines ol ON o.orderid = ol.orderid WHERE o.OrderID = 46 -- 199 UPDATE Sales.Orders SET comments = NULL -- 200 -- Update WHERE CURRENT OF DECLARE crsOrders CURSOR FOR SELECT orderid FROM Sales.orders DECLARE @orderid int OPEN crsOrders FETCH NEXT FROM crsOrders INTO @orderid WHILE @@FETCH_STATUS = 0 BEGIN UPDATE Sales.Orders SET Comments = 'This is order ' + CAST(@orderid AS VARCHAR(10)) WHERE CURRENT OF crsOrders FETCH NEXT FROM crsOrders INTO @orderid END CLOSE crsOrders DEALLOCATE crsOrders -- 201 UPDATE Sales.Orders SET Comments = 'This is order ' + CAST(OrderId as varchar(10)) -- 202 UPDATE Sales.Orders SET comments = NULL -- 203 DECLARE @Comments nvarchar(max) UPDATE Sales.Orders SET @Comments = Comments = 'This is order ' + CAST (OrderId as varchar(10)) WHERE OrderId = 1 SELECT @Comments /* Delete */ -- 204 SELECT * FROM sales.customers c INNER JOIN sales.Orders o on c.CustomerID = o.CustomerID WHERE o.OrderDate < '2010-01-01' -- 205 DELETE FROM c FROM sales.customers c INNER JOIN sales.Orders o on c.CustomerID = o.CustomerID WHERE o.OrderDate < '2010-01-01' -- 206 DECLARE @rowcount int; SET @rowcount = 1; WHILE @rowcount > 0 BEGIN DELETE TOP (100000) FROM dbo.MyVeryLargeTable; SET @rowcount = @@rowcount; WAITFOR DELAY '00:00:10'; END; /* Output clause */ -- 207 DROP TABLE IF EXISTS output_test CREATE TABLE output_test (id int, SomeText varchar(100)) INSERT output_test (id, SomeText) VALUES (1, 'before') INSERT output_test (id, SomeText) VALUES (2, 'before') UPDATE output_test SET SomeText = 'after' OUTPUT DELETED.*, INSERTED.* DROP TABLE IF EXISTS output_test -- 208 INSERT output_test (id, SomeText) OUTPUT INSERTED.* VALUES (2, 'before') -- 209 INSERT output_test (id, SomeText) OUTPUT INSERTED.id VALUES (2, 'before') -- 210 OUTPUT DELETED.* INTO output_test_before -- 211 OUTPUT DELETED.id, DELETED.SomeText, GETDATE() INTO output_test_before (id, Sometext, ChangeTime) /* Merge statement */ -- 212 DROP TABLE IF EXISTS Params_target CREATE TABLE Params_target (drive CHAR(1), threshold_in_GB tinyint) INSERT Params_target VALUES ('C',10), ('D',10), ('E',10), ('Q',10) DECLARE @drive CHAR(1) = 'Q' ,@threshold_in_GB tinyint = 1 IF EXISTS (SELECT * FROM Params_target WHERE drive = @drive) BEGIN UPDATE Params_target SET threshold_in_GB = @threshold_in_GB WHERE drive = @drive END ELSE BEGIN INSERT Params_target VALUES(@drive, @threshold_in_GB) END -- 213 SELECT * FROM Params_target ORDER BY drive -- 214 -- setup DROP TABLE IF EXISTS Params_target DROP TABLE IF EXISTS Params_source CREATE TABLE Params_target (drive CHAR(1), threshold_in_GB tinyint) INSERT Params_target VALUES ('C',10), ('D',10), ('E',10), ('Q',1) CREATE TABLE Params_source (drive CHAR(1), threshold_in_GB tinyint) INSERT Params_source VALUES('Q',1) --actual MERGE statement MERGE Params_target AS TARGET USING Params_source AS SOURCE ON TARGET.drive = SOURCE.drive WHEN MATCHED AND SOURCE.threshold_in_GB <> TARGET.threshold_in_GB THEN UPDATE SET threshold_in_GB = SOURCE.threshold_in_GB WHEN NOT MATCHED BY TARGET THEN INSERT (drive, threshold_in_GB) VALUES (SOURCE.drive, SOURCE.threshold_in_GB) WHEN NOT MATCHED BY SOURCE THEN DELETE OUTPUT $action, deleted.threshold_in_GB, inserted.threshold_in_GB; /* Determine the results of Data Definition Language (DDL) statements on supplied tables and data */ -- 215 -- add column ALTER TABLE [dbo].[Employees] ADD Department varchar(100) NULL -- 216 ALTER TABLE [dbo].[Employees] DROP COLUMN Department -- 217 ALTER TABLE [dbo].[Employees] ADD Department varchar(100) NOT NULL DEFAULT 'Sales' Msg 5074, Level 16, State 1, Line 12 The object 'DF__Employees__Depar__2F5453E4' is dependent on column 'Department'. Msg 4922, Level 16, State 9, Line 12 ALTER TABLE DROP COLUMN Department failed because one or more objects access this column. -- 218 ALTER TABLE [dbo].[Employees] DROP CONSTRAINT [DF__Employees__Depar__2F5453E4] -- 219 ALTER TABLE [dbo].[Employees] ADD Department varchar(100) NOT NULL CONSTRAINT DF_Department DEFAULT 'Sales' -- 220 ALTER TABLE [dbo].[Employees] ADD Department varchar(100) NULL CONSTRAINT DF_Department DEFAULT 'Sales' WITH VALUES -- 221 SELECT FirstName, LastName, Address, Salary INTO Employees_test FROM dbo.employees ALTER TABLE [dbo].[Employees_test] ADD EmployeeID int IDENTITY /* alter column */ -- 222 ALTER TABLE dbo.Employees ALTER COLUMN LastName int -- 223 ALTER TABLE dbo.Employees ALTER COLUMN LastName varchar(5) -- 224 ALTER TABLE dbo.Employees ALTER COLUMN LastName varchar(50) -- 225 ALTER TABLE dbo.Employees ALTER COLUMN EmployeeId tinyint -- 226 ALTER TABLE dbo.Employees ADD CONSTRAINT PK_Employeeid PRIMARY KEY (EmployeeId) -- 227 CREATE TABLE Departments (Department varchar(100) NOT NULL PRIMARY KEY) INSERT Departments VALUES ('Sales') ALTER TABLE dbo.Employees ADD CONSTRAINT FK_Department FOREIGN KEY (Department) REFERENCES Departments(Department) ALTER TABLE dbo.Employees ALTER COLUMN Department varchar(200) -- 228 ALTER TABLE [dbo].[Employees] ADD EmployeeNumber int NULL GO UPDATE dbo.Employees SET EmployeeNumber = EmployeeID ALTER TABLE [dbo].[Employees] ADD CONSTRAINT uq_employeenumber UNIQUE (EmployeeNumber) GO INSERT dbo.Employees (FirstName, LastName, Address, Salary, Department, EmployeeNumber) VALUES('Lisa', 'Simpson','Springfield',1500.00,'Sales', NULL ) -- 229 ALTER TABLE [dbo].[Employees] DROP COLUMN EmployeeNumber -- 230 ALTER TABLE [dbo].[Employees] DROP CONSTRAINT uq_employeenumber ALTER TABLE [dbo].[Employees] DROP COLUMN EmployeeNumber -- 231 ALTER TABLE [dbo].[Employees] ADD EmployeeNumber int NULL EXEC sp_rename 'Employees.EmployeeNumber', 'Employee_Number' ALTER TABLE [dbo].[Employees] DROP COLUMN Employee_Number /* drop column */ -- 232 ALTER TABLE [dbo].[Employees] DROP COLUMN Department -- 233 CREATE INDEX ix_department ON dbo.Employees(Department) ALTER TABLE [dbo].[Employees] DROP COLUMN Department -- 234 DROP INDEX ix_lastname -- 235 ALTER TABLE table WITH CHECK CHECK CONSTRAINT ALL |
-
Recent Posts
Archives
Categories