Tag: SQL Prompt

  • SQL Prompt Fixes Poor Order By Coding

    SQL Prompt is a fantastic coding aid, but it does more than format your code and provide intellisense. Over time, the team has enhanced SQL Prompt to also guide you along and fix some bad code that your team might write out of habit.

    SQL Prompt 10.1 released recently, and one of the recent fixes is for an issue that we’ve denoted as BP002. This is where someone writes this code.

    SELECT arts.ArticlesID
          , arts.Title
          , arts.Description
          , arts.PublishDate
          , arts.URL
      FROM dbo.Articles AS arts
      WHERE arts.PublishDate > DATEADD(WEEK,-2,arts.PublishDate)
      ORDER BY 1

    I don’t see this a lot from Microsoft stack developers, but PHP, Java, and some other languages will do this.

    This has been deprecated, but it’s a poor practice as well. The dependency between the ordering and column list is not good. This is an easy place to make mistakes over time as code is refactored.

    If I do this, SQL Prompt gives me a green squiggly line below the constant.

    2020-04-02 11_49_05-SQLQuery7.sql - Plato_SQL2019.SimpleTalkDev_Steve (PLATO_Steve (63))_ - Microsof

    If I put the cursor here, I get a lightbulb in the sidebar. Clicking that will give you some options.

    2020-04-02 11_49_13-SQLQuery7.sql - Plato_SQL2019.SimpleTalkDev_Steve (PLATO_Steve (63))_ - Microsof

    If I click the first item, this will give me a placeholder where I can choose a column. We could replace this with the first column in the column list, but if you detect this during refactoring, the first item from the column list might not be the one you want.

    2020-04-02 11_51_20-SQLQuery7.sql - Plato_SQL2019.SimpleTalkDev_Steve (PLATO_Steve (63))_ - Microsof

    I can start typing and intellisense takes over.

    2020-04-02 11_49_29-CandidateList

    It’s a small thing, but this is a one way that increases code quality for developers that might not know better.

    If you haven’t tried SQL Prompt, download an eval and see what you think. If you have it, upgrade and ensure you have all the code fixes.

  • SQL Prompt and SSMS 18.5

    I downloaded SSMS 18.5, as there were a lot of fixes in here and thought this was a good upgrade to try. I’d heard reports of major changes in the VS shell causing issues with various add-ins, including Redgate tools, so I decided to verify things. Especially for SQL Prompt.

    Tl;dr: The SQL Prompt fix is here: http://download.red-gate.com/checkforupdates/SQLPrompt/SQLPrompt_10.1.4.14671.exe

    As soon I installed this and started SSMS, the fun began.

    2020-04-08 11_37_40-Microsoft SQL Server Management Studio

    2020-04-08 11_37_47-Microsoft SQL Server Management Studio

    2020-04-08 11_37_53-Microsoft SQL Server Management Studio

    2020-04-08 11_38_03-SQL Prompt

    2020-04-08 11_38_12-Microsoft SQL Server Management Studio

    2020-04-08 11_38_17-Microsoft SQL Server Management Studio

    This was all before the SSMS login. I connected to a database and SQL Prompt is definitely broken.

    2020-04-08 11_39_40-SQLQuery1.sql - Plato_SQL2017.sandbox (PLATO_Steve (58))_ - Microsoft SQL Server

    Fortunately, Redgate released a fix today, which you can download from http://download.red-gate.com/checkforupdates/SQLPrompt/SQLPrompt_10.1.4.14671.exe

    I downloaded that and ran the install. I also upgraded SQL Change Automation at the same time. After this, I restarted SSMS and Prompt now works.

    2020-04-08 11_51_25-CandidateList

    Other fixes are on the way, as the teams are scrambling. This isn’t a big fix, but it is updating some things that changed as MS upgraded part of the shell.

    The most important tool is now working. If you’ve never tried it, download SQL Prompt today and see how much faster you can write code.

  • Schema Filtering in SQL Prompt

    I love SQL Prompt, and I’m regularly impressed by the enhancements our teams continue to make in the product. One item that I found interesting recently was schema filtering. This post takes a quick look at this feature.

    If I go to look for an object, intellisense is amazing. In this case, I’ll type “SSF <tab> cust”. I’ll see this:

    2020-02-27 15_03_37-ObjectDefinitionBox

    There is the customer table first, and as we see on the right, this is the dbo.customer table. Below this, we see there is a Customer table in the ETL schema and two CustomerLoad tables, which are in two different schemas.

    I might actually be working in one of those schemas. Let’s say I’m an ETL developer. I don’t want to accidentally be coding against dbo.Customer when I want to use ETL.Customer. Certainly I might need each one, but in this case, I might just care about loading stuff into the ETL schema and want to work there.

    I can add filtering in SQL Prompt by going to the Options and then the Connections section. As you can see below, I have a drop down for schemas.

    2020-02-27 15_05_03-SQL Prompt – Options

    The default is all schemas, in all databases, but I can change this. I can set a filter. In this case, I’ll choose the “Only load…” option. When I do this, I see a generic wildcard here.

    2020-02-27 15_06_45-SQL Prompt – Options

    I see schemas for all servers and databases. However, I can set filtering for a specific server and database. Or a database on all servers, or certain servers, but all databases. Here I’ll just edit the schema.

    2020-02-27 15_06_05-SQL Prompt – Options

    This will change behavior to only show the ETL schema in all databases on all servers. Now when I type the same thing, I see the ETL schema objects only.

    2020-02-27 15_08_32-ObjectDefinitionBox

    Good, right? Plus, the refreshes and loads are faster. The downside here is that in another database, I see no tables.

    2020-02-27 15_09_14-CandidateList

    I can fix this, and just look in that one database by entering the database name. I’d suggest you set this by server and database, just so you are clear what behavior is happening, but it’s up to you.

    This was a request from developers that do work in some large schemas, and only want to see those objects. It’s been added to SQL Prompt, so update today, upgrade, or download a trial and give it a try.

  • Using Parameters in #SQLPrompt

    I am a big fan of snippets in SQL Prompt, often using them in demos to quickly get code written. However, I’ve liked the idea of snippets and templates for a long time. These are great time savers, and they can dramatically improve productivity and code quality.

    How? If you have certain constructs in your environment that developers struggle to remember or implement, make a snippet. This makes things very easy and consistent. It’s a great way to help younger developers learn as well.

    Here’s an example.

    Create Primary Keys

    I have worked with no shortage of developers that build tables like this:

    CREATE TABLE Shipper
    ( ShipperKey INT NOT NULL
    , ShipperName VARCHAR(100)
    , ShipperAddress VARCHAR(100)
    , ShipperCity VARCHAR(100)
    , ShipperRegion VARCHAR(20)
    , CountryCode CHAR(3)
    )
    GO

    This isn’t a great design, but more importantly, deploying this results in a heap. Perhaps another issue is that there are no indexes, which isn’t usually a good idea.

    A better idea might be a table like this:

    CREATE TABLE dbo.Shipper
    ( ShipperKey INT NOT NULL CONSTRAINT ShipperPK PRIMARY KEY
    , ShipperName VARCHAR(100)
    , ShipperAddress VARCHAR(100)
    , ShipperCity VARCHAR(100)
    , ShipperRegion VARCHAR(20)
    , CountryCode CHAR(3)
    )
    GO
    CREATE INDEX Shipper_Region ON dbo.Shipper (ShipperRegion)

    Now we can’t template all of this, but we can do a few things. I’ll show you how Prompt facilitates this.

    A Customized Snippet

    Let’s start with the basic code. I know I need a table name, I’ll want a PK, and I want to help someone add at least one index. With that in mind, I’ll build this snippet code. Note that I’ve replaced the table name with a parameter. I did this with a search and replace in the script.

    CREATE TABLE dbo.$TableName$
    ( $TableName$Key INT NOT NULL CONSTRAINT $TableName$PK PRIMARY KEY
       $CURSOR$
    )
    GO
    CREATE INDEX $TableName$_ ON dbo.$TableName$ ()

    You can see this in the SQL Prompt Snippet Manager. Note that the parameter (or placeholder) has been inserted in the bottom by Prompt.

    2019-12-10 21_07_39-SQL Prompt - Create New Snippet

    One other thing I might do is add a schema placeholder like this:

    2019-12-10 21_08_35-SQL Prompt - Create New Snippet

    Note that I’ve added a default for schema, as this is usually dbo. I’ll also click the up arrow to the right to ensure schema is entered first.

    Now, let’s use this. I’ll save this and close the options. Then in a new query window, I’ll type my snippet beginning as “crt”. I see this:

    2019-12-10 21_09_41-CandidateList

    My snippet is listed. I can select it and I’ll then see this code. See how Prompt has inserted my snippet, but already highlighted the schemaname parameter and given me the intellisense of the schemas in my database.

    2019-12-10 21_11_28-SQLQuery4.sql - Plato_SQL2017.sandbox (PLATO_Steve (64))_ - Microsoft SQL Server

    I’ll type dbo and Enter. Prompt moves to the next placeholder parameter. Here I see TableName highlighted all over.

    2019-12-10 21_12_31-SQLQuery4.sql - Plato_SQL2017.sandbox (PLATO_Steve (64))_ - Microsoft SQL Server

    If I type “Shipper”, I see this.

    2019-12-10 21_12_40-SQLQuery4.sql - Plato_SQL2017.sandbox (PLATO_Steve (64))_ - Microsoft SQL Server

    Now I’ll hit Enter again. This time Prompt puts the cursor where I need it to start entering other columns.

    2019-12-10 21_13_45-SQLQuery4.sql - Plato_SQL2017.sandbox (PLATO_Steve (64))_ - Microsoft SQL Server

    I can easily enter my columns, and now my table has a PK. What’s more, if I enter a few columns and run this, I’ll see an error.

    2019-12-10 21_14_33-SQLQuery4.sql - Plato_SQL2017.sandbox (PLATO_Steve (64))_ - Microsoft SQL Server

    The table was created, but the index statement isn’t correct. While this doesn’t necessarily ensure developers follow naming standards or create an index, at least this will get them to think about it. They can correct the statement by adding a column to the index statement between parenthesis, and hopefully change the name.

    If you haven’t seen how Prompt can really improve your coding, download an eval today and give it a try. If you have it, take advantage of snippets.