Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 1.

Similar presentations


Presentation on theme: "SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 1."— Presentation transcript:

1 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 1

2 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 2

3 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 3

4 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 4 Batches and the GO command

5 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 5 GO To signal the end of a batch, you use the GO command. A GO command isn’t required after the last batch in a script or for a script that contains a single batch.

6 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 6

7 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 7

8 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 8

9 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 9

10 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 10

11 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 11

12 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 12

13 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 13 Concatenation, cast, convert

14 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 14 Concatenation ( + ) The SQL standard uses || for concatentation Sql Server TSql uses + for concatenation Example: SELECT fname + ' ' + lname as name FROM employees

15 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 15 operands must be character data In SQL Server - TSql... concatenation MUST take two character values (varchar, char, nchar, etc) as its operands Neither parameter can be numeric or date types The following WILL cause an ERROR (if salary is a money column) SELECT fname + salary FROM employees

16 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 16 cast Use the SQL standard "cast" operator to convert data from one datatype into another The following WILL work: SELECT fname + cast(salary as varchar(10)) FROM employees

17 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 17 convert You can use the TSQL "convert" function instead of cast. The following is equivalent to the previous example: SELECT fname + convert(varchar(10), salary) FROM employees

18 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 18 Difference between CAST and CONVERT differences –standards CAST is a standard CONVERT is TSQL specific –extra functionality CONVERT has one additional piece of functionality that CAST does not have see next slide

19 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 19 Convert (type, data, style) Convert takes an optional 3rd (integer) parameter that determines the "style" (AKA format) of the data after the cast. If the data is a money value then format has the following possible values: 0 - no commas, two decimal points 1 - yes commas, two decimal points 2 - no commas, four decimal points

20 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 20 styles for dates, float, real See the following URL: –http://doc.ddart.net/mssql/sql70/ca-co_1.htm

21 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 21 TSQL statements

22 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 22 USE statement USE –The use statement switches a script to use the specified database. –A single script can work in more than one database by issuing the use statement several times

23 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 23 PRINT statement PRINT –Prints out the value of the string expression.

24 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 24 Variables

25 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 25

26 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 26

27 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 27

28 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 28

29 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 29

30 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 30

31 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 31

32 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 32

33 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 33 Table Variables

34 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 34 Scalar vs. table variables AThe variables that we've seen so far can hold a single data item and is defined with a standard data type. These are called scalar variables. A variable that can hold an entire table is called a "table variable" (see next slide...)

35 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 35

36 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 36

37 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 37

38 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 38 Derived tables

39 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 39 Derived Tables The term "Derived Table" is simply a table that is created as the result of a subquery in the FROM clause.

40 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 40 Temporary Tables

41 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 41

42 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 42

43 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 43

44 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 44

45 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 45

46 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 46

47 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 47 IF BEGIN... END ELSE BEGIN... END

48 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 48

49 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 49

50 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 50

51 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 51

52 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 52 DB_ID OBJECT_ID

53 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 53

54 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 54

55 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 55

56 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 56

57 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 57 WHILE

58 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 58

59 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 59

60 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 60

61 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 61 SYSTEM FUNCTIONS (AKA global variables)

62 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 62

63 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 63

64 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 64

65 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 65 Session settings and the SET command

66 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 66

67 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 67

68 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 68

69 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 69 Dynamic SQL and the EXEC command

70 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 70

71 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 71

72 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 72

73 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 73

74 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 74

75 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 75

76 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 76

77 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 77

78 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 78

79 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 79 OSQL

80 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 80

81 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 81

82 SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 82


Download ppt "SQL for SQL Server, C13© 2002, Mike Murach & Associates, Inc. Slide 1."

Similar presentations


Ads by Google