28
1 Working with MS SQL Server II

1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

Embed Size (px)

Citation preview

Page 1: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

1

Working with MS SQL Server II

Page 2: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

2

The sqlcmd Utility

Command line utility for MS SQL Server databases.

Previous version called osql

Available on classroom and lab PCs. Free download from Microsoft

google sqlcmd download

In Windows command window Connect to a Database Server Enter SQL commands on the command line. Results output to the console.

Page 3: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

3

Getting and Installing sqlcmd

Included with Management Studio Express. Free download from Microsoft,

Documentation available in SQL Server 2005 Books Online Free download from Microsoft.

In Visual Studio 2008 Help search for sqlcmd.

Page 4: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

Visual Studio Search Result

Page 5: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

5

The SQL Language

The following slides demonstrate SQL commands using the sqlcmd program.

Remember SQL is used everywhere we interact with a database server: Command line Database management programs

Including Visual Studio Server Explorer Our own programs (in the form of strings)

Page 6: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

Using sqlcmd

In order to use sqlcmd with the SQL Server on scorpius you will need a VPN connection.

See: http://www.cse.usf.edu/~turnerr/Software_Systems_Development/073a_Working_with_MS_SQL_Server.pdf

The following slides use the addresses table that we created and populated in a previous class. Slides 9 - 37 of same presentation.

Page 7: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

7

Using sqlcmd

The Server My Username

Password entered here

Execute commands in buffer

Page 8: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

8

SQL Command Line Example

View a subset of the columns.

Page 9: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

9

Retrieve Specific Entries

Find all addresses with zip code 33707

Page 10: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

10

Wildcard Selection

Find all addresses with last name beginning with S

Page 11: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

11

Wildcard Selection

All addresses with last name containing an s

Page 12: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

12

Case Sensitivity

Typically SQL commands and keywords are not case sensitive.

Data is case sensitive Characters are stored as upper case or lower case.

Sorting order is a mode Default is not case sensitive Search comparisons same as sorting order.

http://sqlserver2000.databases.aspfaq.com/how-can-i-make-my-sql-queries-case-sensitive.html

Page 13: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

13

Case Sensitivity

Page 14: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

Case Sensitive Select

14

Page 15: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

Case Sensitivity

You can make the default for a column case sensitive.

See: http://social.msdn.microsoft.com/Forums/en-US/

sqlsetupandupgrade/thread/850deb43-18ff-492b-bec0-b2b9a7ad76ac

Or google mssql collate

15

Page 16: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

16

Sorting the Output

Zip_Code is a string. So sort is lexicographic, not numeric.

Page 17: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

17

Counting Rows

How many addresses are there with zip code 33707?

Page 18: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

18

Updating Records

Page 19: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

19

Updating Records

Page 20: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

20

Updating Multiple Fields

Page 21: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

21

Deleting a Record

Page 22: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

22

SQL Commands in Visual Studio

We can also use Visual Studio to issue arbitrary SQL commands to the server.

On the Data menu select New Query

Page 23: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

23

SQL Commands in Visual Studio

Page 24: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

24

SQL Commands in Visual Studio

Click here

Page 25: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

25

SQL Commands in Visual Studio

Enter SQL command here.

Page 26: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

26

SQL Commands in Visual Studio

Click here to execute the command.

Page 27: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

27

SQL Commands in Visual Studio

Results appear below the Query window.

Page 28: 1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom

28

Learning SQL

Many books available

One that I recommend: Teach Yourself SQL in 24 Hours (Third Edition) Ryan Stephens and Ron Plew, SAMS, 2003

Lots of information on the web A sampling:

http://en.wikipedia.org/wiki/SQL http://www.w3schools.com/sql/default.asp http://db.grussell.org/ch3.html