SQL Server Blog Archives, by Peter Whyte (Database Administrator).
Includes: Database Admin, Management Studio (SSMS), Windows Admin, PowerShell, WSL, MSSQL Backups, Restores & more…
The release of SQL Server 2019 happened during the first day of Microsoft Ignite (4th November). Up until now, I’ve been using the release candidates for tests… so it’s good to have it all officialised!
Here’s some notes during my first official SQL Server 2019
installation, using the wizard on a Windows Server host;
It’s nice to note down the full feature list for reference…
MAXDOP
This is one of 2 changes I love. More visibility on an
important configuration option, CPU parallelism for queries that run on your
SQL Server instance.
Max Memory
The out of the box max memory configuration commonly brings
chaos… RAM contention is an issue (other things running on the server) and it
can be triggering your monitoring alerts (constant 90%+ RAM usage). So it’s
amazing to have more visibility on this too!
Running scheduled tasks in SQL Server Express has to be done differently due to the limitations of the Edition. As well as having a maximum database size of 10GB and 1GB consumable RAM, Express does include the SQL Server Agent. So, If you need a task to run on a time-based schedule, you’ll have to look outside of SQL Server.
We can schedule our jobs using the Windows Task Scheduler, which will run a PowerShell or DOS script that calls the sqlcmd.
The .bat script for this post will execute Ola Hallengren’s Index Maintenance, which I’ll set to run weekly on the MS SQL Express instance. Ola’s index maintenance stored procedure is assumed to be on the SQL Server for this task.
If you already have SSMS installed, you can find the directory of sqlcmd with the help of this Microsoft Documentation Page – File Locations for Default and Named Instances of SQL Server (search ‘sqlcmd’ after selecting your SQL Version when on the page).
My sqlcmd.exe and bcp.exe clients are in: -> C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Bin
The only thing we need for now is to have it installed on the same machine we’re running the script from.
I wrote another blog post on sqlcmd if you would like more information and examples of executing SQL commands with sqlcmd.exe.
Below you’ll see -Q being used and everything within
the command is within quotes. The -o parameter is also being used to
forward output messages to a text file.
We have our script file ready, and we know where SQLCMD is. It is time for us to schedule the script as a Task in Windows Task Scheduler. This should be a more familiar area for Windows Admins.
1. Open Task Scheduler.
2. Right-click and select Create New Task as shown below.
3. Within the General tab of the prompted window, change the following: –> Name / Description, more information the better for visibility. –> The user account that the task runs as to a new local service account I created for this. Remember, the chosen account requires Logon As Batch and MSSQL permissions (sysadmin for quickness here). –> Run whether the user is logged on or not.
4. Click the Triggers tab and hit New to set the schedule.
The schedule below is set to run every Sunday at 02:00. Set this up to your desired days/times and click OK once done.
5. Click on the Actions tab and click New to add our script as the action.
This is where we add our .bat script file location. Browse to the folder you saved your script and click OK when after selecting the file.
6. Click on the Settings tab and review options. The “Stop task if it runs for longer than” setting is of particular note as the default is 3 days.
7. Now that we have reviewed all tabs within the Create Task window, click OK to create the task.
You will be prompted to enter a username and password if the task is set to use an AD service account.
This Scheduled Task has been created and is awaiting its next execution, which is early Sunday morning.
Your script might not be running for long enough to verify this way. This maintenance solution script run time can vary as it depends on the level of index fragmentation on all SQL Server databases. It’s been run 64 times on my test databases and now only taking a second to complete.
We can also verify the SQLCMD script ran successfully by checking the Scheduled Task History and log output.
The sqlcmd utility allows Transact SQL to be run at the command prompt using ODBC to execute SQL batches for MS SQL Server.
This tool comes with SQL Management Studio, so if you have SSMS installed you can locate the sqlcmd exe file and call it via a Windows command terminal.
Version notes for sqlcmd: # Sqlcmd version 13.1 or higher is required for Always Encrypted (-g) and AAD Auth (-G) # Microsoft ODBC Driver for SQL Server 17.6.1 or higher required for Azure Active Directory (AAD) Authentication and configure a Kerberos environment for macOS/Linux ODBC connectivity
The version information above may drift out of date – always look at Microsoft Docs for more up-to-date information.
In this blog post, I’m showing a demo of finding sqlcmd.exe on my local computer and then showing simple sqlcmd query examples.
SQLCMD and BCP are installed on your computer when you install SQL Server Management Studio. SSMS is the GUI application that most SQL Servers users use to query MSSQL, so there’s a good chance you might already have sqlcmd.
If you do already have SSMS installed, Microsoft has a documentation page for helping us find SQL file locations for each of the many apps. Search ‘sqlcmd’ after selecting your SQL version when on the page.
My sqlcmd.exe & bcp.exe files are in c:\program files\microsoft sql server\client sdk\odbc\130\tools\binn\
For quickness, we can type cmd.exe in the top address bar:
This will bring up a command prompt and the terminal will be as per the directory we executed this from in Windows Explorer. We don’t actually need CMD to be scoped into the same directory as sqlcmd.exe to use it though.
This command prompt is running as the User account I’m logged into Windows with. You can change which user runs CMD (shift-click app) to use another Windows account, or use -U / -P for SQL Server Authentication.
I’m running sqlcmd on the same machine as a default SQL Server instance, and my local user account has sysadmin permissions. That means I’m able to open sqlcmd without any parameters as shown below;
If connecting to a named instance of SQL Server we need to add the following parameter: -S <server-name\instance-name>
Now that we are in sqlcmd in the terminal window we can run any SQL statement our user permissions allow. This demo user is a member of the sysadmin role so can perform any action on the SQL Server instance.
I’m going to run a few commands to check basic SQL host and server information: – @@SERVERNAME : Get SQL Server Hostname – @@SERVICENAME : Get SQL Server Instance Name – @@VERSION : Get SQL Server Product Version
Next, I will verify which user I’m connected to SQL Server as using SUSER_NAME().
Good, I know which server I’m on and who I am.
I now want to find out which databases are on this SQL Server. We can query sys.databases to list all databases on the SQL Instance.
There are 4x system databases on this SQL host and 1x User Database, Adventureworks. The famous Test database provided by Microsoft.
Lets now look at what tables are inside the Adventureworks database by querying sys.tables.
The above SQL commands we have run through here give us a view of what we take for granted in a GUI application. Now that we know the SQL Server host we are on and the databases/tables we can start on some ETL or admin work!
This is a post on how-to on changing the schema of a table in SQL Server, moving it from one schema to another using the ALTER SCHEMA [tableName] TRANSFER SQL statement.
During my time as a SQL Database Administrator, assisting with a task like this is a rare event. If you are more of a Database Developer, then you’ll probably need to do this more often.
If you are making this change to a Production SQL Server, you should ensure all SQL queries that use tables included in this change are updated with the new schema name.
The SQL below will create a new schema within the [Adventureworks] database which will be the new schema we are migrating the test table to.
-- Create a new schema in MS SQL Server
CREATE SCHEMA [finance] AUTHORIZATION dbo;
Setting dbo as the owner will be fine in most cases. If you have experienced SQL Database Developers around you, they might ask for this schema to be created with an alternative schema owner.
We have our new schema created, now let’s move on to the next part for migrating a table to this schema.
-- Change schema of table in SQL Server
ALTER SCHEMA finance TRANSFER [dbo].[DimCurrency]
The schema name in the left-most part of the SQL command is the schema we are transferring to (finance).
Now that I’ve created the new Schema, I’ll refresh the tables within the SSMS Object Explorer. I see the table has changed from dbo.DimCurrency to finance.DimCurrency.
(Note: the red squigglies under the table name in the SSMS query window mean that the query hasn’t been refreshed yet. Hit CTRL + SHIFT + R to update SSMS IntelliSense)
Remember to now update all queries, and change the schema name when querying the transferred table.
I hope this guide has helped you amend the schema of a table in SQL today. If you like this post and want more random tips from an MS SQL DBA, check out my latest posts on my DBA Blog Homepage.
This is a post on installing SQL Server via command; I have a Windows Server 2016 Core running on a local Hyper-V test environment, and I’m installing SQL Server 2019, then uninstalling it.
As this is on my test environment, I’m installing the most
recent SQL Server Edition available, which is SQL Server 2019 CTP 2.4 at the
time of writing.
SQL Server 2019 can be downloaded here – Open it up and select to download the media.
ISO’s are good for local virtual environments (using HyperV
here).
6Mbps…
We’re now ready to get that ISO mounted.
Mount the ISO on a Hyper-V Virtual Machine
1. Click Media > Insert Disk…
2. Select the ISO file.
Install SQL Server via Command
1. First, lets have a look at our drives using the Get-PSDrive
cmdlet.
2. Install using your preferred parameters. I’m adding the Domain Admins group as Sysadmins for quickness.
The above shows the result of the installation was successful, and other things like where the Configuration INI file is (used for Unattended Installs).
4. Next, we should check the SQL Server services, using Get-Service with “SQL” wrapped in wildcards.
Uninstall SQL Server via Command
Uninstalling is as simple as the install, funnily enough..
Once it’s done, we can check the summary file status and services to quickly verify the uninstallation has not been unsuccessful.
If you try to change the order of steps in an Agent Job within SQL Server Management Studio (SSMS), you may encounter the error “Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index (mscorlib)“.
This error can occur even if you are using the latest version of SSMS. It’s unclear what the root cause of this issue is, but I have been able to resolve it for myself and some others by updating to the latest version of SSMS.
Resolve “Index was out of range” SSMS Issue
As mentioned above the resolution is to update to the latest SSMS version. For demo purposes, I’m going to show the actual error I was encountering here.
I create a new SQL Agent Job with 3 job steps:
When I try move the job step 3 (import_second_table) run order from 3 to 2, the Index was out of range error prompts:
Searching for this error online often led me to a lot of unrelated information about .NET development. If updating SQL Server Management Studio (SSMS) has not resolved this issue for you, feel free to leave a comment and we can troubleshoot here.