DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Restore a Transaction Log Backup in SQL Server
  • How to Attach SQL Database Without a Transaction Log File
  • A Deep Dive into Apache Doris Indexes
  • Spring Boot Sample Application Part 1: Introduction and Configuration

Trending

  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • How to Convert XLS to XLSX in Java
  • Measuring the Impact of AI on Software Engineering Productivity
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  1. DZone
  2. Data Engineering
  3. Databases
  4. Refactoring T-SQL Codes With SQL Complete

Refactoring T-SQL Codes With SQL Complete

See how easy our coding experience is when the SQL Complete tool from Devart is added, as an add-in, to the SQL Server Management Studio or Microsoft Visual Studio.

By 
Ahmad Yaseen user avatar
Ahmad Yaseen
·
Updated Mar. 07, 23 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
1.7K Views

Join the DZone community and get the full member experience.

Join For Free

The new era of T-SQL coding starts when the SQL Complete tool from Devart is added, as an add-in, to the SQL Server Management Studio or Microsoft Visual Studio. See how easy our coding experience is with the different types of code completion scenarios.

Recall that dbForge SQL Complete is an advanced-as-you-type SQL IntelliSense tool that helps increase the coding speed due to the advanced SQL code autocompletion, smart formatting, and refactoring experience offered by this tool.

In this article, we will see how we can use dbForge SQL Complete in the SQL code refactoring.

But, before start using the SQL Complete add-in, make sure that it is enabled. This can be achieved by opening the SQL Complete menu and choosing the Enable Code Completion option if not enabled yet.

Microsoft SQL Server Management Studio

Rename Automatically Generated Aliases

When a query is generated using the dbForge SQL Complete tool, aliases will be included automatically for the tables, views, synonyms, and TVFs. 

Find that the generated alias does not match your company's coding style. You can easily change all occurrences of that alias easily, without causing any conflict when the name of the alias is similar to any column/ variable name or part of it.

To rename an alias in a SQL query:

  1. Place the pointer over the alias name to be renamed.
  2. Right-click on the alias and select Rename option. Alternatively, you can choose the Rename option from the SQL Complete task menu.
  3. Although the places where the alias name will be changed are surrounded by a dotted square, you can preview the code changes before applying the changes by clicking on F2.
  4. Provide the alternative name for the alias and click tab/enter to apply the new name for all alias occurrences in the query.

Below you can see an example of renaming the aliases in the SQL query using different ways:

renaming the aliases in the SQL queryThe same procedure can also be used to rename the local variables and the SQLCMD variables in your T-SQL script.

Also, this Rename feature can be very helpful when you need to replace a specific database object name, such as a table, column, trigger, login… etc., in your code, making sure that you will be able to automatically find and correct all references to that object.

Semicolon Insertion

Having standards in your code will make it readable and editable by anyone working on it. Using semicolons at the end of each T-SQL statement properly is one of these methods that make the T-SQL code readable.

dbForge SQL Complete can be the best tool that helps us in such a mission. If you want to insert the missing semicolons at the end of each statement in your query:

  1. Open your script in SQL Server Management Studio or Visual Studio.
  2. Right-click on the query window that contains the script and chooses Insert Semicolons. You can achieve the same by choosing the Insert Semicolons option from the SQL Complete menu.

If you prefer using the shortcuts, just click on Ctrl+B and then Ctrl+C to insert all missing semicolons.

The quick demo below shows how to insert all missing semicolons from a stored procedure:

how to insert all missing semicolons from a stored procedure

Convert EXEC to Script

dbForge SQL Complete provides us with the ability to inline the code of the stored procedure or the scalar function in the EXE statement that is used to execute it within the query window in SQL Server Management Studio or Visual Studio.

This feature is very helpful in replacing the stored procedure or the scalar function execution statement with the inline code and reviewing the underlying query that will be executed before being executed.

The Conver EXEC to Script feature can also be used to unwrap the parameterized queries executed within the sp_executesql statement and eliminate the apostrophes around that SQL statement, making it more friendly and easier to work.

To use the Conver EXEC to Script feature to inline a stored procedure or scalar function code or to unwrap a dynamic SQL into parameterized query within an execute statement:

  1. Point the cursor to the name of the stored procedure.
  2. Right-click on the query window and select the Convert Exec to Script option, and the stored procedure name will be replaced with its definition.

The demo below shows how to inline a stored procedure code within a SQL code:

how to inline a stored procedure code within a SQL codeGenerate CRUD Procedures for a Table

CRUD operations are the CREATE, READ, UPDATE, and DELETE operations performed to process the data hosted in our tables. T-SQL developers write stored procedures to make it easier for the users to perform these operations on the database tables.

One of the most useful features in the dbForge SQL Complete tool is the Script Table as CRUD, which generates customizable stored procedures for the four CRUD operations:

·         CREATE stored procedure for inserting data into the table.

·         READ stored procedure for retrieving data from the table.

·         UPDATE stored procedure for modifying data in the table.

·         DELETE stored procedure for removing data from the table.

To generate the CRUD procedure for a specific table:

  1. Browse the target table from the Object Explorer in SQL Server Management Studio.
  2. Right-click on the table name and choose the Script Table as CRUD option from the SQL Complete menu.

The demo below is used to generate CRUD procedure using SQL Complete within SQL Server Management Studio for a database table:

used to generate CRUD procedure using SQL Complete within SQL Server Management Studio for a database tableScript Database Object

Suppose you have a simple or complex query that contains database objects, and you want to check the definition of these database objects. In that case, you need to search for these database objects under the database using the Object Explorer and check/ script the definition of that object from there.

dbForge SQL Complete makes this operation easier and quicker. From your query, you can easily script any database object as ALTER (for Stored procedures, views, functions, and triggers) or script these objects as CREATE (for the tables, schemas, synonyms…etc.).

To script a database object using dbForge SQL Complete as create or alter:

  1. Select the database object to be scripted in your T-SQL query.
  2. Right-click on that database object and select Script Object as CREATE/ALTER option.

You can achieve the same by using the Ctrl+F12 keyboard shortcut.

The demo below shows how to script a database table as created:

ow to script a database table as createdThe demo below shows how to script a stored procedure as ALTER:

how to script a stored procedure as ALTERStay tuned for the next article, in which we will discuss how to use dbForge SQL Complete for code formatting.

Database Microsoft Visual Studio Software requirements Coding (social sciences) sql

Opinions expressed by DZone contributors are their own.

Related

  • How to Restore a Transaction Log Backup in SQL Server
  • How to Attach SQL Database Without a Transaction Log File
  • A Deep Dive into Apache Doris Indexes
  • Spring Boot Sample Application Part 1: Introduction and Configuration

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!