Problems displaying this newsletter? View online.
SQL Server Central
Featured Contents
Question of the Day
The Voice of the DBA
 

Idempotent

This editorial was originally published on May 6, 2015. It is being re-run as Steve is traveling.

I ran into the word idempotent in the Stairway to Integration Services. I had heard the word, but I hadn't really considered how important it can be for a DBA or developer until that time. It's a term used in computer science, as well as other sciences, but I think it's one that many of us don't consider when we're writing code, especially code used to deploy software to other systems.

Most of us have written scripts like this:

if exists (select object_id from sys.objects where name = 'uspGetSales')
  drop procedure uspGetSales;
create procedure uspGetSales
as
...

At the end of running this code, we have the system in a state. If we run this over and over, we'll regularly return to the same state, which is often exactly what we want to occur. That works great when deploying code, but what about this:

insert into states select 'CO', 'Colorado';

If I run that multiple times, what will happen? Either I'll get errors if one of these fields is a PK, or I'll get multiple inserts. If I'm running this as part of a software deployment, do I want either of those conditions? Do I want my end user to experience either one?

No, I don't. Certainly if I'm manually making changes to systems I can probably avoid issues, but that's not what I want to do. Maybe you do, but I don't. I'd like to be able to restart my deployment if something fails, without causing other issues. I'd like idempotent code, like this:

if not exists (select abbrev from states where abbrev = 'CO')
  insert into states select 'CO', 'Colorado';

That way if I happen to run this code twice, or ten times, I arrive in the same place.

I realize that building scripts and deployment processes that are idempotent is a pain. It's work, but it's also scriptable and repeatable work that is easy to automate over time with a few patterns. I also realize that a little extra work to prevent issues is often an investment that's worth making for both my customers, and my reputation.

Steve Jones - SSC Editor

Join the debate, and respond to today's editorial on the forums

 
 Featured Contents
SQLServerCentral Article

Your Personal ChatGPT Tutorial

Kumar Abhishek from SQLServerCentral

This tutorial will show how to use an AI model with Python to respond to queries.

Technical Article

Live webinar - Prioritizing security: Essential strategies for IT leaders

Additional Articles from SQLServerCentral

In today's rapidly evolving landscape, managing the security of large and complex database estates is a critical challenge for organizations. Join us for a webinar on Nov 21 covering “Prioritizing security: Essential strategies for IT leaders,” where Redgate’s security experts will share their tips on how you can safeguard your database estate and minimize reputational risk. There will also be a Q&A session where you can get expert advice from our panelists.

External Article

Creating Views in MongoDB

Additional Articles from SimpleTalk

Like many relational database systems, MongoDB supports the use of views. A view is a read-only object in a MongoDB

Blog Post

From the SQL Server Central Blogs - SQL Data Pipelines: The Ultimate Guide to Streamlining Your Data Flow

Tracy McKibben from RealSQLGuy - Helping You To Become A SQL Hero

Want to build a data analytics foundation that transforms raw data into valuable business insights? Look no further than SQL! It's the perfect tool for creating powerful data pipelines...

Blog Post

From the SQL Server Central Blogs - Why is fn_xe_file_target_read_file the worst SQL function?

Zikato from StraightforwardSQL

I haven’t had a rant post in a while. There is a saying: “Anything before the word ‘but’ is ignored”. I love Extended Events, but …
reading the extended event...

Pro T-SQL 2022: Toward Speed, Scalability, and Standardization for SQL Server Developers

Site Owners from SQLServerCentral

Learn how to write and design simple and efficient T-SQL code. This is a hands-on book that teaches you how to write better T-SQL with examples and straightforward explanations.

 

 Question of the Day

Today's question (by Steve Jones - SSC Editor):

 

Cloud Sources for the Data Migration Assistant

Which cloud sources can be used for the current Data Migration Assistant (Sep 2024)

Think you know the answer? Click here, and find out if you are right.

 

 

 Yesterday's Question of the Day (by Steve Jones - SSC Editor)

Incremental Statistics

I have run this on SQL Server 2022 for the Sales database:

ALTER DATABASE Sales SET AUTO_CREATE_STATISTICS ON (INCREMENTAL = ON)

I then run this in the Sales database:

USE Sales
GO
CREATE STATISTICS CustomerStats1 ON dbo.Customer (CustomerKey, EmailAddress) WITH INCREMENTAL = OFF

The dbo.Customer table is partitioned. How are statistics created?

Answer: One stats blob for all partitions

Explanation: The incremental = off in the statement overrides the database setting. Ref: Statistics - https://learn.microsoft.com/en-us/sql/relational-databases/statistics/statistics?view=sql-server-ver16

Discuss this question and answer on the forums

 

 

 

Database Pros Who Need Your Help

Here's a few of the new posts today on the forums. To see more, visit the forums.


