Adding Test data to msdb.sysjobhistory

One of our internal people was looking to test some process in (I assume) Redgate Monitor and needed more job history than they had in msdb.sysjobhistory. They wanted to use SQL Data Generator to help, but couldn’t make it work.

This gives the solution I sketched for them. It can work with some other system tables, but not all. Many system tables do not allow user data to be inserted. Some do.

The Main Problem

The main problem here is that SQL Data Generator doesn’t see system tables. If I open a project in msdb, I see this:

2024-11_0297

That matches what I see in SSMS. Only user tables.

2024-11_0296

However, I know I can do this and it works.

INSERT INTO dbo.sysjobhistory
  (job_id, step_id, step_name, sql_message_id, sql_severity, message, run_status, run_date, run_time, run_duration, operator_id_emailed, operator_id_netsent, operator_id_paged, retries_attempted, server)
VALUES
  ('EA6B3BC3-D358-4B0C-A793-8C3C558098AB', 0, 'mystep', 0, 0, 'Executed as user: NT Service\SQLAgent$SQL2022. The job script encountered the following errors.'
  , 0, 20241111, 175438, 0, 0, 0, 0, 0, 'ARISTOTLE\SQL2022')

Now, how to get SQL Data Generator to help?

A Little System Table ETL

Since I know I can insert data into the table, how can I generate data? Apparently SQL Data Generator cannot read these tables, but I can use a trick that I’ve used in the past.

First, I’ll run this in msdb:

SELECT *
  INTO mysysjobhistory
  FROM dbo.sysjobhistory AS s
  WHERE 1 = 0

This code will make a copy of dbo.sysjobhistory with no data. However, this is a user table. Once I do this, now I can refresh SQL Data Generator and I can see my table.

2024-11_0094

Now I can use the settings to get the type of data I want. Here’s a preview of some data I set, using the data in my existing table.

2024-11_0095

Now, I can click generate data and I have data added to my user table. If I query this table, I see data:

2024-11_0096

The last step is to move this data. I’ll use this query:

INSERT dbo.sysjobhistory
   (job_id, step_id, step_name, sql_message_id, sql_severity, message, run_status, run_date, run_time, run_duration, operator_id_emailed, operator_id_netsent, operator_id_paged, retries_attempted, server)
SELECT job_id, step_id, step_name, sql_message_id, sql_severity, message, run_status, run_date, run_time, run_duration, operator_id_emailed, operator_id_netsent, operator_id_paged, retries_attempted, server
  FROM dbo.mysysjobhistory AS m

Once this runs, I can see the data in msdb.

2024-11_0097

Of course, I’d also have to populate sysjobs if I wanted this linked to a job and shown in the Agent Job History Viewer.

Summary

This post showed how I’ve sometimes worked in situations where I couldn’t directly access a table from an application. In this case, I want to get data into sysjobhistory, but SQL Data Generator doesn’t support that directly. My solution:

  1. make a copy of the table
  2. insert data into the copy
  3. move the data into the original

This has worked in a few situations as well where I might need to move/stage data before it gets into an application table. In this case, we wanted to generate some random history for Redgate Monitor to read.

This can work for other tables as well, as long as you can insert..select into them.

SQL Data Generator is a neat tool to generate data quickly for a variety of purposes in SQL Server. Give it a try, especially if you already have the Toolbelt Essentials.

Posted in Blog | Tagged , , , | Comments Off on Adding Test data to msdb.sysjobhistory

Have Grace

Thanksgiving is tomorrow in the US and it is supposed to be a holiday when we give thanks for our blessings in life. My wife usually has everyone in our family tell what they are thankful for this year. I also see many people posting things they are grateful for during the month of December.

Last month I was lucky enough to have dinner with Bob Ward and we were talking about some of the things we’d seen in travels, often some stressful times for ourselves or others. We’ve seen many people get upset or angry or have some other reaction. Both of us have some empathy for others, recognizing that we don’t really know their history or experiences, and it doesn’t make sense for us to get upset. If we knew those things,  we might better understand the reaction that someone displays to a situation.

Bob used a great phrase, saying that we should “have grace” when dealing with others.

I love that. I know I have stressed days, or grumpy days, or times when I’m not at my best behavior and I’d hope others would understand. I try to do the same for others, and I remind myself to “have grace” now when I see someone dealing with difficult situations.

I hope all of you enjoy the holidays this year, but if not, I hope others have grace when dealing with you. And I hope you remember to have grace with others.

Steve Jones

Listen to the podcast at Libsyn, Spotify, or iTunes.

