sql存储过程编写

This article talks about the three standard SQL unit tests which can be written against any stored procedure ultimately becoming SQL unit testing object to meet internal or external business specification.

本文讨论了可以针对最终成为SQL单元测试对象以满足内部或外部业务规范的任何存储过程编写的三个标准SQL单元测试。

This will also shed light on the importance of standardizing your SQL unit testing arsenal to an extent that it becomes quicker to get your SQL unit testing job done against your desired database.

这也将使标准化SQL单元测试工具库的重要性暴露到一定程度,以便可以更快地针对所需的数据库完成SQL单元测试工作。

Additionally, this article emphasizes database developers and professionals to think about having stored SQL unit tests templates ready to be used against any existing or upcoming databases.

此外,本文强调数据库开发人员和专业人员要考虑已准备好将存储SQL单元测试模板用于任何现有或即将使用的数据库。

先决条件 (Pre-requisites )

Let us first quickly go through the pre-requisites of the article so that the concepts and walkthroughs in this article can be easily understood by the readers.

让我们首先快速浏览本文的先决条件,以便读者可以轻松理解本文中的概念和演练。

T-SQL技巧 (T-SQL skills )

The article assumes that the readers are well familiar with T-SQL scripting and are comfortable to write and run SQL queries against the databases.

本文假定读者对T-SQL脚本非常熟悉,并且可以轻松地对数据库编写和运行SQL查询。

SQL单元测试概念 (SQL unit testing concepts)

It is also desired that the readers have basic SQL unit testing concepts along with the know-how of any of the two database unit testing practices:

还希望读者具有基本SQL单元测试概念以及两种数据库单元测试实践中的任何一种的专有技术:

  1. Conventional database unit testing 常规数据库单元测试
  2. Test-driven database development (TDDD) 测试驱动的数据库开发(TDDD)

To get a quick understanding of these SQL unit testing practices please refer to the following articles:

为了快速了解这些SQL单元测试实践,请参考以下文章:

  1. Conventional SQL Unit Testing with tSQLt in Simple Words 简单单词中使用tSQLt进行的常规SQL单元测试
  2. Fundamentals of Test-Driven Database Development (TDDD) with tSQLt unit testing
  3. tSQLt 单元测试 的测试驱动数据库开发(TDDD) 基础

TSQLt熟悉 (TSQLt Familiarity )

This article also assumes that the readers are well familiar with tSQLt, one of the most famous SQL unit testing frameworks, which by default (design) supports test-driven database development.

本文还假定读者非常熟悉tSQLt,它是最著名SQL单元测试框架之一,默认情况下(设计)它支持测试驱动的数据库开发。

If you have not got enough understanding of tSQLt then please refer to the following articles:

如果您对tSQLt不够了解,请参阅以下文章:

  1. tSQLt – A Forgotten Treasure in Database Unit Testing tSQLt –数据库单元测试中被遗忘的宝藏
  2. Why you should cleverly name Database Objects for SQL Unit Testing 为什么要为SQL单元测试巧妙地命名数据库对象

设置样本数据库 (Setup Sample Database)

Setup a sample database named StandardTSQLT by running the following SQL script:

通过运行以下SQL脚本,设置一个名为StandardTSQLT的示例数据库:

CREATE DATABASE StandardTSQLT
GOUSE StandardTSQLTCREATE TABLE [dbo].[TheTable]
([TemplateId] INT NOT NULL IDENTITY(1,1),[Name] VARCHAR(60) NOT NULL,[Detail] VARCHAR(200) NULL, CONSTRAINT [PK_TestTemplate] PRIMARY KEY ([TemplateId])
);
GOCREATE PROCEDURE [dbo].[TheObject]@Name VARCHAR(60),@Detail VARCHAR(200)
ASINSERT INTO TheTable(Name,Detail)VALUES(@Name,@Detail)GO

Run the script and view the sample database and its objects:

运行脚本并查看示例数据库及其对象:

设置tSQLt框架 (Setup tSQLt Framework)

Run tSQLt.class.sql (downloaded from tSQLt.org) script against the sample database to add tSQLt framework for SQL unit testing:

对示例数据库运行tSQLt.class.sql(从tSQLt.org下载)脚本,以添加用于SQL单元测试的tSQLt框架:

创建SQL单元测试类模板 (Create SQL unit test class template)

First of all, create a general test class called ObjectTests in the sample database StandardTSQLT as follows:

