Developers often have to copy and store SQL scripts in different languages as strings. Manually “translating” SQL scripts into a language-specific string is a tedious process where mistakes are easily made
ApexSQL Refactor is a SQL formatter that can help out by inserting a language specific code before and after a SQL statement and adding escape characters for quotes and code to terminate a line. Copy SQL code as feature provides a simple way to convert a SQL script into a language-specific string – Java, VB.NET, C#, Perl, PHP, Delphi, PowerBuilder, Ruby, C++, or any other custom language for which a conversion rules are defined
In order to copy SQL scripts into another programming language as specific strings, the first you thing that needs to be done is to open the code you would like that needs to be converted in SQL Server Management Studio or Visual Studio:
CREATE TABLE ApexSQLDocs ( DocID INT NOT NULL IDENTITY, DocTitle NVARCHAR(50) NOT NULL, DocFileName NVARCHAR(400) NOT NULL, CONSTRAINT PK_ApexDocs_DocID PRIMARY KEY CLUSTERED (DocID ASC) ) GO
Then, select the SQL segment for converting. If a segment is not specified, ApexSQL Refactor will convert the whole SQL statement.
Now, select the Copy code as option and one of the languages listed in the ApexSQL Refactor menu:
The SQL script which has been converted into the selected language is now stored in the clipboard. Now, all that needs to be done is to simply paste it into the desired project.
For example, Copy code as Java will give:
String sql = "CREATE TABLE ApexSQLDocsn" + "(n" + " DocID INT NOT NULL IDENTITY,n" + " DocTitle NVARCHAR(50) NOT NULL,n" + " DocFileName NVARCHAR(400) NOT NULL,n" + " CONSTRAINT PK_ApexDocs_DocID PRIMARY KEY CLUSTERED (DocID ASC)n" + ")n" + "GO";
And Copy code as PHP will give:
$sql = "CREATE TABLE ApexSQLDocs\n" . "(\n" . " DocID INT NOT NULL IDENTITY,\n" . " DocTitle NVARCHAR(50) NOT NULL,\n" . " DocFileName NVARCHAR(400) NOT NULL,\n" . " CONSTRAINT PK_ApexSQLDocs_DocID PRIMARY KEY CLUSTERED (DocID ASC)\n" . ")\n" . "GO";
However, if the syntax offered by pre-defined languages is not what is needed, a custom template with the Customize languages option can be created and the appropriate rules can be defined. Here, the language template name can be specified, escape characters for quotes, code that will be added before and after SQL statements, and the characters which end a line:
The new language will appear in the ApexSQL Refactor’s Copy code as submenu, allowing to easily convert the SQL code using the steps given above
Copying and converting SQL scripts into a specific programing language strings doesn’t need to require “by hand” modifications. Use ApexSQL Refactor to create a “translator”, which will handle this job.
May 23, 2013