How to 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 Convert to code feature that provides a simple way to convert a T-SQL script into client language-specific syntax – Java, VB.NET, c#, Perl, PHP, Delphi, PowerBuilder, Ruby, C++, or other custom languages for which conversion rules are defined.

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 that needs to be converted in SQL Server Management Studio or Visual Studio:

  2. Select the code segment that needs to be converted. If code is not selected, all code in a query editor will be converted
  3. In the ApexSQL Refactor menu, select the Convert to code command and one of the languages listed:

  4. The code converted into the selected language is stored in the clipboard. Just paste it into desired 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 is needed, create a custom language template, where the language rules can be defined:

  1. In the ApexSQL Refactor menu, select the Convert to code command, 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 it can be easily convert to desired 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