Archive

Archive for December, 2008

Execute dynamic SQL in MSSQL

December 18th, 2008

I hit this need when I was developing own custom module into Commerce Server. Commerce Server have all tables separated into catalogs. Catalog is group of tables which contains records for particular client (site), but have same structure like all other catalogs. Lets say that we have two clients ‘Client1′, ‘Client2′. Each client has tables for products ‘Client1_Products’, ‘Client2_Products’. Now we can create two procedures for retrieving data from both tables or better we can create one procedure, which will have one input parameter - Catalog name.

We can create the procedure:


CREATE PROCEDURE [dbo].[GetProducts]
(
  @CatalogName nvarchar(85),
  @Language nvarchar(85) = 'en-US',
  @ProductID nvarchar(255)
)
AS
BEGIN
  SET NOCOUNT ON;

  DECLARE @sql nvarchar(4000)
  SET @sql = 'SELECT
      *
    FROM [dbo].[' + @CatalogName + '_Products] AS [Products]
    INNER JOIN [dbo].[' + @CatalogName + '_Products_' + @Language + '] AS [ProductsLang] ON
      [Products].[ProductID] = [ProductsLang].[ProductID]
    IF(@ProductID IS NOT NULL)
      SET @sql = @sql + ' WHERE [Products].[ProductID] = @ProductID '

  EXEC sp_executesql @sql,
    N'@ProductID nvarchar(85)',
    @ProductID
END

Now we are able to call this procedure for any client and as bonus get language related data for products. EXEC [dbo].[GetProducts] ‘Client1′, ‘en-US’; will return all products for ‘Client1′.

We can specify product ID if we want to receive only one product. Product ID condition will be added to where.


IF(@ProductID IS NOT NULL)
  SET @sql = @sql + ' WHERE [Products].[ProductID] = @ProductID '

At the end of stored procedure we will execute generated SQL.


EXEC sp_executesql @sql,
  N'@ProductID nvarchar(85)',
  @ProductID

First parameter of sp_executesql is a Unicode string that contains a Transact-SQL statement or batch. More complex Unicode expressions, such as concatenating two strings with the + operator, are not allowed at this point. On 64-bit servers, the size of the string is limited to 2 GB, the maximum size of nvarchar(max).

Second parameter is list of strings that contains the definitions of all parameters that have been embedded in first parameter. Every parameter must be defined in @params.

Third parameter are values for the parameters that is defined in the second parameter.

OUT or OUTPUT indicates that the parameter is an output parameter. By this, you can get last inserted sequence number for example.

Database , , , ,

Power Toys for Visual Studio

December 3rd, 2008

‘Ok. Who changed this part!? Who can I blame?’

Some time ago I was looking for possibility to have functionality similar to ‘Blame’ - in Subversion - into Visual Studio for Source control. After some time just found what I’m looking for. It’s called Team Foundation Server Power Tools. It doesn’t contains only functionality for blaming your colegues, there is a lot more.

Unshelve Command

Use the unshelve command to unshelve and merge the changes in the workspace.

Rollback Command

Use the rollback command to roll back changes that have already been committed to Team Foundation Server.

UU Command

Use the UU (Undo Unchanged) command to undo unchanged files, including adds, edits, and deletes.

Annotate Command

Use the annotate command to download all versions of the specified files and show information about when and who changed each line in the file.

Review Command

Use the review command to optimize the code review process to avoid checking in or shelving.

History Command

Use the history command to display the revision history for one or more files and folders. The /followbranches option returns the history of the file branch’s ancestors.

TweakUI Command

Use the tweakUI command to modify Team Explorer client connection values. This command enables you set various connection settings. In addition, this command enables you to define the client certificate needed to connect to a Team Foundation Server that has been configured to require client-side certificates.

… and more.

To be honest, I still think that is better to use SVN as source control, many times we have problem with MS source control. Some files is not possible to get from repository with ‘Get latest version’ command, it doesn’t recognize files which are not modified but were saved.

Power Toys for Visual Studio Pack Installer

Power Toys for Visual Studio Pack Installer

Power Toys

Power Toys for Visual Studio are small tools that provide aid to developer pain-points or assist in diagnosing development-related issues. In addition to providing support, the power toys are released as Microsoft Shared Source to provide sample code to real-world solutions and allow for collaborative-development.

The Pack Installer is your one-and-only tool for staying up-to-date with all community releases for Visual Studio and development. The 1.0 version is now available as a Shared Source Release on CodePlex. Last release includes new UI improvements, file download verification, and the availability of the Pack Installer specification and test plan.

Visual Studio