SQL Server 2016 - Development and T-SQL
Merge rows in SQL - I have a table like below ID Ident source val opendate closedate IsActive -------------------------------------------------------- 101 euid01 File01 x010 01-01-2023 01-01-2999 1 101 euid02 File01 x111 01-01-2023 01-01-2999 1 101 euid02 File01 x222 01-01-2023 01-10-2024 0 102 euid01 File11 x010 01-01-2023 01-01-2999 1 102 euid02 File12 x333 01-01-2023 01-01-2999 1 102 euid02 File10 x444 01-01-2023 01-10-2024 […]
Development - SQL Server 2014
How can I tell if an UPDATE command updated any rows? - I am calling an Update routine from C# and need to determine if the Update routine found a matching row to update. If it does not, I call the Insert Routine. I was hoping that if no row was updated, I would get an error which would tell me that I need to do the […]
SQL Server 2019 - Administration
It is difficult to learn Oracle. - I am a new student to learn Sql syntax. Is there any skills I need to master first?
SQL Server 2019 - Development
problem with PIVOT Table - Thanks in Advance! I'm having trouble grouping all counts and sums on one line for each county which should not look like (Code attached) Note one county is repeated and most are like that: County     MH    SA DD Cost Camden    0        0    1     152.88 Pitt      […]
Pivot Using SQL - I need some help with pivot. For the following data set:   and produce an output like this:
Iterative query to sum a colum relating to dates. - Hi, I have a simple table with 5 columns as follows with about 80,000 rows, I wish to sum the Hours for each Client at 2 week intervals. In other words starting at a particular EndDate, sum the Hours for each client for 2 week period. ID,  ClientID,  EndDate,  Hours 1 ,        […]
how to force SS to run certain SP in parallel? - hi everyone I have a SP that is run in SSIS.  That particular SP is basically running a bunch of SP.  See below for code.  SP1 and SP2 are independent of each other so they can definitely be run in parallel.  They all read data from the same table but each writes to a different […]
cannot read the next data row for dataset x - Hi, a peer asked me today to help on this ssrs error.   There is a lot going on in this report but the dataset in question uses a table meant just for ssrs and is filtered in the report by the userid of the person running the report.   When i run the query with a […]
SQL Azure - Development
Copying table data efficiently - Environment: Azure db with P15 (4000 DTU) Table 1: Having 90 coluumns of which 25 columnd encrypted , having approximately 60 million records Table 2: Exact similar structure as Table 1 with no records Requirement : Need to copy data from Table 1 to Table 2 In quicker time and least usage of DTUs. workdone: […]
Reporting Services
Report server errors - http://servername/reports/browse/ could not load folder contents http://servername/reportserver The server principal "NT SERVICE\SQLServerReportingServices" is not able to access the database "ReportServerTempDB" under the current security context. How do I fix this ? Does this have anything to do with SPNs ?https://learn.microsoft.com/en-us/sql/reporting-services/report-server/register-a-service-principal-name-spn-for-a-report-server?view=sql-server-2017
Reporting Services (2019) - Changing Service Account - This is something that happened while investigating a problem - so not impacting me any more - but I wonder what I'm missing. When trying to change the Service Account in the Reporting Services Configuration Manager from built-in to a domain account, it fails to connect to the database with the error: A connection could […]
Design Ideas and Questions
Should testing of Business Report changes be allowed in Prod? - When I arrived at the company 2.5 years ago, the environments were a mess!  Dev crap in Prod, Prod stuff in dev, many databases didn't have a Dev version, etc.  It has been a grueling marathon, but the environment is mostly stabilized.  I've even set up a PreProd environment where users can test deployments or […]
MySQL
MySQL joining - a tricky problem from a newby - Hi, I'm not even sure whether this is possible. I've three tables, one containing a location ID, and a location name. The second table is a users table, which contains the kind of information you'd expect, i.e. name address etc. The third table is a transactions table containing rows describing transactions which can be described […]
SQL Server 2022 - Administration
Blocking Connection to Server - Good Afternoon, We recently procured a service management software for our company, which uses SQL server 2019( 15.0.2125.1) as backend. The client application uses sql usernames to login into the application . I find this as a security issue, as any internal user can directly connect to the server either through ODBC or through SSMS […]
Migrating standalone database to always-on cluster. - I am planning to migrate a currently standalone database to SQL Always On. This would be an OS & SQL upgrade & migrate activity to an entire new box of servers running on latest Windows Server & SQL Server. I have never perform this configuration before and will be referring to steps which is stated […]
 

 

RSS FeedTwitter

This email has been sent to {email}. To be removed from this list, please click here. If you have any problems leaving the list, please contact the webmaster@sqlservercentral.com. This newsletter was sent to you because you signed up at SQLServerCentral.com.
©2019 Redgate Software Ltd, Newnham House, Cambridge Business Park, Cambridge, CB4 0WZ, United Kingdom. All rights reserved.
webmaster@sqlservercentral.com

 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -