Chapter 6 PROGRAMMING SQL SERVER 2000 335 Now

Chapter 6 PROGRAMMING SQL SERVER 2000 335 Now SQL Server can tell that what you really want is one specific row from the sysobjects table. This is, of course, assuming you declared the @ID variable. You didn t do that because you re not sure how to declare a variable? Well, it s time to learn. Declaring Variables A variable is declared using the handily named DECLARE statement. So, to declare a single variable with a type of int, you use something like this: DECLARE @id int That s pretty simple, so now try the advanced version: DECLARE @id int, @foo int, @fee varchar(30) The comma separating each of the variables is required by SQL Server. The fact that they re all on new lines and tabbed in is just for the sake of style. The following is an equivalent statement: DECLARE @id int, @foo int, @fee varchar(30) So, it s important to note that you can take any statement and break it onto multiple lines. Don t overdo it, but use this feature to make your scripts easier to read and maintain. In addition, it s easier to draw conclusions about what a variable does by what kind of data is in the variable, or what type the variable has, and putting the variables on different lines helps to emphasize their types. Variable Types You can create variables of nearly any type that you can store in a table, with the restriction that you can t create variables of type TEXT, NTEXT, or IMAGE. So, you can create int, tinyint, smallint, datetime, smalldatetime, uniqueidentifier, varchar, nvarchar, char, nchar, and so on. You can (and should) specify lengths where appropriate, such as varchar(30) or nchar(15). You can create a string variable that holds up to 8,000 bytes, so you can build a varchar(8000) or an nvarchar(4000) in a variable. (Remember that the nchar and nvarchar types are double-wide characters, so they take up twice as much room as a varchar.)

For high quality website hosting services please check java web hosting website.

Comments are closed.