Monday, February 23, 2009

.Net - Command Line Compiler

The C# Command-Line Compiler (csc.exe)



There are a number of techniques you may use to compile C# source code. In addition to Visual
Studio 2005 (as well as various third-party .NET IDEs), you are able to create .NET assemblies using
the C# command-line compiler, csc.exe (where csc stands for C-Sharp Compiler). This tool is included
with the .NET Framework 2.0 SDK. While it is true that you may never decide to build a large-scale
application using the command-line compiler, it is important to understand the basics of how to
compile your *.cs files by hand. I can think of a few reasons you should get a grip on the process:
• The most obvious reason is the simple fact that you might not have a copy of Visual Studio
2005.
• You plan to make use of automated build tools such as MSBuild or NAnt.
• You want to deepen your understanding of C#. When you use graphical IDEs to build applications,
you are ultimately instructing csc.exe how to manipulate your C# input files. In this
light, it’s edifying to see what takes place behind the scenes.
Another nice by-product of working with csc.exe in the raw is that you become that much
more comfortable manipulating other command-line tools included with the .NET Framework 2.0
SDK. As you will see throughout this book, a number of important utilities are accessible only from
the command line.

SQL - Delete

DELETE [FROM] {table_name | view_name} WHERE CURRENT OF cursor_name

Arguments
FROM

Is an optional keyword included for compatibility with other versions of ESQL/C.

table_name

Is the same table used in the SELECT statement portion of the DECLARE CURSOR STATEMENT.

view_name

Is the same view used in the SELECT statement portion of the DECLARE CURSOR statement.

cursor_name

Is a previously declared, opened, and fetched cursor. Cursor names can have as many as 30 characters, and can include alphanumeric characters and any symbols that are legal in file names. Hyphens (-) are not permitted. The first character must be a letter.

Remarks
In addition to having the functionality of the Transact-SQL DELETE statement, the Embedded SQL DELETE statement includes functionality known as positioned delete, which deletes the row most recently fetched by a cursor. The DELETE statement used in standard Transact-SQL statements is known as a searched delete.

Note that a positioned delete has no search condition. The WHERE CURRENT OF option is used in place of a search condition clause. The WHERE CURRENT OF option cannot be used in a PREPARE statement.

In a positioned delete that uses a browse cursor, the SELECT statement used to open the cursor must include a FOR BROWSE clause. The base table(s) must include a timestamp column. If an error prevents any row found by the search condition from being deleted, no changes are made to the database.

When using a browse cursor, or a standard cursor with optimistic concurrency control (SET CONCURRENCY with the OPTCC or OPTCCVAL option), if the row has been changed after the last FETCH statement, no changes are made to the database and the value of SQLCODE is set to -532. Also, the SQLERRD3 field in the SQLCA data structure shows that no rows were processed.

Examples
EXEC SQL DECLARE c1 CURSOR FOR
SELECT au_fname, au_lname FROM authors FOR BROWSE;
EXEC SQL OPEN c1;
while (SQLCODE == 0)
{
EXEC SQL FETCH c1 INTO :fname, :lname;
if (SQLCODE == 0)
{
printf("%12s %12s\n", fname, lname);
printf("Delete? ");
scanf("%c", &reply);
if (reply == 'y')
{
EXEC SQL DELETE FROM authors WHERE CURRENT OF c1;
printf("delete sqlcode= %d\n", SQLCODE(ca));
}
}
}

SQL - Insert

Inserting a Row Using INSERT...Values
The VALUES keyword specifies the values for one row of a table. The values are specified as a comma-separated list of scalar expressions whose data type, precision, and scale must be the same as or implicitly convertible to the corresponding column in the column list. If a column list is not specified, the values must be specified in the same sequence as the columns in the table or view.

For example, this statement inserts a new shipper into the Shippers table using the VALUES clause:

INSERT INTO Northwind.dbo.Shippers (CompanyName, Phone)
VALUES (N'Snowflake Shipping', N'(503)555-7233')

A column list is required for this insert because the ShipperID column has the IDENTITY property; therefore, values cannot be inserted into it.

SQL - Select into

SELECT INTO
The SELECT INTO statement retrieves one row of results. The SELECT INTO statement is also known as a singleton SELECT statement.

Syntax
SELECT [select_list] INTO {:hvar [,...]} select_options

Arguments
select_list

Is the list of items (table columns or expressions) to retrieve data from.

hvar

Is one or more host variables to receive the select_list items.

select_options

Is one or more statements or other options that can be used with the Transact-SQL SELECT statement (for example, a FROM or WHERE clause). The GROUP BY, HAVING, COMPUTE, CUBE, and ROLLUP clauses are not supported.

Remarks
The SELECT INTO statement retrieves one row of results and assigns the values of the items in select_list to the host variables specified in the INTO list. If more columns are selected than the number of receiving host variables, then the value of SQLWARN3 is set to W. The data type and length of the host variable must be compatible with the value assigned to it. If data is truncated, the value of SQLWARN3 is set to W.

The Embedded SQL SELECT INTO statement is compatible with the Transact-SQL SELECT INTO statement. The Embedded SQL SELECT INTO statement is used only when results are retrieved for substitution in the application. The Transact-SQL SELECT INTO statement does not return results to the application and must be issued by using the Embedded SQL EXECUTE statement.

If more than one row is returned, SQLCODE is set to +1, which indicates an exception.

Examples
EXEC SQL SELECT au_lname INTO :name FROM authors WHERE stor_id=:id;

SQL - Identity

IDENTITY (Function)
Is used only in a SELECT statement with an INTO table clause to insert an identity column into a new table.

Although similar, the IDENTITY function is not the IDENTITY property that is used with CREATE TABLE and ALTER TABLE.

Syntax
IDENTITY ( data_type [ , seed , increment ] ) AS column_name

Arguments
data_type

Is the data type of the identity column. Valid data types for an identity column are any data types of the integer data type category (except for the bit data type), or decimal data type.

seed

Is the value to be assigned to the first row in the table. Each subsequent row is assigned the next identity value, which is equal to the last IDENTITY value plus the increment value. If neither seed nor increment is specified, both default to 1.

increment

Is the increment to add to the seed value for successive rows in the table.

column_name

Is the name of the column that is to be inserted into the new table.

Return Types
Returns the same as data_type.

Remarks
Because this function creates a column in a table, a name for the column must be specified in the select list in one of these ways:

--(1)
SELECT IDENTITY(int, 1,1) AS ID_Num
INTO NewTable
FROM OldTable

--(2)
SELECT ID_Num = IDENTITY(int, 1, 1)
INTO NewTable
FROM OldTable

Examples
This example inserts all rows from the employee table from the pubs database into a new table called employees. The IDENTITY function is used to start identification numbers at 100 instead of 1 in the employees table.

USE pubs
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'employees')
DROP TABLE employees
GO
EXEC sp_dboption 'pubs', 'select into/bulkcopy', 'true'

SELECT emp_id AS emp_num,
fname AS first,
minit AS middle,
lname AS last,
IDENTITY(smallint, 100, 1) AS job_num,
job_lvl AS job_level,
pub_id,
hire_date
INTO employees
FROM employee
GO
USE pubs
EXEC sp_dboption 'pubs', 'select into/bulkcopy', 'false'

SQL - Select



SQL




Using the Select List



The select list defines the columns in the result set of a SELECT statement. The
select list is a series of expressions separated by commas. Each expression defines
a column in the result set. The columns in the result set are in the same order
as the sequence of expressions in the select list.



These attributes of the result set columns are defined by the expressions in the
select list:



  • The data type, size, precision, and scale of the result set column are the same
    as those of the expression defining the column.




  • The name of the result set column is the name associated with the expression defining
    the column. The optional AS keyword can be used to change the name, or to assign
    a name if the expression has no name.




  • The data values for the result set column are derived from the evaluation of the
    expression for each row of the result set.



The select list can also contain keywords controlling the final format of the result
set:



  • DISTINCT


    The DISTINCT keyword eliminates duplicate rows from a result set. For example, there
    are many rows in the Northwind Orders table with the same value for
    ShipCity. To get a list of the ShipCity values with duplicates removed:



    SELECT DISTINCT ShipCity, ShipRegion FROM Orders ORDER BY ShipCity


  • TOP n


    The TOP keyword specifies that the first n rows of the result set are returned.
    If ORDER BY is specified, the rows are selected after the result set is ordered.
    n is the number of rows to return, unless the PERCENT keyword is specified.
    PERCENT specifies that n is the percentage of rows in the result set that
    are returned. For example, this SELECT statement returns the first 10 cities, in
    alphabetic sequence, from the Orders table:



    SELECT DISTINCT TOP 10 ShipCity, ShipRegion FROM Orders ORDER BY ShipCity




The items in the select list can include:



  • A simple expression: a reference to a function, a local variable, a constant, or
    a column in a table or view.




  • A scalar subquery, which is a SELECT statement that evaluates to a single value
    for each result set row.




  • A complex expression built by using operators on one or more simple expressions.




  • The * keyword, which specifies that all columns in a table are returned.




  • Variable assignment in the form @local_variable = expression. The SET
    @local_variable
    statement can also be used for variable assignment.




  • The IDENTITYCOL keyword, which is resolved as a reference to the column in the table
    having the IDENTITY property. For example, the IDENTITY property has been defined
    for the OrderID column in the Northwind Orders table, so the
    expression Orders. IDENTITYCOL is equal to Orders.OrderID.




  • The ROWGUILDCOL keyword, which is resolved as a reference to the column in a table
    having the ROWGUIDCOL property.




  • Creating a new column (using SELECT INTO) that uses the IDENTITY property by using
    the specified syntax. For example, to create a new column named counter in
    the authors table that is an int column, you should start at a value
    of 100 and increment by values of 1 for each succeeding number, use counter
    = IDENTITY(int, 100, 1).




  • Temporarily adding a column to the query results that designates whether the CUBE
    or ROLLUP operation added the row or not. Use the GROUPING keyword.



This example shows many of the items that can be in a select list:



SELECT FirstName + ' ' + LastName AS "Employee Name", IDENTITYCOL AS "Employee
ID", HomePhone, Region, 10 AS Constant FROM Northwind.dbo.Employees ORDER BY LastName,
FirstName ASC