Tag: SQLNewBlogger

  • Use SCOPE_IDENTITY()–SQLNewBlogger

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

    I ran across a question on Facebook, of all places, the other day. Someone had asked a friend how to return a value from a procedure and assign it to a variable. My friend answered, but in the discussion, I noticed the poster was looking to return @@IDENTITY to the calling procedure as the value of the row that was just inserted.

    Don’t do that. At least not without understanding the potential issues.

    It’s been years since I’ve seen @@IDENTITY in use, and for a number of years before that, this was an easy “weed out” question in interviews.

    If you look at the documentation for @@IDENTITY, the documentation notes that SCOPE_IDENTITY() and @@IDENTITY both return the last identity value inserted in the table, but @@IDENTITY is not limited in scope to the current session.  This means that when concurrent inserts occur, you could receive the identity value of another session. Depending on how you use this value, that may or may not be an issue.

    How does this work? Let’s create a simple table with an identity. I also create a logging table and a trigger that will add a message to my logging table when I add a row to the first table.

    CREATE TABLE newtable
        (
          id INT IDENTITY(1 ,1)
        , mychar VARCHAR(20)
        );
    GO
    CREATE TABLE Logger
     (logid INT IDENTITY(56,1)
     , logdate DATETIME
     , msg VARCHAR(2000)
     );
    GO
    CREATE TRIGGER newtable_logger ON dbo.newtable FOR INSERT
    as
      INSERT INTO logger VALUES (GETDATE(), 'New value inserted into newtable.')
    RETURN
    ;
    go
    

    If I run this, what do I expect to be returned?

    INSERT INTO dbo.newtable
            ( mychar )
    VALUES  ( 'First row'  -- mychar - varchar(20)
              )
    
    SELECT @@IDENTITY
    
    
    
    

    However I get this. A 56 in my result set for @@identity.

    2015-09-22 17_32_20-Cortana

    Why?

    The reason is that the last identity value was 56, from the logging table. The order of operations is

    • insert value into newtable
    • @@identity set to 1
    • trigger fires
    • insert into logger
    • @@identity set to 56

    That’s often not what we want when capturing an identity value. What’s worse, this behavior can exist, but not manifest itself until someone changes a trigger later.

    If I change this SCOPE_IDENTITY(), I get a different result.

    2015-09-22 17_38_26-Start

    This is because the SCOPE_IDENTITY() function takes the scope into account and doesn’t get reset by the trigger code.

    SQLNewBlogger

    This took some time to write. Mostly because I had to setup the demo, test things, and then get the explanation straight in my head. It took me 15-20 minutes, including lookup time in BOL, but if you are new to writing, this might take a bit longer. You’d also want someone to review your explanation since this can be tricky to explain.

    Reference

    • @@IDENTITY
    • SCOPE_IDENTITY()
  • Remember the N

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

    I saw a post recently from someone that was having trouble with Chinese characters being inserted into a SQL Server table. I’m not sure if they were using an application or just SSMS, but they inserted this:

    insert into mytable select ‘<IDC>亮块(DS3或DS4)-清理/维修显像滚筒</IDC>’

    And they got this in the table:

    <IDC>??(DS3?DS4)-??/??????</IDC>

    That’s a problem that I can see, even if I can’t read Chinese.

    Someone else posted a note that when you insert, you need to let SQL Server know your string is Unicode. That means prefixing your string with an N’.

    I looked in Books Online, and found this note under the nvarchar section:

    “Prefix Unicode character string constants with the letter N. Without the N prefix, the string is converted to the default code page of the database. This default code page may not recognize certain characters.”

    I think this means that the N’ isn’t necessarily required, but it depends on the default code page of your database. For most of us, if we don’t include it, I believe our data gets converted to UTF-16, which might not be what we want.

    SQLNewBlogger

    I ran across the post and spent 5 minutes researching things and looking in BOL. This really took about 5 more minutes to write.

    Reference

    NVarchar –  https://msdn.microsoft.com/en-us/library/ms186939.aspx

  • How Many Times Will You Change a Password?

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

    If you create a login and the user can’t log in, how many times will you change the password?

    It turns out I’ll do it 5 times.

    I was setting up a new installation of DLM Dashboard on a test machine. In the setup it asks for an account to run under. I dislike setting my own account (even for tests), so I flipped over to SSMS and added a new login, entered a password, unchecked “require change” and set this as a sysadmin. I clicked OK and returned to Chrome.

    I entered the password and hit “Add”, only to get the “login failed” message for the user. Surely I mistyped something, so I typed the password again, with the same result.

    Maybe I mistyped it in SSMS. Go back, change it to the same thing, adding a character in SSMS (let’s call this the first change) and then hit enter in Chrome.

    Failure.

    Maybe I mistyped it. Go back to SSMS, change the password again (now twice), this time making it simpler. Uncheck the “policy check” and try again.

    Failure.

    Hmmm. I’m confused. Let me type a password in Notepad. I’ll copy paste that in SSMS (now 3 times) and into Chrome.

    Still a Failure.

    At this point I’m confused. Why can’t a new user log in? I’m wracking my brain.

    Maybe I have a sticky keyboard key? I’ll change the password again, this time to 5 of the same character (now 4 changes). I go slowly, typing the same 5 characters into Chrome.

    Failure.

    What’s the cause? I’m starting to wonder if perhaps logins aren’t allowed on a protocol, and it hits me. SQL Authentication.

    I go to the instance properties and I never allowed SQL Authentication when I installed SQL Server. After all, this is a test machine.

    Change that and restart SQL Server. Change the password again (5 times) to a decent password that won’t be guessed if someone gets to this machine.

    DLM Dashboard setup proceeds.

    SQLNewBlogger

    We all make mistakes. We do things wrong. Talk about how you learn and figure things out. This is a good story and lesson for me.

    Resources

    I should know better.

  • Upgrading a SQL Azure Database to v12

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

    I was looking to test some of the Azure v12 capabilities, but I needed to upgrade a database. Here’s a quick look at how.

    In the old portal, you’ll see your databases.

    2015-08-14 13_49_10-SQL Databases - Microsoft Azure

    I love that there’s no information on database version. I’m sure the Azure people love this, but I don’t. Very, very annoying.

    If I pick one of these, and go to the “Quick Glance” items on the lower right, I’ll see the “update to v12” message.

    2015-08-14 13_49_34-SQL Databases - Microsoft Azure

    However in the new portal, it’s different. At first glance it looks the same, but zoom in.

    2015-08-14 13_51_00-SQL databases - Microsoft Azure

    Now you can see the icons are different. I couldn’t read the 12 at first, but I did notice the color difference.

    2015-08-14 13_52_12-SQL databases - Microsoft Azure

    Now I can go into the properties of the database and see those. Mine said v2 here, which was strange. Anyway, pick that.

    2015-08-14 13_54_02-Movies & TV

    You’ll get a new blade, which will show you the upgrade benefits. Select “Upgrade this server”.

    2015-08-14 13_54_07-Latest SQL database update - Microsoft Azure

    Now you get a blade to upgrade the server.

    You must type the server name, not the database name, to upgrade. Note that all databases on this server will be upgraded. Be aware of that. However, it’s not the full server name.

    2015-08-14 13_54_48-New notification

    It’s just the host, but a tool tip will prompt you if you mess that up.

    2015-08-14 13_55_00-

    Once that’s done, the upgrade will start on the blade. You can close this.

    2015-08-14 13_55_07-Movies & TV

    The upgrade will also appear in the main blade for the database. These are scheduled, not real time.

    2015-08-14 13_55_14-Movies & TV

    However, within about 3-4 minutes, mine changed to “upgrading” from the schedule notice.

    2015-08-14 14_00_05-Movies & TV

    This was a small database, with just one table in it. However the complete upgrade took awhile. The database is available and usable while this is happening, but it does take time to complete. I think mine took about 15 minutes before it was done, but I wasn’t watching the entire time, so I could be wrong.

    Once it’s done, it has the new version

    2015-08-14 14_04_30-Mytester - Microsoft Azure

    Easy enough, and if you need to upgrade your systems, here’s a simple way. If you have lots of them, I’d use PoSh.

    SQLNewBlogger

    This post didn’t take that long to write. I had the database ready, and I took a few screen shots then started the upgrade. I wrote most of this while waiting for the process to complete and then went on to other work and came back for the final screenshot.

    Easy enough, and you should practice this and document it. You’ll have some idea of how to use Azure if someone asks you to later along with some notes for yourself.

    References

    None