• Skip to main content
  • Skip to primary sidebar

DallasDBAs.com

SQL Server Database Consulting

  • Services
  • Pocket DBA®
  • Blog
  • Testimonials
  • Contact
  • About

Kevin3NF

TIL: Joining across databases with different collations

May 16, 2017 by Kevin3NF 3 Comments

Quick post today…

Yesterday I was approached by a developer with something it has never occurred to me to try in 18 years of being a DBA.

“Can I write a query across two databases with different collations?”

My first thought took me back to SQL 7, when you had to reinstall SQL Server to change collation settings (IIRC).   My second and immediate thought was “Quit being so old and look it up, Kevin.”

So I bounced over to The Google, then to an answer on StackExchange that I can’t find right now (of course, but I did upvote it yesterday) and found what I needed.

I have two test databases on my local SQL 2014 Dev instance, both with an Orders and OrderDetails table in them with no data.  Both were the default collation so I changed one of them to CS instead of CI:

ALTER DATABASE [test2] COLLATE SQL_Latin1_General_CP1_CS_AS

I entered a test order in each DB manually, and in the “Note” column I changed the case of the word Five.   Why Five?  No idea…its just text, and that’s what my developer (Jared) was having to join on.

Select * from Test.dbo.orderdetails where note = 'five'
 
Select * from test2.dbo.OrderDetails where Note = 'FIve'

Results:


OrderDetailID OrderID ItemQty Note
------------- ----------- ----------- ---------
1 1 5 Five

(1 row(s) affected)

OrderDetailID OrderID ItemQty Note
------------- ----------- ----------- ---------
1 2 5 FIve

(1 row(s) affected)

Next up, write the Select statement the normal way to duplicate the error Jared was getting:

--fails
Select *
from Test.dbo.orderdetails A join test2.dbo.OrderDetails B
 on A.[note] = B.[Note]

Result:

Msg 468, Level 16, State 9, Line 11
Cannot resolve the collation conflict between “SQL_Latin1_General_CP1_CS_AS” and “SQL_Latin1_General_CP1_CI_AS” in the equal to operation.

 

So…now to bring it in the correct way (and there is probably more than one correct syntax for this…I didn’t look):


--works
Select *
From Test.dbo.orderdetails A
Join test2.dbo.OrderDetails B
on A.[note] = 
   Cast(B.[Note] as varchar(50)) COLLATE SQL_Latin1_General_CP1_CI_AS

Result:


OrderDetailID OrderID ItemQty Note OrderDetailID OrderID ItemQty Note
--------------------------------------------------------------------- 
1 1 5 Five 1 2 5 FIve

(1 row(s) affected)

 

All I did here was tell the query to treat the Test2 Note field as Case Insensitive for this one query.  Nothing changed in the source data itself, or the database settings/DDL.

It took me five times longer to write this up than to test it, since I already had some generic test databases ready to go.   Same ones I use for testing my answers on MSDN, SSC and DBA Stack questions.

If this helped you, please share, like, re-post, etc. as you see fit.   Or a thumbs up in a comment if you like.

Thanks for reading!

Kevin3NF

(stalk me on Twitter)

Filed Under: SQL, TIL, TSQL

Free SQL Server DBA Fundamentals training (DFW) – completed

May 4, 2017 by Kevin3NF 2 Comments

Free.  In person.  Interactive.

Update: This is in-person only…not online or Webex at this time.

I really like training and teaching the basics of anything to people new to it.  Right now that is SQL Server, and cycling (I am Kevin3NF or CoachKev depending…).  Every day on the various forums I see the same questions over and over on basic tasks…and the askers go away with a solution but very often they don’t get the understanding to be able to resolve the issue on their own the next time.

So…if you are in or near the Dallas/Ft. Worth, Texas area and have a couple of hours free after work once every 2 weeks come join me for some very informal training.

Target audience:

  • Managers
  • Developers
  • SysAdmins
  • Students (college, tech schools, etc.)
  • Very junior/accidental DBAs that are struggling to stay afloat

If you are already a DBA, this is probably not for you unless you are just starting out.

RSVP for Day 5 here

5 weeks, every other Wednesday 6-8pm starting on May 24, 2017 in Richardson, TX.

  •  Day 1 (May 24):
    • Intro to databases, with a little history thrown in
    • Installing SQL Server 2016/2014 (on your laptop)
  • Day 2 (June 7):
    • Getting around in SQL Server Management Studio
    • SQL Server Security Model
  • Day 3 (June 21):
    • Backup Basics…the Why, followed by How
    • Creating a database from scratch, or from a backup
  • Day 4 (July 5):
    • Database querying basics – getting the CRUD
    • Indexing and basic performance troubleshooting
  • Day 5 (July 19):
    • High Availability/Disaster Recovery options (overview)
    • Wrap up

