Friday, 29 November 2013

Personal preferences % (modulus of - as in software logic) Others' = Public Opinion

Public opinion, rumor and gossip are similar in one respect - they are like the modulus operator ('%'),

if the other side is 'even', the result is zero; if the other side is 'odd', the remainder gets calculated ! :)

Table value parameters - SQL Server

Table value parameters are the best way to pass a list or dictionary or key-value to a SQL Server stored procedure, a feature available from SQL Server 2008 onwards.

It is sometimes funny, this trend of many useful software and language features going unnoticed, when their applicability and usefulness is quite high. It is also likely that the usage or non-usage, of many such features may have influenced some programming trends or caused many simpler techniques to disappear.

One such feature is the usage of custom types, in SQL Server through the Table Valued Parameter (TvP), that makes a programmer's life simple.

A TvP is a custom type that can be defined for any database with the syntax,

Create type [dbo].[typeName] as table(
fieldName (type)(size)

Since arrays are not built-in to SQL Server, the customer type TvP helps in passing values to stored procedures as parameters of a SQL query with a SQLCommand or a SQLDataAdapter object.

There are many ways o passing values to a TvP type but the simplest is a DataTable object. Build the custom columns (DataColumn collection) corresponding to your TvP fields and add the values  as DataRows to the DataTable object.

So, the TvP is actually a type of table - a UserDefined TableType and hence, its name ! And so, due to less resources for some reason on the web, regarding its usage, this extremely useful data type or rather, table type is rarely used with its predecessor, the XML type more preferred.

The single most important thing when using a TvP is to remember that it is a table type and therefore, when using it in stored procs or functions, it is recommended for use with an alias failing which weird error messages may crop up totally unrelated to the type's structure or usage!

Many going the bookish way do not have much use for the TvP because it can be used only with "Insert" - this is what is the definition for usage but the context is not mentioned with the restriction leading many to believe that it must be a strange type !

It can be used within the SQL Server context with "Insert" because being a table type, you need to insert values into it first and only then you can use it! But from a program context, you can use it for any purpose - "update", "delete" or whatever.

The example below shows how to create a TvP, declare it in a stored procedure and iterate through it with the help of a cursor.


For a TvP, named 'myList'

// TvP declaration in SQL Server

use myDb;
create type myList as table(
myListValue nvarchar(max) 

// Stored Procedure

create procedure myProc

@myListVar myList READONLY       -- ReadOnly is a required attribute for a TvP

declare @listName nvarchar(max)

declare @getListValues cursor
set @getListValues=cursor for

-- below, you are consuming the field defined in the TvP and getting the list value.
select c.myListValue from @myList c

open @getListValues

fetch next from @myListValue into @listName


-- update statement

fetch next from @myListValue into @listName
close @getListValues
deallocate @getListValues

Program code:

using System.Data;
using System.Data.SqlClient;

... more statements - class etc

DataTable dataTableObject=new DataTable("typeName");

The name of the DataTable, supplied as a parameter to its constructor, is the custom type defined in the database. The column collection corresponds to as many fields as is defined in the type.

SQLCommand/SQLDataAdapter cmd=new SQLCommand/SQLDataAdapter("StoredProcName",sqlConnectionObject);




cmd.ExecuteNonQuery(); // being an update query in the stored procedure.

Tuesday, 26 November 2013

NVarchar (in SQL Server) - the mysterious 'N'

Sometimes the most preferred data type can cause problems due to the stereotyped acceptance that if it is the most adopted and works, it must be the best.

NVarchar is one such type.

Although this SQL type is quite likely the 'good' one and the 'chosen' one for all text type data requirements, it is not for at least two common requirements related to data-warehousing and transport.

1. Not to be used if the requirement is to have a columnar storage index.
2. Not to be used if the requirement is to export data in a flat file. I made this as a note for some future reference for myself but upon re-read it falls 'flat' !

The 'N' of course is extremely useful as a type prefix if you wish to avoid collating an entire database because just changing the data type of the table column to 'N' type will enable non-English text to be inserted into the column due to the unicode compatibility of the 'N' type.

Thursday, 21 November 2013

Game 9 - If it were a child's play

The board position after Black queens with a check and white can play Nf1, Rf1 or Bf1 to save the check.

Anand played Nf1.

The evaluation line was expecting Bf1 and below are some possibilities with a few lines leading to a checkmate in favor of Anand.

If White had played, Bf1

 and black responds with Qxf6 because the impending R - > h4 threatens mate on h7 and black has to rescue its king from it

and if intead of Qxf6, if black plays Nxf6  White's gxf6, Qg7# wins for White.

But then if it were only a child's play! :)

Monday, 18 November 2013

Change and 'generation gap'

Because 'Change', of a social or mass kind. happens over a period of time or say, generations, to understand how effecting 'change' effectively happens, it is important to first dispel stereotyped understanding of words like 'generation gaps'.

As it is understood and propagated, a generation gap is related to lack or lesser numbers of effective communication channels between different generations. Understand it, instead, as "reduce the gap between generations by introducing generations to current age demand and technologies and past shortcomings" and change, instead of happening over decades, will begin to happen in years and even months. 

Friday, 15 November 2013

Game 5 - huh?

Protocols are good to enforce and profess but only when a correct disposition towards convention and accepted compliance to what is correct in the given context  is also considered as a given.

The FIDE website on the FWCM 2013 has the link to the Game 5 Replay still pointing to game 4 !!

If a website is about a World Championship event, it is normal convention to be reflecting up-to-date information to reflect commitment to quality and maintain integrity.

Wednesday, 13 November 2013

And Carlsen misses a decisive counter-trap - game 4 !!

Game 4 - Carlsen takes the Berlin route and trades for safety in move 35.

Vishy plays 35 Ne4 nviting Carlsen to gobble up the pawn on g4 and Carlsen obliged...the experts exclaimed as to why the need for speed.

because after 35...Rxg4+ 36. Kf2, everybody wanted black to play Ne5 or Rd8 to counter 37. Nd6+

The idea of 36...Ne5 is to support the rook on g4 and 36...Rd8 is to keep White from playing Nd6+

Here is the position following 36...Rd8.

37. Nc5+ bxc5 38. Rxg4 Ne5 and black threatens to take the rook back with Nxg4+

if 39. Rg3? Bxb5 (takes the piece back and the exchange fork remains!)


but Carlsen obliged Anand's favor in game 3 and let go with a simple Rf4+ and rook back to Rf8 !!

Of course, it might be argued that Anand may not have played Nd6+ or Nc5+ but a World Champion does not sacrifice a pawn just like that and go two pawns down and a World no. 1 is clearly not in his elements if he cannot follow a simple unwritten practice in Chess - capture an open rank with rook and the more important, activate a passive piece and bring it in into play!

So that the above is not seen as a criticism of two greats of the game, I must note here that it is an observation of how two Grandmasters approach a likely-long championship, with both seizing on the first opportunity to shun complex, potentially explosive combinations to positional play.

Incidentally, this means that both are relying on positional play, which is human and cannot be analysed as well with the help of computer it? :)

Tuesday, 12 November 2013

Vishy misses a clear advantage ? - Game 3 World Chess Championship 2013

It is very rare that a position of this nature comes forth in a World Championship game, especially when both the players are known to be sharp.

The position is after the 29th move of Game 3 of the ongoing World Chess Championships being played in Chennai between Vishwanathan Anand, the reigning world champion and Magnus Carlsen, the World no.1 and the challenger to the crown.

1. Nf3 d5 2. g3 g6 3. c4 dxc4 4. Qa4+ Nc6 5. Bg2 Bg7 6. Nc3 e5 7. Qxc4 Nge7 8. O-O O-O 9. d3 h6 10. Bd2 Nd4 11. Nxd4 exd4 12. Ne4 c6 13. Bb4 Be6 14. Qc1 Bd5 15. a4 b6 16. Bxe7 Qxe7 17. a5 Rab8 18. Re1 Rfc8 19. axb6 axb6 20. Qf4 Rd8 21. h4 Kh7 22. Nd2 Be5 23. Qg4 h5 24. Qh3 Be6 25. Qh1 c5 26. Ne4 Kg7 27. Ng5 b5 28. e3  (the adjacent image has analysis by Fritz, barring the first line, which is quite a bizarre suggestion all the other lines of variations shows ...dxe3 29. Rxe3 Bxb2) 

but Anand played 29...Bd4 !?

So, I thought of analyzing the ...Bxb2 line and the results were + all the way for Black even though a draw, forced by White through a Bishop sacrifice (resulting from a sharp-ish Ra6 line for White, annotated below), does look the most likely result, the way to the draw shows a lot of interesting plus-es for Black that might have led to a pressure situation for White! Maybe what Carlsen meant by "survived a scare"!

Here is the 29....Bxb2 line

At the press conference, Anand mentioned "Re1 and White had enough compensation" but not really as the below position shows after 30. Nxe6+ fxe6 31. Re1

31...e5 32. Rb1 Bd4 33. Re2 Qf7 34. Bc6 b4 35. Qe4  Rd6 36. Qf3 b3   -+

Below is a sharp-ish variation that Carlsen may have played with the above assumption that Carlsen would be only too eager to release his Queen from the 'h' square quickly and bring it into play.

30. Nxe6 fxe6 31. Ra6 e5 32. Be4 Rd6 33. Bc6! (allowing for Qe4 to come into play, freeing the Queen, and at the same time threatening the b5 pawn.