Note, podcasts are only available for a limited time online.

Posted in Editorial | Tagged | Comments Off on Have Grace

Computer Algebra

I was a bit of a math nerd in high school and college. Some of you might have been as well, but I took advanced math all through high school, culminating with AP Calculus as a senior with 11 other kids (of about 320). In college, I started with Calculus III freshman year and went on to take 7 more semesters of various high level maths. One of those classes included analyzing data with linear regression, which we did with hand calculators and formulae.

At SQL Saturday Pittsburgh 2024, I watched a talk from Jeff Moden on linear regression. It was a trip down memory lane, with Jeff explaining how the process worked, the flaws, and how this technique could be used to do some predictions on data stored in SQL Server. It was a great session on the topic, but I liked that Jeff showed how you can use SQL Server to do various math calculations that might be useful to analyze data. I see applications sometimes programming various formulas, but I don’t often see people doing this in database queries.

This reminded me of some early programs I’d written in BASIC on an Apple II computer. We had to do labs and hand calculate out various results based on measurements. However, small mistakes in our notes sometimes created large errors in our lab reports, which would affect our grades. Sometimes we’d make a mistake in the chemistry process and sometimes we’d just write the wrong number down (or forget to do so). However, we couldn’t repeat the lab, so filling out reports at home was maddening and stressful.

I wrote programs that create a form and let me enter all my data. This would calculate the results, and I could “massage” the data to get a low error count. Maybe not the most ethical method, but for high school labs in which we were time and resource limited, and where our grades depended on a high level of accuracy, it seemed acceptable to me. I also learned the formulas better since I had to program them and debug my work.

Creating a simple system to do calculations can be very handy for many of us in our lives. While there is software available for many purposes, like examining mortgages or calculating a budget, doing some of that work yourself let’s you customize things, as well as ensure you understand how the program works. However, instead of C#, Python, or something else, I might use SQL these days as I use daily. I can store data across time, something that might help me better analyze my own data with SQL.

If you’ve never tried math formula in SQL Server, give it a try. You might have some new appreciation for the equations you solved in school, while building a little more familiarity and skill with the language you use in your daily work.

Steve Jones

Listen to the podcast at Libsyn, Spotify, or iTunes.

Note, podcasts are only available for a limited time online.

Posted in Editorial | Tagged | Comments Off on Computer Algebra

Grinding Away: Kobe Bryant

I saw this story from Robert Horry, one of the greatest clutch shooters in basketball. Don’t know it’s true, but I think it is.

When I got traded to the Lakers in ’97, Kobe Bryant was just a rookie. The dude couldn’t shoot threes. We would play this shooting game every day after practice. It was me, Kobe, Brian Shaw, Mitch Richmond and Kurt Rambis. Kobe would lose every time. We would get to practice the next day and sure enough, Kobe would already be there shooting nothing but threes. Like clockwork, at the end of practice he’d say, “Let’s play the game! I’m ready for you.” And we would beat his ass again.

He would never stop. It was incredible. He practiced until one day, a couple months later, he finally won. If you literally said, “Kobe, I bet you can’t make five in a row by dropping the ball and kicking it in from half court,” that motherfucker would go out there and practice it until he could do it. And that’s what people don’t understand when they talk about champions — when they talk about a winner’s mentality. Kobe’s dedication to the game is unreal.

In case you think this isn’t true, listen to Kobe in this interview.

Why I like this

To compete against others, you have to work harder. I tell kids I coach that it isn’t enough to get better in sports, but you have to get better faster than others are getting better. That takes work, it takes setbacks, it takes failures. It takes effort to overcome those to get better.

You might not have the raw talent to be one of the best every in your area, but you can be really, really good, and you can compete with many others.

The modern world, with jobs being remote, companies hiring everywhere, more tech people wanting jobs, you need to compete well, which means work.

Work to improve.

Why I don’t like this

I do think that Kobe worked in a very narrow domain, playing his sport. His sport didn’t change much, and he just needed to improve his skills, while knowing how to work against others. He was competing in a narrower space, against many knowns.

For many of us, our world is more complex, with many more dimensions of new technologies, unknown demands or desires from employers (or potential employers), and an every growing set of things that appear in our industry.

Driving to continue to be better is good, but you’ll never get to be amazing in everything in tech, or even one platform. Keep some balance and remember there are other important things in your life. Not so important you shouldn’t improve, but important enough to require some of your time.

Posted in Blog | Tagged , | Comments Off on Grinding Away: Kobe Bryant