Tag: SQLNewBlogger

  • What Backups Are In This File?–#SQLNewBlogger

    I had a question on multiple backups in a file and had to check my syntax. This post shows how to see which backups are in a file.

    Note: Don’t do this. Put backups in separate files.

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

    Setup

    I have a sandbox database. I made a backup of this.

    BACKUP DATABASE [sandbox] TO  DISK = N'D:\SQLBackup\sandbox.bak' 
       WITH NOFORMAT, INIT,  
       NAME = N'sandbox-Full Database Backup', 
       SKIP, NOREWIND, NOUNLOAD,  STATS = 10
    GO

    Note I used INIT, which will ensure this is the only backup in this file.

    I then changed something, in this case, I made a new table (I was testing things for Rich).

    CREATE TABLE testforrich (myid INT)
    GO
    INSERT dbo.testforrich (myid) 
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
      FROM sys.columns AS c
    GO

    I then ran another backup. However, this time I wanted to append to the existing file.

    BACKUP DATABASE [sandbox] TO  DISK = N'D:\SQLBackup\sandbox.bak'
      WITH NOFORMAT, NOINIT,  
      NAME = N'sandbox-Full Database Backup', 
      SKIP, NOREWIND, NOUNLOAD,  STATS = 10
    GO

    The NOINIT keyword is in here, which appends the backup to the same file. In essence, sandbox.bak will then contain two different backups in one file. For this test, I then made another change and another backup.

    TRUNCATE TABLE testforrich
    GO
    BACKUP DATABASE [sandbox] TO  DISK = N'D:\SQLBackup\sandbox.bak'
      WITH NOFORMAT, NOINIT,  
      NAME = N'sandbox-Full Database Backup', 
      SKIP, NOREWIND, NOUNLOAD,  STATS = 10
    GO
    
    

    Now I have three backups in the file.

    Checking Contents

    If I were to click the restore item in SSMS and pick the file, I see this:

    2023-05-03 10_15_53-Restore Database - sandbox

    Note that the position is listed as “3”, which means this is restoring the newest (most recent) backup by default. I don’t seem to be able to edit this, though if I click timeline and change the time, I can get a different backup. I see different backups in there:

    2023-05-03 10_30_16-Backup Timeline_ sandbox

    However, when are those backups? This timeline isn’t great.

    I can use RESTORE HEADERONLY. The command I ran is:

    RESTORE HEADERONLY FROM DISK = 'd:\sqlbackup\sandbox.bak'
    GO

    This gives me all three backups, which are shown as different positions in the file.

    2023-05-03 10_31_38-SQLQuery7.sql - ARISTOTLE.sandbox (ARISTOTLE_Steve (72))_ - Microsoft SQL Server

    From here, I could perform a restore with a different backup if I needed to.

    SQL New Blogger

    This was a quick post that I wrote after I spent 5 minutes creating a test for something. I grabbed my code, took a few screen shots, and it took about 10 minutes to assemble this.

    Easy for you, and this shows a potential interviewer or manager that you can dig into a small issue, learn, and solve it. Try it for yourself and write a blog post.

  • 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.