Tag: SQLNewBlogger

  • Adding a Computed Column–#SQLNewBlogger

    Recently I needed to add a computed column to a table and realized that I didn’t remember the syntax. This short post show how to do this.

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    Adding the Column

    I had a table, OrderHeader, and wanted to add a new column, OrderedByDate. I can do this with a simple ALTER statement and an ADD. The inserting part is the computation. You use as AS clause with the computation instead of a datatype. For example, my code was:

    ALTER TABLE dbo.OrderHeader 
       ADD OrderedBy AS OrderDate;
    GO

    This added a copy of my OrderDate column with a new name. This is useful for zero downtime deployments in some cases, and in this case, I wanted to just have a copy. However, if I wanted some calculation, I could easily do that by specifying this as I would in a SELECT statement. For example, if I wanted the computed column to be a week later, I could do this:

    ALTER TABLE dbo.OrderHeader 
       ADD OrderedBy AS DATEADDD(DAY, 7, OrderDate);
    GO

    This would add a week to the original value and return that. I can likewise do any sort of string or numeric manipulation I want. A common one is adding or multiplying two columns together for a new value. For example, adding various charges for a total in an order.

    When you do this as a computed column that is not persisted, no space is taken in the actual table rows. If you persist this, then space is used.

    More information on Microsoft Learn.

    SQL New Blogger

    I realized that I needed to double check the syntax in the docs, and when I did, I took this as an opportunity to write a short blog post. I grabbed a link, wrote some code, and then spent 10 minues knocking out this post.

    If some employer does this a lot, they might search your blog to see if you can do this. A nice few posts on how to do this, what persisted does, how you index this, etc. Take a few minutes and start blogging on topics like this throughout your week.

  • Context Info Across Databases–#SQLNewBlogger

    Does Context Info work across databases? This post shows it does.

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers. Here are some hints to get started.

    The Demo

    Someone asked the question, would a trigger in another database see context info from a different database. I thought it should work, but decided to test it.

    Here I’m going to create a table and trigger in database compare2. This is looking for a context value.

    USE compare2
    GO
    CREATE TABLE TriggerTest (myid INT, mychar CHAR(1))
    GO
    CREATE TRIGGER tri_triggertest ON  dbo.TriggerTest FOR INSERT
    AS
    BEGIN
         IF CONTEXT_INFO() = 0x1256698456
             PRINT 'caught'
         ELSE
             UPDATE dbo.TriggerTest
              SET mychar = 'X'
              FROM inserted i
              WHERE i.myid = dbo.TriggerTest.myid
    END
    GO

    Now, back in DB 1, I’m going to set CONTEXT_INFO and insert a value into the database. This should give me a result where the trigger updates the table. A “normal” action.

    USE compare1
    GO
    SET CONTEXT_INFO 0x000
    GO
    INSERT compare2.dbo.TriggerTest (myid, mychar) VALUES (1, NULL)
    GO

    This does, as the table contains a 1 and X.

    Now, same connection, let’s set the magic value for context and insert a row. Now the trigger should avoid the update, letting my bypass the normal action. This is what someone was trying to do.

    SET CONTEXT_INFO 0x1256698456;  
    GO 
    SELECT CONTEXT_INFO(); 
    GO 
    INSERT compare2.dbo.TriggerTest (myid, mychar) VALUES (2, NULL)
    GO

    When I look at the results, I have the “caught” message. The final results from the table are shown here:

    2023-01-27 15_05_55-SQLQuery5.sql - ARISTOTLE_SQL2022.compare1 (ARISTOTLE_Steve (53))_ - Microsoft S

    As you can see here, the context is with the connection, not the database. The database doesn’t matter for this value, it’s whether or not the connection that sets the context (the session really) is still alive when it accesses the other database.

    SQL New Blogger

    This was a quick test for me to answer a question and prove this to someone (and myself). I thought this would work, but I spent 5 minutes devising a test. It took me less than 10 minutes to put this post together.

    This shows volunteerism (helping someone), testing ability, and diligence to prove something I suspected was true. I didn’t assume, I tested. Lots of employers love that.

    You can raise your brand and be a SQL New Blogger like this, showing your knowledge.

  • Select All in a Power BI Slicer–#SQLNewBlogger

    One of the things I’ve been working on this year is a Power BI report for the kids I coaco in volleyball. As a part of this, I want to be able to show all kids, or a few kids, in relation to each other.

    This post covers enabling this for a Slicer on a report.

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers. Here are some hints to get started.

    Adding the Slider

    I have a basic report with a line graph. It looks like this once I’ve added in the value to capture at the Y, the date as the X, and the player as the legend.

    2022-11-06 10_16_41-2022_16Select_Online - Power BI Desktop

    I’ll add a slicer to the report and drop the players in there. I see the players, but I don’t have an easy way to select all of them.

    2022-11-06 10_17_00-2022_16Select_Online - Power BI Desktop

    I can select 1, but if I select another, the report changes. I can CTRL+select to get multiple players, but I want to easily get everyone.

    2022-11-06 10_17_14-2022_16Select_Online - Power BI Desktop

    I knew this was possible because I’d seen other reports show this. I didn’t see anything obvious, so I actually had to search the docs and found a note that explained how to enable this.

    If I click the slicer, I can then select the  Format option on the right, which is the center icon at the top of the visuals pane. You can see the tool tip below.

    2022-11-06 10_17_22-2022_16Select_Online - Power BI Desktop

    Once I do this, I need to expand the Slicer Settings and then the Selection area, as shown here.

    2022-11-06 10_17_30-2022_16Select_Online - Power BI Desktop

    Now I want to click the “Show Select All” button.

    2022-11-06 10_17_35-2022_16Select_Online - Power BI Desktop

    Now my slicer show a Select All at the top.

    2022-11-06 10_17_45-2022_16Select_Online - Power BI Desktop

    Easy.

    I found that when I have a few slicers, which I wanted for this report, it was easy to add them all, then do the formatting for one. If I clicked another slicer, I was in the same format spot and could enable SELECT ALL for the next one. For me, I actually had 3 slicers, so it was nice to format them all very quickly to add this option.

    SQL New Blogger

    This is a really basic post, but it was also something that I didn’t intuitively figure out. I had to research a bit and look around. That’s the skill that many employers need and want.

    This also might trigger someone to ask me about Power BI and what I know, which gives me the chance to talk about the learning and experimenting I’ve done.

    You could do this as well. It took me about 15-20 minutes to grab screen shots and write this.

  • An Update to Skipping the Leading Digit in T-SQL–#SQLNewBlogger

    There was an interesting question in a forum, which I wrote about before. How do you skip the leading digit in a numeric value. I had looked at a UNION, but someone had a better suggestion, so I’m adding to the post.

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers. Here are some hints to get started.

    Another Predicate

    In the previous post, I used a <> and then a > and < with a UNION. Howver, someone noted that NOT BETWEEN is an option. I could rewrite the query this way:

    SELECT *
     FROM dbo.Room AS r
     WHERE room NOT BETWEEN  200 AND 299
    

    This gives me the same result:

    2022-10-05 14_32_43-SQLQuery6.sql - ARISTOTLE.sandbox (ARISTOTLE_Steve (61))_ - Microsoft SQL Server

    This also results in a seek.

    2022-10-05 14_33_30-SQLQuery6.sql - ARISTOTLE.sandbox (ARISTOTLE_Steve (61))_ - Microsoft SQL Server

    The BETWEEN or NOT BETWEN is sargable, meaning we can use an index to find those values that fall into this ordering and exclude them.

    Worth remembering BETWEEN when writing queries for ranges.

    SQL New Blogger

    An update to my post, showing a better way, showing I am happy to take advice from others, and I can improve code. This is a skill that employers need, especially when teams need to become more consistent with coding when working together. Showing you can adapt and grow is a good skill.

    You could write a simple blog like this that shows how you might change some code you wrote in the past. I bet it takes less than 30 minutes.