首先,在示例数据库StandardTSQLT中创建一个称为ObjectTests的常规测试类,如下所示:

USE StandardTSQLT
GO-- Creating unit test class ObjectTests
CREATE SCHEMA [ObjectTests]
Authorization dbo
GO
EXECUTE sp_addextendedproperty @name='tSQLt.TestClass',@value=1,@level0type='SCHEMA',@level0name='ObjectTests'

Please note that this article presents a new approach inspired by the SQL unit testing guidelines by Dave Green.

请注意,本文提出了一种新方法,该方法受Dave GreenSQL单元测试指南的启发。

为什么要进行标准SQL单元测试 (Why Standard SQL unit tests)

It is not odd to think that do we really need standard SQL unit tests while things change frequently in the database and SQL unit testing realm?

难怪我们真的需要标准SQL单元测试,而数据库和SQL单元测试领域中的事情却经常发生变化吗?

Yes, it is good to have standard SQL unit tests so that they can not only save time but also help to standardize our overall SQL database development and testing strategy contributing to streamlining database lifecycle management process.

是的,拥有标准SQL单元测试是一件好事,这样它们不仅可以节省时间,而且可以帮助标准化我们的总体SQL数据库开发和测试策略,从而有助于简化数据库生命周期管理流程。

典型SQL单元测试方案
(Typical SQL unit testing scenario
)

If we closely look at a typical SQL database development and SQL unit testing scenario then we can easily understand the importance of having standard SQL unit tests beforehand as we move from database to database.

如果我们仔细研究典型SQL数据库开发和SQL单元测试场景,那么我们在从一个数据库迁移到另一个数据库时,可以很容易地理解事先进行标准SQL单元测试的重要性。

A typical SQL unit testing scenario is as follows:

典型SQL单元测试方案如下:

  1. Create a database object to meet some business requirement 创建数据库对象以满足某些业务需求
  2. Create a SQL unit test to check the database object 创建一个SQL单元测试以检查数据库对象
  3. Run SQL unit test to check the database object does the job or not 运行SQL单元测试以检查数据库对象是否执行此任务
  4. If the test is passed then move on to the next SQL unit test 如果测试通过,则继续进行下一个SQL单元测试

How many times you have to repeat this process and in fact for one database you may create several database objects which must be unit tested to ensure that they do the job they were created for.

您必须重复执行此过程多少次,实际上,对于一个数据库,您可能会创建几个数据库对象,必须对它们进行单元测试,以确保它们能够完成为其创建的工作。

Let us suppose you already have some SQL unit tests in mind or on paper (as a start-up) then would not that be easy to straightaway create or reuse those standard SQL unit tests for any new object you work on.

让我们假设您已经在脑海中或在纸上(作为初创公司)已经有了一些SQL单元测试,那么就很难直接为您使用的任何新对象创建或重用这些标准SQL单元测试。

However, please bear in mind that it is not always possible to have standard SQL unit tests that fit every case.

但是,请记住,并非总是可能有适合每种情况的标准SQL单元测试。

存储过程和标准单元测试
(Stored Procedure and Standard unit tests
)

Another good example to understand the need for standard SQL unit tests is to look at a stored procedure.

了解标准SQL单元测试需求的另一个很好的例子是查看存储过程。

If you reverse engineer a stored procedure it is typically a set of SQL scripts that are frequently used to serve the purpose.

如果对存储过程进行反向工程,则通常是一组SQL脚本,这些脚本经常用于实现此目的。

So, if we can wrap a set of repeatable SQL scripts into a stored procedure to avoid repetition and standardize the process then why not we can have standard SQL unit tests to be applicable to most of the SQL unit testing objects.

因此,如果我们可以将一组可重复SQL脚本包装到存储过程中以避免重复并标准化过程,那么为什么不可以将标准SQL单元测试应用于大多数SQL单元测试对象。

Let us now focus on the three standard SQL unit tests which you can write against almost any stored procedure.

现在,让我们集中讨论可以针对几乎所有存储过程编写的三个标准SQL单元测试。

1 –测试以检查对象是否存在 (1 – Test to check object exists)

The first standard SQL unit test against any stored procedure is going to check whether it has been created or not.

针对任何存储过程的第一个标准SQL单元测试将检查它是否已创建。

Please remember the order in which database object and its SQL unit test is created may vary depending on the SQL unit testing pattern in use.

请记住,创建数据库对象及其SQL单元测试的顺序可能会因所使用SQL单元测试模式而异。