If this goes as well as I hope, I will offer it again after some time off.  Maybe in a different part of the Metroplex.

Not sure if this class is right for you?  Ask in the comments, send me a DM on Twitter, or email me.

Thanks for reading!

Kevin3NF

Follow Dallas DBAs on LinkedIn:

Filed Under: Accidental DBA, Apprentice, Beginner, Career, EntryLevel, Speaking

SQL Saturday Houston, 2017

April 27, 2017 by Kevin3NF Leave a Comment

I will be presenting my session “Backups for non-DBAs…the Why, not the How” at SQL Saturday Houston on June 17.

Abstract:

Join me and get some new perspective on what your DBA is doing, and why!

Targeted at the non-DBA such as BI/DW folks, Application Developers, Managers, and System Admins, this session will go over the reasons we back up our databases, our systems, etc. Considerations such as Single points of failure, High Availability/Disaster recovery, Business Continuity and others will be discussed in this interactive conversation. It will be conversation heavy, with supporting slides to download, and one Database backup demo at the end if time permits.

Everyone involved in a technical role needs to at least know that the things they have created will be recoverable in the event of a disaster, or even just an “oops” moment. The CIO/CTO should know how long critical systems will be down when bad things happen.

Backups are everyone’s responsibility…whether asking the right questions or implementing the process.

If you come on Friday, there is a pre-conference full day of training on Performance Tuning by Brent Ozar…$199 as of this post for a full day of awesomesauce. Or, my friend Tim Mitchell (b|t) will teach you how to build better SSIS packages for $129.

Hope to see you there!

Kevin3NF

Filed Under: Accidental DBA, backup, Beginner, Speaking, SQLSaturday

TSQL Tuesday – Changing Times

April 11, 2017 by Kevin3NF Leave a Comment

T-SQL Tuesday is a blog party started by Adam Machanic (b/t) over five years ago. The first Tuesday of each month a blogger hosts the party and suggests a topic. Anyone who is interested blogs on that topic on the second Tuesday. It can be a lot of fun and quite a challenge to blog on a topic you didn’t pick.

This month’s topic is presented by Koen Verbeeck (b/t), who has challenged us to write about “The times they are a-changing”, a response to Will the Cloud Eat my DBA Job? by Kendra Little (b|t).

I previously blogged my Predictions for the future of the DBA role, so I offer these posts as my T-SQL Tuesday submission:

Prediction – The SQL Server DBA Role

Supporting post on why I am embracing PowerShell

Yes Koen, the times are indeed changing, as they have been for years and always will.  Remember, your mobile phone has more power than the best computers 50 years ago.  And when was the last time you actually saw a buggy-whip? 🙂

Anticipate the changes, test the betas, learn the terminology….or be left behind maintaining that old SQL Server 7.0 install in the corner that everyone is scared to touch.

Thanks for reading!

Kevin3NF

Feel free to stalk me on twitter…

 

Filed Under: Career, PowerShell, SQL, TSQL2sday

How I became a:________________

April 3, 2017 by Kevin3NF Leave a Comment

Thanks to Andy Warren (b|t) for this idea….

I am starting a collection of “How I Became A _____________” stories and will be aggregating them right here.   Your contributions are welcomed and encouraged.  You can email me some text, send me a link to your blog where you write it up, or even a video on YouTube if you have it.  Include what section below it should go in.  Longer submissions would be better as links to your blogs, shorter ones work as just text if you prefer.  Add your link as a comment, and I’ll delete that after incorporating into the list.

For now, I’d like to stick with items in and around the database world…DBA, Developer, Architect, BI pro, Database Manager, SQL Server, Oracle, etc.

The goal here is to have a place for people considering database work as a profession a place to go to get more info direct from the mouths of those of us already in the industry.  Lets keep the contributions family-friendly and encouraging!

 

