Quickly convert SQL code to language-specific client code

Integrating SQL Server with business infrastructure often requires that developers prepare T-SQL code for use within various client codes, such as C#, PHP, and Perl. This means adding language specific code that will be inserted before and after every SQL statement, as well as escape characters for quotes and code to terminate a line, so that the other languages can parse and execute it correctly. Updating the code manually is a time consuming process prone to mistakes.

This is where ApexSQL Refactor can help. It has the Copy code as feature that provides a simple way to convert your T-SQL script into client language-specific syntax – Java, VB.NET, c#, Perl, PHP, Delphi, PowerBuilder, Ruby, C++, or other custom languages for which you have defined conversion rules.

ApexSQL Refactor is a SQL formatter and database-refactoring tool for SQL Server Management Studio and Visual Studio. Use it to format SQL queries, refactor SQL code, qualify objects, expand wildcards, encapsulate SQL, locate and highlight unused variables and parameters.

To convert SQL code into language-specific code:

  1. Open SQL code you want to convert in SQL Server Management Studio or Visual Studio

  2. Select the code segment you want to convert. If you don’t select it, all code will be converted
  3. In the ApexSQL Refactor menu, select the Copy code as option and one of the languages listed

  4. The code converted into the selected language is stored in the clipboard. Just paste it into your project

Copy code as Java gives:

Copy code as PHP gives:

And that’s it.

If the syntax offered by pre-defined languages is not what you need, create a custom language template, where you can define the language rules yourself:

  1. In the ApexSQL Refactor menu, select the Copy code as option, then the Customize languages option
  2. In the Customize language templates window, click the New button
  3. Specify the language template name, escape characters for quotes, code that will be added before and after SQL statements, and the characters that end a line, so SQL statements can be correctly parsed by the client code language:

  4. The new language will appear in the menu, so you can easily convert your SQL code using the steps given above

Converting SQL code into a specific client programing language doesn’t have to require manually adding the necessary code at the beginning and end of every SQL statement. Use ApexSQL Refactor to create the “translator” that will automatically add the necessary code.

April 4, 2013