常规SQL单元测试 (Conventional SQL unit testing)

In this pattern, database objects are created first and their SQL unit tests are written next.

在这种模式下,首先创建数据库对象,然后编写其SQL单元测试。

So the order is as follows:

因此顺序如下:

  1. You create a SQL unit testing object which is stored procedure to meet business specification 您创建一个SQL单元测试对象,该对象是存储过程以满足业务规范
  2. You create a SQL unit test to check if it exists or not 您创建一个SQL单元测试以检查它是否存在

Obviously, it exists so what is the point of creating the SQL unit test to check object exists or not.

显然,它存在,因此创建SQL单元测试以检查对象是否存在的意义何在。

Actually, this is going to make sure the object has not been accidentally or purposely deleted (due to a conflicting requirement). This is somewhat like a schema-bound view which we can call SQL unit test bound object, a new term I am coining for the first time.

实际上,这将确保没有意外或有意删除对象(由于需求冲突)。 这有点像架构绑定视图,我们可以将其称为SQL单元测试绑定对象,这是我第一次创造的新术语。

创建SQL单元测试对象存在模板 (Create SQL unit test Object Exists Template)

Next, create SQL unit test object exists template as follows:

接下来,创建SQL单元测试对象存在的模板,如下所示:

--Creating object exists test template
CREATE PROCEDURE ObjectTests.[Test to check Object exists]
AS
BEGIN--Assemble--Act--AssertEXEC tSQLt.AssertObjectExists @ObjectName = N'TheObject'
END;
GO

空运行SQL单元测试 (Dry Run SQL unit test )

  --Run tSQLt TestsEXEC tSQLt.RunAll

So, the first most common SQL unit test is to check if the object of interest exists or not is complete now. We have just created this SQL unit test in the form a template that is going to be used afterward.

因此,第一个最常见SQL单元测试是检查目标对象是否存在,现在是否完成。 我们刚刚以模板形式创建了此SQL单元测试,此模板将在以后使用。

2 –测试以检查对象是否与正常输入数据相对应的正常输出 (2 – Test to check object has normal output against normal input data)

The next most desired SQL unit test is to check if object outputs normally when given normal input data.

下一个最需要SQL单元测试是在给定正常输入数据时检查对象是否正常输出。

普通输入数据 (Normal Input Data)

Normal input data is any acceptable input given to SQL unit testing object.

普通输入数据是提供给SQL单元测试对象的任何可接受的输入。

For example, if your object under test is a stored procedure which accepts Name parameter then any valid name or string supplied to the stored procedure during the SQL unit test is considered normal input data.

例如,如果您的被测试对象是一个接受Name参数的存储过程,则在SQL单元测试期间提供给该存储过程的任何有效名称或字符串都被视为普通输入数据。

正常输出数据 (Normal Output Data)

This means if an object has been given a normal input we expect it to give us the normal or expected output.

这意味着,如果已为对象提供了常规输入,则我们希望它能够为我们提供常规或预期输出。

For example if a stored procedure accepts a name and displays the list of authors having the same name then this is normal output data.

例如,如果存储过程接受一个名称并显示具有相同名称的作者列表,则这是正常的输出数据。

Alternatively, if a stored procedure accepts a name and adds it to a table then getting the contents of that table is considered normal output provided it contains the expected data inserted by stored procedure.

或者,如果存储过程接受一个名称并将其添加到表中,则只要该表包含存储过程插入的预期数据,获取该表的内容即视为正常输出。

In other words, this SQL unit test ensures that the stored procedure (if this is the object under test), when given acceptable input, produces acceptable output.

换句话说,此SQL单元测试可确保在给定可接受的输入时,存储过程(如果这是被测试的对象)产生可接受的输出。

普通输入模板时创建普通输出 (Create Normal Output When Normal Input Template)

Now create this SQL unit test template as follows:

现在,按如下所示创建此SQL单元测试模板:

--Create object normal output for normal input template
CREATE PROCEDURE [ObjectTests].[test to check Object ouputs normally when given normal input]
AS
-- Assemble
EXEC tSQLt.FakeTable @TableName='dbo.TheTable',@Identity=1-- Fake related table CREATE TABLE [ObjectTests].[Expected] -- Create expected table
([TemplateId] INT,[Name] VARCHAR(60) NOT NULL,[Detail] VARCHAR(200) NULL,
)INSERT INTO ObjectTests.Expected -- Insert data into exepcted table
(TemplateId,Name,Detail)
VALUES
(1,'Object Exists','Test to check object exists')-- Act
EXEC dbo.TheObject 'Object Exists','Test to check object exists' -- Run procedure
SELECT * INTO ObjectTests.Actual FROM dbo.TheTable -- Put the output into Actual table-- Assert (compare expected table with actual table results)
EXEC tSQLt.AssertEqualsTable @Expected='ObjectTests.Expected',@Actual='ObjectTests.Actual'

