Confusion with Database Corruption
When working with clients on fixing their corrupt SQL Server databases we run into some common beliefs about corruption that are not quite true, such… Read More »Confusion with Database Corruption
When working with clients on fixing their corrupt SQL Server databases we run into some common beliefs about corruption that are not quite true, such… Read More »Confusion with Database Corruption
Working on a recent SQL Server merge replication project we needed to update some of the servers in a merge replication scenario without upgrading all… Read More »For merge publications, the version of the Subscriber must not exceed the version of the Publisher
Doing some recent investigation to find out why backups where not showing up where they were expected to be I put together this script to… Read More »TSQL To List Recent Backups
After finishing a recent project we received this feedback from the customer. As a community health center with many competing priorities and limited resources, we… Read More »Another Happy Customer
Occasionally I have the need to look up the size of all databases on an instance, based on the log file size and the data… Read More »Size of all databases on an instance
When you execute xp_cmdshell SQL Server runs a command at the operating system level similar to the old DOS prompt, or CMD shell. There are… Read More »Who does SQL Server run xp_cmdshell command as?
To switch a database from simple to full recovery model, it is simple to do when you have one or two databases that need to… Read More »Switching multiple databases to FULL recovery model.
5 years at Stedman Solutions, LLC. Yes, it has been 5 years since I have been full time at Stedman Solutions. Check out my post… Read More »5 years full time at Stedman Solutions, LLC.
Occasionally I have come across the need to save the output from a query to a varchar value, for instance varchar(max). There are lots of ways to do this, could you use a cursor and just append to a string each loop through the cursor, but there is an easier way.
Examples of a need to do this would be to write the output to a file, or to build an email message that is going to be sent, or many other possible scenarios.
Lets start with a simple query on SQL Server 2019, grabbing a list of databases, and with a where clause to keep the list short for now.
--start with a simple query
SELECT *
FROM sys.databases
WHERE database_id < 9;
Which produces output something like this.

Now lets trim it down to just the data we want to build into string output.
Read More »Saving Query Output to a StringPosted 3/29/2020 — Edited 3/30/2020 to fix a couple of copy and paste errors, and to clarify a couple of items. Thanks Charlie for your… Read More »SQL Server – Writing to a File