Database Administration:

  • Jeff Miller (t) reboots his career and joins the Dallas DBAs staff
  • Andy Levy (b|t) finds out he has DBA tendencies…
  • Arun Sirpal (b|t) never stops learning!
  • Michael D’Spain, the landlocked surfing DBA musician shares his story
  • David Alcock (b|t) shares his path to DBA, along with tips for those considering the idea from across the pond.
  • Jr. DBA Julie (t) shares her story
    The story of how I became a SQL database administrator is … less than glamorous. Without going into specifics, I found myself at 35 years old in the position of needing to change careers – and needing to do so at a breakneck speed, because quite unexpectedly I became the sole provider for my family. I had a bachelor’s degree in art, few marketable skills, no professional job history for the past 10 years, and the weight of the consequences of failure breathing hard down my neck.
    So I reached out. I talked to people. I spammed my Facebook and Twitter feeds, asking people what it was like in their industry – what the job prospects were, what the working conditions were like, what the requirements were, what the long term growth potential might be. I made sure everyone I knew was aware I was looking for a job.
    I did not get a single response to the resumes I submitted to job postings. I DID get a response from my friend Jen McCown, who asked me one day if I’d like to do grunt DBA work for her home consulting business. YES, I said, I would love to do that.
    “Is this a pity job?” I asked her.
    “No no no,” she lied.
    Jen and her husband Sean spent a good part of the next couple of months training me to be a DBA. It was very difficult, as the learning curve was so steep as to be nearly vertical. I lived, breathed, ate and slept databases. I was keenly aware of what failure would mean, for me and my children, if I didn’t nail this. I remember one instance where I was sitting with my youngest child while he fell asleep, and he was annoyed by the light of my laptop and the click of the keys as I worked on some issue or other.
    “I hate databases,” he grumbled and pulled the covers over his head.
    At the moment, I kinda hated databases, too.
    Several months later, Sean introduced me to the folk at a local company, who had a position open for a junior DBA. They needed someone who could do backups and restores, document procedures, run queries, and look into basic problems like blocking and security access. I got the job.
    I’m almost three years into a DBA career now, and I am occasionally reminded with great humility and gratitude that I would not be here without Sean and Jen. I was lucky. But I am coming to realize that all of us have a Sean-and-Jen – people who opened up doors with opportunities for work. So how would I apply my experience becoming a DBA to others? To a certain extent, you have to make your own luck. Networking is the single greatest avenue to luck, but a lot of work comes before and after the opportunity presents itself. Have your resume together, read articles and books, take training courses and certifications, go to user group meetings and such. Most importantly, be prepared to work your butt off. Database work, especially in the beginning of a career, may mean a lot of late nights, even overnights. It’s not for everybody, but it certainly is a great path.
  • Chris Yates (b|t) – The SQL Professor
  • Anders Pederson (b|t) in 2 parts:
    • About Anders
    • Anders gives Access the boot
  • @SQLAndy: How to Become a SQL Server DBA (whitepaper, site registration required)
  • DBA Kevin Hill gives an email interview

Database Consultant:

  • David Alcock (b|t) turns to Consulting!
  • Matt Gordon (b|t)- Support Analyst to Consultant

Database Developer:

Business Intelligence/Analytics:

Database Management:

ETL Developer:

Database Architect:

Teacher/Presenter/Speaker:

  • Erin Stellato’s (b|t) 20 year presenting journey
  • John Deardurff (b|t), Trainer – Database on a Dare

 

Filed Under: Accidental DBA, Beginner, Career, Oracle, SQL

DBCC Opentran, simplified!

April 3, 2017 by Kevin3NF 2 Comments

In my Top 10 SQL Server Functions post awhile back, I listed DBCC OPENTRAN as one of the top 3, and for good reason.

An Open transaction may simply be something that has not finished yet, or someone issued a BEGIN TRAN without a corresponding COMMIT or ROLLBACK.  Or as we will see at the end, replication is having issues.

You can use this against any database with minimal syntax and get back solid information very quickly.

 
--connect to sample db
use MyDatabase
go

--as generic as this command gets and still runs:
DBCC OPENTRAN
 

Result if nothing is open:

No active open transactions.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

If I start and execute a DML (insert, update or delete) transaction with BEGIN TRAN and leave out the corresponding COMMIT, I get:

Transaction information for database ‘SmallData_BigLog’.
Oldest active transaction:
SPID (server process ID): 64 <———–
UID (user ID) : -1
Name : user_transaction
LSN : (637:4620:1)
Start time : Apr 1 2017 4:59:02:307PM
SID : 0x0105000000000005150000004893a0845595b6fef515cd5de9030000
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Now, if I open a second transaction (in a new query window) and execute any DML statement without the COMMIT, and then run DBCC OPENTRAN again, I get:

Transaction information for database ‘SmallData_BigLog’.
Oldest active transaction:
SPID (server process ID): 64 <———–
UID (user ID) : -1
Name : user_transaction
LSN : (637:4620:1)
Start time : Apr 1 2017 4:59:02:307PM
SID : 0x0105000000000005150000004893a0845595b6fef515cd5de9030000
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Yes…the same output, as this is just showing the ONE oldest transaction.

I can run a query to show that there are two SPIDs with open transactions:

--
SELECT spid, blocked,[dbid],last_batch,open_tran
FROM master.sys.sysprocesses
WHERE open_tran <> 0
 

DBCC Opentran Spid

If I COMMIT spid 64 and re-run DBCC OPENTRAN, the SPID changes to the second transaction I started:

Transaction information for database ‘SmallData_BigLog’.
Oldest active transaction:
SPID (server process ID): 52  <———–
UID (user ID) : -1
Name : user_transaction
LSN : (637:9603:1)
Start time : Apr 1 2017 5:11:20:830PM
SID : 0x0105000000000005150000004893a0845595b6fef515cd5de9030000
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

If I COMMIT spid 52 and re-run DBCC OPENTRAN along with checking sysprocesses for open_tran <> 0 I get:

No active open transactions.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
spid blocked dbid last_batch open_tran
—— ——- —— ———————– ———
(0 row(s) affected)

 

Now, all of that was just running DBCC OPENTRAN by itself.  There are additional options:

--specify dbname, dbid or 0 for the current database
DBCC OPENTRAN (SmallData_BigLog)

You will get results in the same format as the previous examples.

You can suppress all messages, regardless of if a transaction is open or not (but I have no idea why this would help you…)

DBCC OPENTRAN (0) with no_infomsgs

Result:

Command(s) completed successfully.

 

If you needed to periodically capture the oldest transaction, in order to review later, use WITH TABLERESULTS:

-- TableResults only shows the oldest open tran
-- useful running in a loop to load the oldest
-- tran over time.

--create a temp table
CREATE TABLE #OpenTranStatus (
ActiveTransaction varchar(25),
Details sql_variant
);

-- Execute the command, putting the results in the table.
INSERT INTO #OpenTranStatus
EXEC ('DBCC OPENTRAN (SmallData_BigLog) with tableresults')
SELECT * FROM #OpenTranStatus
DROP TABLE #OpenTranStatus
   

In the above, you could create a user table instead of a temp table of course…it depends on your needs.

One more particularly useful item you may see when running DBCC OPENTRAN by itself:

Transaction information for database ‘Music’.
Replicated Transaction Information:
Oldest distributed LSN : (37:143:3)
Oldest non-distributed LSN : (37:144:1)
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

 

If your database is participating in Replication as a Publisher, this may show up when running OPENTRAN, but it doesn’t necessarily mean that the transaction is actually open.  I set this up and stopped the Replication Log Reader and Distribution agent jobs.   I then added some data to a published table (article) and ran DBCC OPENTRAN to get the above result.  Note that there are two lines with LSN information in them (no SPIDs)

I then ran the Log Reader Agent job and got back:

Transaction information for database ‘Music’.
Replicated Transaction Information:
Oldest distributed LSN : (37:157:3)
Oldest non-distributed LSN : (0:0:0)
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

I have verified that new records I inserted have been read by the log reader, AND distributed to the subscriber(s).  This means that while you are seeing

Oldest distributed LSN : (37:157:3)

There is not an error…just info.

If you have non-distributed LSNs, there is something to troubleshoot in the replication process which is way outside the scope of this post.  A non-distributed replicated transaction/LSN CAN cause some huge Log file growth, and need to be investigated.  If this happens frequently, use the TABLERESULTS option to log to a regular table and alert on it.

Hopefully this gives you some insight into various ways to use DBCC OPENTRAN as well as use cases for the various options.  90% of the time I run this, it is due to application transactions timing out, or log file growth issues.

I love comments….please feel free to leave questions for me in them on this topic.

Thanks for reading!

Kevin3NF

My Pluralsight course for new SQL Server DBAs

Follow @Dallas_DBAs


Filed Under: Accidental DBA, Beginner, EntryLevel, Performance Tuning, SQL

  • « Go to Previous Page
  • Go to page 1
  • Interim pages omitted …
  • Go to page 29
  • Go to page 30
  • Go to page 31
  • Go to page 32
  • Go to page 33
  • Interim pages omitted …
  • Go to page 44
  • Go to Next Page »

Primary Sidebar

Search

Sign up for blogs, DBA availability and more!

Home Blog About Privacy Policy
  • Home-draft
  • Blog
  • About Us

Copyright © 2026 · WordPress · Log in

 

Loading Comments...