空运行SQL单元测试 (Dry Run SQL unit tests)

  --Run tSQLt TestsEXEC tSQLt.RunAll

3 –测试以检查对象是否具有异常输出以及异常输入数据 (3 – Test to check object has abnormal output against abnormal input data)

This is also one of the most widely used SQL unit tests since it checks for abnormal input and expects abnormal output.

这也是最广泛使用SQL单元测试之一,因为它检查异常输入并期望异常输出。

输入数据异常 (Abnormal Input Data)

Abnormal input data can be of different forms including no data at all to check the behavior of the object.

异常输入数据可以采用不同的形式,包括根本没有数据可以检查对象的行为。

输出数据异常 (Abnormal Output Data)

Abnormal output data can also be of various types including no output based on no data.

异常输出数据也可以是各种类型,包括基于无数据的无输出。

请针对异常输入模板创建异常输出 (Please create Abnormal Output against Abnormal Input Template)

Yes, I would like you to give it a try and let me know how far you can go. The clue is to consider no input and no output abnormality when writing your SQL unit test template.

是的,我希望您尝试一下,让我知道您可以走多远。 提示是在编写SQL单元测试模板时考虑没有输入也没有输出异常。

最后的话和进一步的阅读 (Final Word and Further Reading)

In this article, we have successfully created two general SQL unit tests templates and purposely left the third one for the readers to try to implement.

在本文中,我们成功创建了两个常规SQL单元测试模板,并有意将第三个模板留给读者尝试实施。

However, all of these SQL unit testing templates can now test any general stored procedure.

但是,所有这些SQL单元测试模板现在都可以测试任何常规存储过程。

Please go through the following articles to see these templates in action:

请仔细阅读以下文章,以查看这些模板的实际效果:

  1. Why you should cleverly name Database Objects for SQL Unit Testing 为什么要为SQL单元测试巧妙地命名数据库对象
  2. Fundamentals of Test-Driven Database Development (TDDD) with tSQLt unit testing tSQLt单元测试的测试驱动数据库开发(TDDD)基础
  3. Conventional SQL Unit Testing with tSQLt in Simple Words 简单单词中使用tSQLt进行的常规SQL单元测试

目录 (Table of contents)

tSQLt – A Forgotten Treasure in Database Unit Testing
Conventional SQL Unit Testing with tSQLt in Simple Words
Fundamentals of Test-Driven Database Development (TDDD) with tSQLt unit testing
10 Most Common SQL Unit Testing Mistakes
Why you should cleverly name Database Objects for SQL Unit Testing
Three Standard SQL Unit Tests you can write against any Stored Procedure
Creating SQL Unit Testing Utility Procedures with tSQLt
tSQLt –数据库单元测试中被遗忘的宝藏
简单单词中使用tSQLt进行的常规SQL单元测试
tSQLt单元测试的测试驱动数据库开发(TDDD)基础
10个最常见SQL单元测试错误
为什么要为SQL单元测试巧妙地命名数据库对象
您可以针对任何存储过程编写三个标准SQL单元测试
使用tSQLt创建SQL单元测试实用程序过程

翻译自: https://www.sqlshack.com/three-standard-sql-unit-tests-you-can-write-against-any-stored-procedure/

sql存储过程编写

