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:
Open SQL code that needs to be converted in SQL Server Management Studio or Visual Studio:
- Select the code segment that needs to be converted. If code is not selected, all code in a query editor will be converted
- In the ApexSQL Refactor menu, select the Convert to code command and one of the languages listed:
- 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:
- In the ApexSQL Refactor menu, select the Convert to code command, then the Customize languages option
- In the Customize language templates window, click the New button
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:
- 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