A blog about SQL Server, Books, Movies and life in general
Thursday, December 29, 2016
The 15 most popular posts in 2016
Another year is almost over. As the last post of this year I decided to share with you the 15 most popular posts in 2016. I just used Google Analytics to grab this info, I excluded the home page from the top 15. Four of these posts were written in 2006 and one was written in 2005
Here they are in order of popularity
1 Some cool SQL Server announcements SQL Graph, Adaptive Query Plan, CTP1 of SQL vNext, SQL Injection detection
This is my recap of the chalkboard session with the SQL Server team at the SQL Server PASS summit in Seattle.
2 Five Ways To Return Values From Stored Procedures
A very old post that shows you five ways to return values from a stored proc
3 SQL Server 2016 SP1 released, SQL Server vNext available for download
Posted this while the connect event was going on
4 Installing SQL Server on Linux, connecting and running queries from Linux
A quick walk through on how to install SQL Server on Ubuntu, I also included a video in the post
5 Are your foreign keys indexed? If not, you might have problems
A post showing you that indexing foreign keys might be beneficial if you do deletes
6 Convert Millisecond To "hh:mm:ss" Format
A very old post showing you how to convert from milliseconds to "hh:mm:ss" format
7 What's new in SQL Server 2016: CREATE OR ALTER
A post showing how the new CREATE OR ALTER syntax works
8 Why you need additional privileges to truncate tables compared to delete statements
The reason why you need these privileges is shown
9 What's new in SQL Server 2016: Truncate partitions
This post shows how truncate partitions works
10 What we learned from the reddit SQL Server AMA thread on /r/sqlserver
A recap of the AMA SQL Server thread on reddit
11 Playing around with the new stuff in SQL Server vNext and SQL Server 2016 SP1
Just a post showing some of the new stuff released in SP 1 of SQL Server 2016 and vNext
12 ISO-11179 Naming Conventions
A very old post linking to the ISO-11179 Naming Conventions document
13 Query Analyzer Trick
A very old post explaining how you can drag the columns into the query window from object explorer. I think by now everyone knows this one right?
14 What was the first computer you bought, what were some of your favorite games?
Mine was the Commodore 128, what about you?
15 Chaos Isolation Level In SQL Server
This is linked from dba.stackexchange.com, it is kind of interesting because it was a silly post I made
That is all... see you in 2017...
Tuesday, December 27, 2016
Didn't go to PASS but want to watch the sessions?
You didn't go to the PASS summit this year, but you would still want to watch the sessions? There is a way now, you can buy the USB stick with all the sessions, you can also download the sessions you are interested in. The passboutique site has the details, They also have a sale going on at the moment.
So if you got a bunch of money over the holidays, this would be a great investment.....
Saturday, December 17, 2016
SQL Server vNext version Community Technology Preview 1.1 available
A nice holiday present for you all has just arrived: SQL Server next version Community Technology Preview 1.1
Here is what is new in terms of the SQL Engine
- Language and performance enhancements to natively compiled T-SQL modules, including support for OPENJSON, FOR JSON, JSON built ins as well as memory-optimized tables support for computed columns.
- Improved the performance of updates to non-clustered columnstore indexes in the case when the row is in the delta store.
- Batch mode queries now support “memory grant feedback loops,” which learn from memory used during query execution and adjusts on subsequent query executions; this can allow more queries to run on systems that are otherwise blocking on memory.
- New T-SQL language features:
- Introducing three new string functions: TRIM, CONCAT_WS, and TRANSLATE
- BULK IMPORT supports CSV format and Azure Blob Storage as file source
- STRING_AGG supports WITHIN GROUP (ORDER BY)s
So finally we can write the following instead of doing LTRIM and RTRIM
TRANSLATE
SELECT TRIM( ' NoSPaces ') AS Result;
That will return just the value NoSpaces
You can also specify what characters to trim
This acts like a bunch of replace statements, instead of REPLACE(REPLACE(REPLACE(REPLACE(SomeVal,'[','('),']',,')'),'{','('),'}',,')') you can do the following which is much cleaner
SELECT TRANSLATE('2*[3+4]/{7-2}', '[]{}', '()()');
Running that will return 2*(3+4)/(7-2)
Everything that is new in version 1.1 can be found here https://msdn.microsoft.com/en-us/library/mt788653.aspx
What are you waiting for??..... pick where you want to install the next version.....
- Install on Red Hat Enterprise Linux
- Install on Ubuntu Linux
- Pull and run a Docker container on Linux, Windows, or macOS
- Download the preview for Windows
- Create a SQL Server on Linux virtual machine in Azure
See...you won't be bored this weekend.....
Friday, December 16, 2016
Interesting SQL related links for the week of Dec 16, 2016
Here are some interesting articles I read and tweeted about this past week, I think you will like these as well. If you are bored this weekend, some of these might be good for you to read
SQL Server + Python — What’s new
We recently announced SQL Server v.Next CTP1 on Linux and Windows, which brings the power of SQL Server to both Windows and — for the first time ever — Linux. You can now connect your applications to SQL Server running on Linux, Windows and macOS (with Docker).
For our Python developers, we have a few updates that will improve Python connectivity with SQL Server. Pyodbc is now:
- Ability to insert into, update and filter by values stored in encrypted columns from a Query Editor window.
- The new online encryption algorithm, exposed in the Set-SqlColumnEncryption PowerShell cmdlet, which makes tables available for both reads and writes during the initial encryption and column encryption key rotation.
This article addresses the first of the two enhancements.
Friday, December 09, 2016
Connecting to SQL Server when your default database is not available
One of our database on the development went in suspect mode today. This database was the default for a bunch of logins. These people could not login now. Someone needed to use a different database but he couldn’t login because the database that was in suspect mode was the default database for the login he was using.
I told this person to click on the Options button in the connection dialog and specify another database. I guess there was an misunderstanding because this person couldn’t get it to work. This means it is time for a blog post.
Let's take a look how this all works
Here is a script that will create 2 databases
CREATE DATABASE Good GO CREATE DATABASE OopsBad GO
Now create a new login named TestLogin with a password of Test. We are also adding the login we just created to the OopsBad database and we will make the login part of the db_owner role
USE [master] GO CREATE LOGIN [TestLogin] WITH PASSWORD=N'Test', DEFAULT_DATABASE=[OopsBad] USE [OopsBad] GO CREATE USER [TestLogin] FOR LOGIN [TestLogin] GO USE [OopsBad] GO ALTER ROLE [db_owner] ADD MEMBER [TestLogin] GO
Add the login we just created to the Good database as well and make the login part of the db_owner role
USE [Good] GO CREATE USER [TestLogin] FOR LOGIN [TestLogin] GO USE [Good] GO ALTER ROLE [db_owner] ADD MEMBER [TestLogin] GO
Make sure that you can login with the TestLogin account
Now that you know that you can login with the TestLogin account, use another account and put the OopsBad database in offline mode
Take the database online
ALTER DATABASE OopsBad SET OFFLINE
Now if you try to login with the TestLogin account, you will see the following error
Here is what you need to do, on the connect to server window, click on the Options button
One the next screen, click on the Connection Properties tab, do not click on Browse server... from the drop down. If you click that you might have to wait for a long time
Instead just type in the database name, in our case the database name is Good
Click on Connect and you should be in.... That's it, pretty simple, you just have to know where to find it.
Interesting SQL related links for the week of Dec 09, 2016
Here are some interesting articles I read and tweeted about this past week, I think you will like these as well. If you are bored this weekend, some of these might be good for you to read
Transaction Commit latency acceleration using Storage Class Memory in Windows Server 2016/SQL Server 2016 SP1
This enhancement is especially valuable for workloads which require high frequency, low latency update transactions. These app patterns are common in the finance/trading industry as well as online betting and some process control applications.
As many of you know, I publish a bi-weekly newsletter to more than 13,500 subscribers that contains an editorial on a SQL Server topic, a demo video, and a book review of my most recently completed book. We’re making all the 2015 demo videos available so everyone can watch them – 25 videos in all, mostly in WMV format. I did the same thing the last few years for the 2014 videos, 2013 videos, 2012 videos, and 2011 videos.
Query completion is a prerequisite for the availability of an actual query plan, but with LQS (Live Query Statistics), you can already get information about in-flight query executions (see this blog post), which is especially useful for long running queries, and queries that run indefinitely and never finish.
To look at in-flight query executions, the query execution statistics profile infrastructure must be enabled on demand.
Over the years, I’ve had many inquiries about the slides, T-SQL scripts, and additional troubleshooting information. I’m happy to report that I’ve finally collated all of the associated content from that presentation!
After installation you can run the Stored Procedure as follows:
EXEC sp_WhatsupQueryStore @dbname, @timewindow, @topqueries, @return_store_config, @return_forced_plans, @return_top_duration, @return_top_cpu, @return_top_log_read, @return_top_log_write, @return_top_phys_read
The columnstore technology is available in two flavors; clustered columnstore index (CCI) for DataMart analytics workloads and nonclustered columnstore index (NCCI) to run analytics queries on operational (i.e. OLTP) workload. Please refer to NCCI vs CCI for the differences between these two flavors of columnstore indexes. The columnstore index can speed up the performance of analytics queries up to 100x while significantly reducing the storage footprint. The data compression achieved depends on the schema and the data, but we see around 10x data compression on average when compared to rowstore with no compression. This blog will focus on Analytic workloads using CCI but cover NCCI in a future blog.
Here are some freshly written scripts for a classic DBA question: what’s going on in my stats?
With the release of SQL Server v.Next public preview on Linux and Windows, the ability to connect to SQL Server on Linux, Windows, Docker or macOS (via Docker) makes cross-platform support for all connectors, including the JDBC driver, even more important. To enable Java developers to use the newest SQL Server features, we have been updating the JDBC driver with client-side support for new features, including Always Encrypted and Azure Active Directory Authentication.
Microsoft R Server 9.0, Microsoft's R distribution with added big-data, in-database, and integration capabilities, was released today and is now available for download to MSDN subscribers. This latest release is built on Microsoft R Open 3.3.2, and adds new machine-learning capabilities, new ways to integrate R into applications, and additional big-data support for Spark 2.0.
This release includes a brand new R package for machine learning: MicrosoftML. This package provides state-of-the-art, fast and scalable machine learning algorithms for common data science tasks including featurization, classification and regression. Some of the functions provided include:
This release includes a brand new R package for machine learning: MicrosoftML. This package provides state-of-the-art, fast and scalable machine learning algorithms for common data science tasks including featurization, classification and regression. Some of the functions provided include:
- Fast linear and logistic model functions based on the Stochastic Dual Coordinate Ascent method;
- Fast Forests, a random forest and quantile regression forest implementation based on FastRank, an efficient implementation of the MART gradient boosting algorithm;
- A neural network algorithm with support for custom, multilayer network topologies and GPU acceleration;
- One-class anomaly detection based on support vector machines.
This release will bring support for Elastics Pools and Azure AD Authentication among other new features. We are also working on handling monitoring of larger number of databases. We are expecting to improve the scale by the time we RTM. Here are some numbers to give you an idea for this public preview:
With the introduction of the Temporal feature in SQL 2016 and Azure SQL Database, there is an ability to time travel through the state of data as it was at any given point of time. Alongside In-Memory OLTP, Temporal on memory optimized tables allows you to harness the speed of In-Memory OLTP, and gives you the same ability to track history and audit every change made to a record. Temporal added to memory optimized tables also allows you to maintain a “smaller” memory optimized tables and thereby a smaller memory footprint by deleting data that isn’t “hot” anymore from the current memory optimized table, which in turn moves it to the history table without having an external archival process to do that.
When memory optimized and temporal tables are combined, an internal memory optimized table is created in addition to the history table, as depicted in the diagram below. Data is flushed asynchronously from the internal in-memory History table to the disk based history table. The flush interval isn’t currently configurable. Data is flushed when the internal table reaches 8% of the memory consumed by the current table, OR you can flush it manually by executing the procedure sys.sp_xtp_flush_temporal_history. The internal memory optimized table is created with the same column definitions as the current in-memory table, but with a single index.
As requested by the community to complete the per-operator information, starting with SQL Server 2016 SP1 we are now exposing memory grant per grant iterator (such as Sorts and Hash Matches). These give you added insight into memory grants, and how overall memory usage is driven throughout execution.
Friday, December 02, 2016
Interesting SQL related links for the week of Dec 02, 2016
Here are some interesting articles I read and tweeted about this past week, I think you will like these as well. If you are bored this weekend, some of these might be good for you to read
Microsoft is putting reprogrammable chips into millions of servers
Some giants like Microsoft are also using alternative silicon to execute their neural networks after training. And even though it’s crazily expensive to custom-build chips, Google has gone so far as to design its own processor for executing neural nets, the tensor processing unit.
Lock Pages in Memory and Instant File Initialization privileges are couple of configuration settings which every DBA, SQL Server consultant or enthusiast have it in their checklist to ensure they see a predictable performance for their SQL Server instance. While Lock Pages in Memory privilege information is logged in SQL Error log, Instant File initialization information was first introduced in SQL Errorlog starting SQL Server 2016 RTM and later added to SQL Server 2014 with SP2.
When you are managing, administering or monitoring large deployment of SQL Servers, it is still cumbersome to programmatically query SQL Error log to check if these permissions are enabled for the SQL Server service account. With SQL Server 2016 SP1, we have added new columns in the DMV which now makes it easy to develop scripts to programmatically query and report whether Lock Pages in Memory and instant file initialization privileges are enabled on a given instance of SQL Server.
SQL Server uses memory to store in-transit rows for hash join and sort operations. When a query execution plan is compiled for a statement, SQL Server estimates both the minimum required memory needed for execution and the ideal memory grant size needed to have all rows in memory. This memory grant size is based on the estimated number of rows for the operator and the associated average row size. If the cardinality estimates are inaccurate, performance can suffer:
- For cardinality under-estimates, the memory grant can end up being too small and the rows then spill to disk, causing significant performance degradation compared to a fully memory-resident equivalent.
- For cardinality over-estimates, the memory grant can be too large and the memory goes to waste. Concurrency can be impacted because the query may wait in a queue until enough memory becomes available, even though the query only ends up using a small portion of the granted memory.
We recently announced SQL Server v.Next CTP1 on Linux and Windows, which brings the power of SQL Server to both Windows — and for the first time ever — Linux. Developers can now create applications with SQL Server on Linux, Windows, Docker, or macOS (via Docker) and then deploy to Linux, Windows, or Docker, on-premises or in the cloud.
As part of this announcement, we have released new SQL tools and also updated existing SQL tools. Developers can use these tools to connect to and work with SQL running anywhere, including SQL Server on Linux, Windows or Docker.
Sunday, November 27, 2016
Changing the SQL Server port on Linux
Today, I decided to see how I can change the port SQL Server is listening on
To see what port SQL Server is listening on, you can use the netstat command, here is what the output looks like when I run netstat -tulpn | grep sqlservr
I used grep to limit the output to lines that contain sqlservr only
Next, to change the port that SQL Server is listening on to 3000, I ran the following command
I restarted SQL Server bu running the command systemctl restart mssql-server
Then I ran netstat -tulpn | grep sqlservr again and as you can see the output now has 3000 as the port number
Now I tried using sqlcmd, I ran the following sqlcmd -S localhost -U SA
I got an timeout error
Finally I ran the sqlcmd command again but I added port number 3000, like this
sqlcmd -S localhost,3000 -U SA
As you can see, this worked and I could connect to SQL Server listening on port 3000 when supplying that to the sqlcmd command
How do we reset SQL Server back to the default port?
To reset SQL Server back to use the default port, run the command below
/opt/mssql/bin/mssql-conf unset tcpport
Here is a screenshot of running netstat, resetting the port, restarting SQL Server and running netstat again
You want to learn a little more?
We used tulpn as options in our netstat command. So what do all these options do?
[-t]
tcp
[-u]
udp
[-l]
Show only listening sockets
[-p]
Show the PID (process identifier) and name of the program to which each socket belongs.
[-n]
Show numerical addresses instead of trying to determine symbolic host, port or user names.
On a Linux system to get help for a command, use man, this will bring up the user manual for that command
So if you run man netstat, you will see the following (I only pasted 1 page here, there are more)
man netstat
--------------------------
NETSTAT(8) Linux Programmer's Manual NETSTAT(8)
NAME
netstat - Print network connections, routing tables, interface statis‐
tics, masquerade connections, and multicast memberships
SYNOPSIS
netstat [address_family_options] [--tcp|-t] [--udp|-u] [--raw|-w]
[--listening|-l] [--all|-a] [--numeric|-n] [--numeric-hosts]
[--numeric-ports] [--numeric-users] [--symbolic|-N]
[--extend|-e[--extend|-e]] [--timers|-o] [--program|-p] [--verbose|-v]
[--continuous|-c]
netstat {--route|-r} [address_family_options]
[--extend|-e[--extend|-e]] [--verbose|-v] [--numeric|-n]
[--numeric-hosts] [--numeric-ports] [--numeric-users] [--continuous|-c]
netstat {--interfaces|-i} [--all|-a] [--extend|-e[--extend|-e]] [--ver‐
bose|-v] [--program|-p] [--numeric|-n] [--numeric-hosts] [--numeric-
ports] [--numeric-users] [--continuous|-c]
netstat {--groups|-g} [--numeric|-n] [--numeric-hosts]
[--numeric-ports] [--numeric-users] [--continuous|-c]
Manual page netstat(8) line 1/348 15% (press h for help or q to quit)
To see all my SQL Server on Linux posts, click here: SQL Server on Linux
To see all my SQL Server on Linux posts, click here: SQL Server on Linux
Labels:
Howto,
Linux,
SQL Server on Linux,
SQL Server vNext,
ubuntu
Saturday, November 26, 2016
You snooze you lose... I lost....
I was working on a post about the new columns sql_memory_model, sql_memory_model_desc columns in DMV sys.dm_os_sys_info. These columns allow DBAs to programmatically identify if Lock Pages in Memory (LPIM) privilege is in effect at the service startup time.
I was setting it up, had one instance running with pages locked in memory and the other one without. Took some screenshots, then I noticed the MSSQL Tiger Team published the post
Developers choice: Programmatically identify LPIM and IFI privileges in SQL Server
So really there was no need for my post anymore. But what do I do with my beautiful screenshots?
Well not someone who likes to waste stuff, I decided to write just a couple of sentences and put the screenshots in this post and then link to the post by the MSSQL Tiger Team
Here is the query I ran to see instant file initialization is enabled, I ran this against two instances, one a SQL Server 2016 Service Pack instance, the other a SQL Server vNext instance
SELECT instant_file_initialization_enabled,* FROM sys.dm_server_services
You can see the output in the pic below, the vNext instance does not have instant file initialization enabled, the 2016 SP1 instance does have instant file initialization enabled
To see if Lock Pages in Memory is in use, you can run the following query
SELECT sql_memory_model,sql_memory_model_desc FROM sys.dm_os_sys_info
You can see the output in the pic below, the vNext instance does not use Lock Pages in Memory, the 2016 SP1 instance does use Lock Pages in Memory
The sys.dm_os_sys_info DMV also has a new column that SQL Server 2016 SP1 does not have.. process_physical_affinity
To get all the details about this stuff in the pictures go read the post
Developers choice: Programmatically identify LPIM and IFI privileges in SQL Server published by the MSSQL Tiger Team
What to get your friendly DBA this holiday season
You have a DBA that you want to get something for this holiday season, but you have a hard time figuring out what to get him?
Here are 3 choices that will make a good present
The first two have to do with the availability of SQL Server on Linux. This is in preview right now, but it will be here before you know it.
I can already see the job postings a year fron now...
Install SQL Server on Windows as well as Linux
Manage Linux and Windows servers
And of course... 5 years experience installing SQL Server vNext on Linux :-)
You go to the interview, then someone asks what does touch File1 do?
You get my point, SQL Server DBAs will need to learn Linux and they will need to learn this fast.
If your DBA likes to read, as a first step, I would recommend the following two books
Unix and Linux: Visual QuickStart Guide (4th Edition)
How Linux Works: What Every Superuser Should Know 2nd Edition
The Visual QuickStart book is for the total beginner, the How Linux Works book will go into more detail. Either book is perfect if the person has never touched a *nix system before.
Both books are nicely formatted, clear and concise, once the person is done with one of these books, the person can get himself/herself one of those 1000 page tomes
The next item is a book by an author whose previous work I have read and enjoyed
High Performance SQL Server The Go Faster Book by Benjamin Nevarez
I noticed that Benjamin Nevarez's latest book is available now on the Apress site for $9.99 in epub and pdf format, this price is valid through November 29, 2016
I didn't read this book yet, so here is what the website has to say about this book
What You Will Learn
Understand SQL Server's database engine and how it processes queries
Configure instances in support of high-throughput applications
Provide consistent response times to varying user numbers and query volumes
Design databases for high-throughput applications with focus on performance
Record performance baselines and monitor SQL Server instances against them
Troubleshot and fix performance problems
Here is a breakdown of the chapters
How SQL Server Works
Pages 1-19
Analyzing Wait Statistics
Pages 21-43
The Query Store
Pages 45-69
SQL Server Configuration
Pages 71-84
tempdb Troubleshooting and Configuration
Pages 85-99
SQL Server In-Memory Technologies
Pages 101-129
Performance Troubleshooting
Pages 131-154
Indexing
Pages 155-180
SQL Server Storage
Pages 181-196
There you have it...one of these will make a nice gift for your friendly DBA....
Here are 3 choices that will make a good present
The first two have to do with the availability of SQL Server on Linux. This is in preview right now, but it will be here before you know it.
I can already see the job postings a year fron now...
Install SQL Server on Windows as well as Linux
Manage Linux and Windows servers
And of course... 5 years experience installing SQL Server vNext on Linux :-)
You go to the interview, then someone asks what does touch File1 do?
You get my point, SQL Server DBAs will need to learn Linux and they will need to learn this fast.
If your DBA likes to read, as a first step, I would recommend the following two books
Unix and Linux: Visual QuickStart Guide (4th Edition)
How Linux Works: What Every Superuser Should Know 2nd Edition
The Visual QuickStart book is for the total beginner, the How Linux Works book will go into more detail. Either book is perfect if the person has never touched a *nix system before.
Both books are nicely formatted, clear and concise, once the person is done with one of these books, the person can get himself/herself one of those 1000 page tomes
The next item is a book by an author whose previous work I have read and enjoyed
High Performance SQL Server The Go Faster Book by Benjamin Nevarez
I noticed that Benjamin Nevarez's latest book is available now on the Apress site for $9.99 in epub and pdf format, this price is valid through November 29, 2016
I didn't read this book yet, so here is what the website has to say about this book
What You Will Learn
Understand SQL Server's database engine and how it processes queries
Configure instances in support of high-throughput applications
Provide consistent response times to varying user numbers and query volumes
Design databases for high-throughput applications with focus on performance
Record performance baselines and monitor SQL Server instances against them
Troubleshot and fix performance problems
Here is a breakdown of the chapters
How SQL Server Works
Pages 1-19
Analyzing Wait Statistics
Pages 21-43
The Query Store
Pages 45-69
SQL Server Configuration
Pages 71-84
tempdb Troubleshooting and Configuration
Pages 85-99
SQL Server In-Memory Technologies
Pages 101-129
Performance Troubleshooting
Pages 131-154
Indexing
Pages 155-180
SQL Server Storage
Pages 181-196
There you have it...one of these will make a nice gift for your friendly DBA....
BULK INSERT with a file containing linefeeds gotcha
I blogged about this before but someone ran into this issue again at work so I thought it made sense to revisit this.
I prefer to use BULK INSERT or bcp as much as possible, this is especially true if all I need to do is dump the file into a table. If there is a need to do complex stuff, I will use SQL Server Integration Services or Informatica. These days files are generated by all kinds of systems, these can be Windows, *nix, Mac, Raspberry Pis, IoT systems and many other systems.
If you get an error importing one of these files, your first course of action is to open one of these files in something like Notepad++ or Editplus. Notepad++ and Editplus have more functionality and are many times faster than notepad. The nice thing about either of these is that you can see control characters.
See how you can see the linefeeds here? You cannot do that in notepad
So let's say you get a file where the row terminator is a linefeed, how would you specify that as a row terminator in BULK INSERT?
You can try n which stands for newline
BULK INSERT SomeTable FROM 'D:\JunkdrawImportMe.txt' WITH (FIELDTERMINATOR = 't', FIRSTROW =2, ROWTERMINATOR = 'n')
Nope, that doesn't work, you get 0 rows inserted
You can try r which stands for carriage return
BULK INSERT SomeTable FROM 'D:\JunkdrawImportMe.txt' WITH (FIELDTERMINATOR = 't', FIRSTROW =2, ROWTERMINATOR = 'r')
Nope, that doesn't work either, you get 0 rows inserted
What about l for linefeed?
BULK INSERT SomeTable FROM 'D:\JunkdrawImportMe.txt' WITH (FIELDTERMINATOR = 't', FIRSTROW =2, ROWTERMINATOR = 'l')
You get an error
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 1 (SomeDate).
What about if you try a CHAR(10) which is a linefeed
BULK INSERT SomeTable FROM 'D:\JunkdrawImportMe.txt' WITH (FIELDTERMINATOR = 't', FIRSTROW =2, ROWTERMINATOR = CHAR(10) )
You get this error
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'CHAR'.
How about if you embed it by using Dynamic SQL
DECLARE @cmd varchar(1000) SET @cmd = 'BULK INSERT SomeTable FROM ''D:\JunkdrawImportMe.txt'' WITH ( FIELDTERMINATOR = ''t'', FIRSTROW =2, ROWTERMINATOR = '''+CHAR(10)+''')' EXEC(@cmd)
The dynamic SQL solution works, that was my original answer to this.
What about if we use the hex code for line feed 0x0a?
BULK INSERT SomeTable FROM 'D:\JunkdrawImportMe.txt' WITH (FIELDTERMINATOR = 't', FIRSTROW =2, ROWTERMINATOR = '0x0a') --CHAR(10) or ‘l’ does not work -- 0x0a is the hex code for linefeed CHAR(10)
Bingo, that works as well. I actually prefer this, who wants to muck around with Dynamic SQL if there is an easier way....
That's all, keep this in mind next time you get a file with a linefeed and you are struggling importing that file
Friday, November 25, 2016
Interesting SQL related links for the week of Nov 25, 2016
Here are some interesting articles I read and tweeted about this past week, I think you will like these as well. If you are bored this weekend, some of these might be good for you to read
Released: Public Preview for System Center Management Packs for SQL Server
We are getting ready to update the SQL Server Management Packs family. Please install and use this public preview and send us your feedback (sqlmpsfeedback@microsoft.com)! We appreciate the time and effort you spend on these previews which make the final product so much better.
Please download at:
Included in the download are:
- Microsoft System Center Management Pack for SQL Server 2008/2008 R2/2012/2014/2016 (6.7.10.0)
- Microsoft System Center Management Pack for SQL Server Replication 2008/2012/2014/2016 (6.7.10.0)
- Microsoft System Center Management Pack for SQL Server Analysis Services 2008/2012/2014/2016 (6.7.10.0)
- Microsoft System Center Management Pack for SQL Server Reporting Services 2008/2012/2014/2016 (6.7.10.0)
- Microsoft System Center Management Pack for SQL Server Dashboards (6.7.10.0)
The SQL Product team has identified the following known issues which the customers may encounter after installing SQL Server 2016 SP1. The following post will help the customers plan and prepare their environment for the following known issues while installing SQL Server 2016 SP1.
Ooo – clustered index scan – that’s bad, right?
Not so fast. Click on the plan to view it in PasteThePlan.com, and hover your mouse over the Clustered Index Scan:
In the latest generation of SQL Server Management Studio, we moved to the Visual Studio 2015 Isolated Shell. While this provides SSMS a modern IDE foundation for many functional areas, it also had some consequences. Specifically, the integration with source control systems in SSMS no longer works the way it did in SSMS 2014 and prior. Previously, one could install the Visual Studio MSSCCI provider and then integrate with various source control systems. Visual Studio 2015 does not support MSSCCI so that is no longer an option to use in SSMS.
Of course, the good news is that Visual Studio 2015 includes TFS and Git source control integration. With the move to VS 2015 Isolated Shell, SSMS should be able to use these packages as well, right? The answer is…yes…but! The issue for SSMS is that the TFS source control integration package VS provides also includes the entire suite of TFS integration features. If we include this package by default, SSMS will have Team Explorer in its entirety which includes things such as work item tracking, builds, etc. This doesn’t fit in the overall experience SSMS is designed for, so we aren’t going to include this package as part of SSMS. The full TFS integrated experience is included as part of SQL Server Data Tools which is designed for a more developer-centric set of scenarios.
That said, if source code integration is an important aspect of how you use SSMS, you can enable the Visual Studio packages manually.
Dear SQL DBA,Whenever I create a complex view, the first time I use it in a query, the query is slow. The same thing happens after I alter the view. What is the reason behind this?
This is a great question — because when you ask this question, you’re about to discover a lot of interesting, useful things about how SQL Server runs queries.
There are two ways that SQL Server can use memory to make a query faster the second time you run it. Let’s talk about how the magic happens.
Sunday, November 20, 2016
Installing SQL Server on Linux, connecting and running queries from Linux
I decided to play around with installing SQL Server vNext on Linux today. I decided to pick the Ubuntu distribution. I created a VM in VirtualBox and then installed Ubuntu 16.04. After that was done, it was time to install SQL Server.
All this stuff on this page runs on Linux, there are no windows components, if you have just a Linux desktop/server, you are all set
Here are the steps
Import the public repository GPG keys:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
Register the Microsoft SQL Server Ubuntu repository:
curl https://packages.microsoft.com/config/ubuntu/16.04/mssql-server.list | sudo tee /etc/apt/sources.list.d/mssql-server.list
Run the following commands to install SQL Server:
sudo apt-get update
sudo apt-get install -y mssql-server
After the package installation finishes, run the configuration script and follow the prompts.
sudo /opt/mssql/bin/sqlservr-setup
Once the configuration is done, verify that the service is running:
systemctl status mssql-server
I myself like to run ps and then grep for mssql
ps -ef | grep mssql
Ok so SQL Server is installed. In order to be able to run queries against SQL Server, we need to install the SQL Server tools
Open a new terminal window
Import the public repository GPG keys:
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
Register the Microsoft Ubuntu repository:
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
Update the sources list and run the installation command:
sudo apt-get update
sudo apt-get install mssql-tools
You will get a bunch of prompts, just follow it, say YES or y and you are set
Now let's run a command
Run this from a terminal window
sqlcmd -S localhost -U SA
Enter you password
Enter a simple query like SELECT @@version
Type GO, hit enter and voila, you should get results
Here is what it looks like
Here is also a video of the whole process, all of this was done in less than 5 minutes
Here is also a link to the official documentation
Install SQL Server: https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-ubuntu
Install the tools: https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-setup-tools#ubuntu
Visual Studio Code
Now unless you want to spend the rest of your life in a command window, I suggest you install VS CodeVisit this page https://code.visualstudio.com/#alt-downloads
Now for me .deb didn't work, so I got the tarball (.tar.gz) and manually extracted it
Then I ran the Code application inside the VSCode-linux-x64 folder
The first thing we have to do is install the mssql extension
Click on View-->Extensions, type in mssql, click on the install button
Now open a new file and save it with a .sql extension
Click on View-->Command Palette
Type sqlman
Add the server localhost), db name, username (probably sa) and add the password, save this with a name so you will know what it is
Type some valid SQL, hit CTRL + SHIFT + E or right click and select execute query
You should see something like this
There you have it, SQL Server running on Ubuntu, VS Code running on Ubuntu returning data from the SQL Server instance
I did hit an issue with trying to run xp_readerrorlog, you can read about that here:
How to read the errorlog on SQL Server installed on Linux
If you want to install SQL Server on Docker on a Mac, take a look at Aaron Bertrand's post
VS Code on Mac meets SQL Server on Linux (in Docker)
To see all my SQL Server on Linux posts, click here: SQL Server on Linux
Subscribe to:
Posts (Atom)