sql存储过程编写_您可以针对任何存储过程编写三个标准SQL单元测试相关推荐

  1. (2)存储过程中可以调用其他存储过程吗?_详解Oracle创建存储过程、创建函数、创建包及实例演示...

    概述 说句实在的,平时工作基本上不会去背啥创建存储过程.创建函数.创建包之类的语法,但是相信大家面试啥的却基本会笔试这些,所以就对存储过程.函数和包的语法做下总结,也做个备忘!这里面语法大家理解就可以 ...

  2. sql azure 语法_深入了解Azure Data Studio:扩展和Azure SQL DB开发

    sql azure 语法 In the previous articles listed below, we went through the Azure Data Studio tool, star ...

  3. sql 分组求和_数据仓库工具–Hive(归纳笔记第六部分:SQL练习)

    写在开头: 本章是Hive教程第六部分,着重于归纳SQL编写. 文章内容输出来源:拉勾教育大数据高薪训练营. 本章将介绍Hive中常见的面试题和自己的解答思路,以供大家训练和记忆. SQL面试题 1. ...

  4. 太长的sql怎么分析_因为ESR, 我一定要推荐你这款 SQL 神器

    经常看我们号的读者,对SQL执行的理解,一定与别人不一样. 别人看到一条SQL,脑子里除了从上到下执行,就不会有别的想法.但是我们的读者肯定不是这样.比如以下这条简单的不能再简单的SQL,聚合求和: ...

  5. 软件开发中的需求文档由谁来编写_使用 RStudio 中的 Rmarkdown 编写演示文档

    首先介绍一下 R 这个在数据分析和统计学中使用的非常广泛的一门语言: R 是一个数据分析.统计建模和作图的软件,它包含一门计算机语言称为 R 语言,R 语言与通常的 C.C++.Java 等编程语言相 ...

  6. python自动化测试脚本怎么编写_【Python + uiautomator2】之编写unittest自动化测试脚本...

    #!/usr/bin/env python#-*- coding: utf-8 -*-#@Time : 2018/08/31 09:43#@Author : zc#@File : 发起任务.py im ...

  7. sql 精读(三) 标准 SQL 中的编号函数示例

    编号函数概念 编号函数会根据每一行在指定窗口中的位置向该行分配整数值. RANK().DENSE_RANK() 和 ROW_NUMBER() 示例: WITH Numbers AS(SELECT 1 ...

  8. 391、Java框架46 -【Hibernate - 查询HQL、查询Criteria、查询标准SQL】 2020.10.19

    0.目录 1.HQL 2.使用HQL,根据name进行模糊查询 3.查询Criteria 4.使用Criteria,根据name进行模糊查询 5.查询-标准SQL 6.使用标准SQL,根据name进行 ...

  9. sql int 比较_分享 21 个编写 SQL 的好习惯

    点击上方 IT牧场 ,选择 置顶或者星标 技术干货每日送达 前言 每一个好习惯都是一笔财富,本文基于MySQL,分SQL后悔药, SQL性能优化,SQL规范优雅三个方向,分享写SQL的21个好习惯,谢 ...

最新文章

  1. <文献阅读>用转移熵通过微阵列的时间序列推断基因调控网络(inferring gene regulatory networks from microarray time series data
  2. Spring Security——根据请求Header[Accept]不同返回不同类型资源解决方案
  3. 后悔贪心+P2949 [USACO09OPEN]Work Scheduling G
  4. Hibernate基础
  5. 专访阿里云专有云马劲,一个理性的理想主义者
  6. SpringBoot学习笔记(2) Spring Boot的一些配置
  7. 洛谷——P1706 全排列问题
  8. JSK-337 汽水瓶【数学+模拟】
  9. Linux给驱动模块传参数(module_param()用法)
  10. PHP switch问题
  11. 安卓 实现一个简单的计算器
  12. 天翎知识管理系统为研究所文档管理组织创新赋能
  13. 【ts】有关报错Line 0: Parsing error: Cannot read property ‘map‘ of undefined的解决方法
  14. “百度有啊”可以访问了,大家预测一把其前景如何?
  15. 【Redux】异步action与同步action
  16. 【二分图匹配】矩阵游戏
  17. 创新指南|如何以STEPPS模型6招打造病毒式传播产品
  18. 编译原理--实验2 语法分析
  19. Python 数据挖掘之中医证型关联规则挖掘
  20. Python实现GWO智能灰狼优化算法优化支持向量机分类模型(SVC算法)项目实战

热门文章

  1. Excel自动换行、Export2Excel 自动换行
  2. AGC 012 B - Splatter Painting
  3. 关于bash中if语法结构的广泛误解(转)
  4. 如何判断一个变量是数组还是对象
  5. LeetCode(783)——二叉搜索树结点最小距离(JavaScript)
  6. LeetCode(1103)——分糖果 II(JavaScript)
  7. 【零基础学Java】—finally代码块(四十七)
  8. weka分类器怎么设置类别_AI 迁移学习怎么玩?手把手教你实战迁移学习
  9. docker 部署了一个网站玩
  10. 电脑不启动任务管理器时cpu使用率很高,短时间内不使用电脑时启动为什么能耗可下降到15%以下?