Category: Blog

  • No Defaults – T-SQL Tuesday #68

    tsqltuesday

    It’s the second Tuesday of the month, and time for T-SQL Tuesday. This month’s invitation is from Andy Yun, where he asks you to Just Say No to Defaults.

    This is the monthly blog party started by Adam Machanic, and it’s the chance for you to write on a particular topic every month. The hosts rotate, so you have to watch the #tsql2sday hashtag for the topic. Posts for the month need to go live during the day according to UTC time.

    I also keep a list of topics on the blog here, and you should feel free to write about any past topics and post something on your blog. It’s great practice, and a good way to get started as a #SQLNewBlogger.

    Default Changes

    There are all sorts of defaults in SQL Server. The setup program presents you with a number of choices, but in most cases a default exists for setting because SQL Server needs something.

    I used to have a setup script that I would run for every new install. In a few jobs, back when we used to have physical servers, a hardware person or sysadmin would install Windows and SQL Server onto a new computer and then send me the name for customization. My script, which was really a series of SQLCMD calls in a batch file that in turn called various other scripts, was designed to add an administrative database, create some jobs to track the system, setup backups, and more.

    The process really did part of what Policy Based Management can do, but was simpler and tailored to ensure that all of our SQL Servers worked in a similar manner. We could override settings, but this quick script gave us a starting point that all DBAs understood. We even ran this on development machines for instances we didn’t manage as it allowed us to troubleshoot other issues, it only took a few minutes, and it removed some of the management headaches from the developers’ minds.

    However, there is one thing I’ve almost always changed on my instances. I try to do it during setup, but at times I need to do it later. That setting is the default location for files. I do this as I want to usually have data files, log files, and backup files separate from each other.

    Even if I don’t have different drives, but setting up separate locations here now, I can easily move the files later and make one change here for the defaults and I know I’ll have things separate.

    I’m not running a new install this week, but I’ll show you how to change it on an instance that’s installed. First, right click the instance in Object Explorer and click Properties.

    2015-07-06 14_17_48-SQLQuery2.sql - not connected_ - Microsoft SQL Server Management Studio

    Next, go to the Database Settings section.

    2015-07-06 14_24_07-Server Properties - JOLLYGREENGIANT_SQL2012

    At the bottom here you see locations for data, log, and backup. In this case, on my laptop, I only have two drives, so I can’t achieve great separation.

    However in any production system, I’d have the data and logs separated to different physical drives, or at least different LUNs. Backups might go with logs, but they’d ideally be separated to another location.

  • Database Version Control Workshops

    We’ve got more Workshops coming this fall from Redgate Software to help you get the most out of our tools and improve your database development processes. The list of our training schedule is out, and I’ll be at a few of these.

    The schedule does changes as we can add more dates and partners, so keep checking it, or let us know if you’d like to have a class near you.

    Baton Rouge, LA – Jul 31, 2015

    I’ll be traveling to Baton Rouge, LA for our July 31 class with Ike Ellis on Database Source Control. Ike’s teaching, and I’m there to help and manage the labs that we’ve put together for the class.

    This workshop is the day before SQL Saturday #423 in Baton Rouge, so if you’re attending that, consider coming a day early and getting some hands on knowledge of database and version control on Friday. It’s only $100 (a limited time sale), so register today.

    New York City – August 20, 2015

    We’ve also scheduled to give a Database Source Control workshop in New York City, in Manhattan on August 20, 2015. Once again, Ike and I will run the workshop, giving you a chance to learn how you can better manage the code in your databases with a VCS.

    Come Learn

    I hope to see some of you at one of these workshops, or one later in the year. We’ve really worked hard to pack a lot of information into a long day, helping you actually use the tools and technologies to control your database code. We’ll talk about how a VCS integrates with your code, what to include, how to handle branching and merging, and more.

    We’ve put the price of these workshops on sale for the remainder of the year, so take advantage and register today.

  • Azure SQL Database – Adding a Login

    I’m writing this post as a way to help motivate the #SQLNewBloggers out there. Read the bottom for a few notes on structuring a post.

    I am trying to slowly do a little work in the Azure SQL Database world, building some skills that I can use, and I can teach you a bit more about. One of the things I needed to do lately was add a new login to my system, which isn’t intuitive in the portal.

    I did go to the Managing Databases and Logins in Azure SQL Database to get some help, but it wasn’t completely clear how to do this.

    I setup a database recently for a test project, but I wanted a different user for this database than others I have on this particular Azure SQL Server. I am using the new (2015) portal, and there didn’t seem to be a good way to add a new login, so I connected to do this in SQL. The reference below showed me the standard “CREATE LOGIN” statement I’d use with an on-premise instance, so I tried to run that.

    CREATE LOGIN sscqa WITH PASSWORD = mypwd; GO

    I got this:

    2015-06-23 14_45_19-SQLQuery3.sql - mhknbn2kdz.database.windows.net,1433.AdventureWorks2012 (sjones

    OK, no issues. I changed the connection to the use the master database. I used the options after clicking the “Change Connection” button and put in master.

    2015-06-23 14_47_06-SQLQuery3.sql - mhknbn2kdz.database.windows.net,1433.AdventureWorks2012 (sjones

    I connected and ran the code to get this:

    2015-06-23 14_47_30-SQLQuery3.sql - mhknbn2kdz.database.windows.net,1433.master (sjones (63))_ - Mic

    I needed to not only change my database user connection, but also use the admin user I have. In this case, it’s the jt user I inherited from Jamie Thomson as part of the Hosted AdventureWorks project he started.

    Once I did that, the login was created without an issue. Now I need to link it to a user.

    SQLNewBlogger

    This took about 5 minutes to write, and 5 minute to reshoot some screen shots. The rest was really the 20 minutes I spent mucking around, researching, and reading how to do this. Hopefully this is quicker for me, and you, if you have this need.

    Anyone could write this, and I’d encourage you to write your own stories about working with an Azure database if you want to learn about the topic.

    Reference

  • Get a comma separated list

    I’m writing this post as a way to help motivate the #SQLNewBloggers out there. Read the bottom for a few notes on structuring a post.

    I was working on a test of sorts and wanted to return multiple values as the output, but as a single variable. In other words, I couldn’t return a result set, I needed to return a string.

    I knew this was easy, and decided this would make a nice simple blog. Here we go.

    Let’s start with a simple table. Here’s one that has a few rows in it.

    CREATE TABLE MyTest
    ( id int);
    GO
    INSERT mytest values (1), (2), (3);
    go

    I want to return the values “1, 2, 3” as a string, in any order. Here’s how it works:

    DECLARE @i VARCHAR(MAX);
    SELECT @i = COALESCE(@i + ‘, ‘,”) + CAST( Id AS VARCHAR)
    FROM MyTest;
    SELECT @i;

    The COALESCE is important as the first time this runs, we have a NULL for the variable. In this case, we return an empty string. This is almost like the inverse of the operation that ends recursion. We add in the first row, and we end up with ‘1’ as the string.

    Note: it could be 2 or 3 in the string as I don’t have an ORDER BY. DO NOT depend on the order of insertion in the table. If you care about ordering, always include an ORDER BY.

    The next execution has a blank string (NOT NULL), so that is returned. In this case, we have ‘1’ + ‘, ‘ for the first part. The second part adds in the next row.

    This continues, and I get a nice set of output.

    2015-06-10 17_20_39-SQLQuery4.sql - ARISTOTLE.sandbox (ARISTOTLE_Steve (62))_ - Microsoft SQL Server

    SQLNewBlogger

    This one was short. It took me almost as much time to write the code and find a reference as it did to write the post. Five minutes.

    References

    This is basic T-SQL, but here’s another look at this.