sql過濾空字符串,sql過濾代碼段_如何創建和管理T-SQL代碼段

 2023-10-18 阅读 22 评论 0

摘要:sql過濾代碼段 介紹 (Introduction) Transact-SQL (T-SQL) snippets were first introduced in SQL Server 2012 Management Studio. T-SQL snippets are templates containing one or more T-SQL statements which you can easily use them when you develop T-SQL scripts.

sql過濾代碼段

介紹 (Introduction)

Transact-SQL (T-SQL) snippets were first introduced in SQL Server 2012 Management Studio. T-SQL snippets are templates containing one or more T-SQL statements which you can easily use them when you develop T-SQL scripts. The main concept behind code snippets is code reuse. With code reuse you develop faster, easier and with less syntax errors. If you frequently use specific T-SQL statements, then you should consider creating snippets with these statements as they will help you a lot.

sql過濾空字符串、 Transact-SQL(T-SQL)片段最初是在SQL Server 2012 Management Studio中引入的。 T-SQL代碼段是包含一個或多個T-SQL語句的模板,您在開發T-SQL腳本時可以輕松地使用它們。 代碼段背后的主要概念是代碼重用 。 通過代碼重用,您可以更快,更輕松地開發并且語法錯誤更少。 如果您經常使用特定的T-SQL語句,則應考慮使用這些語句創建代碼片段,因為它們將對您有很大幫助。

There are two types of T-SQL snippets: (i) Expansion, and (ii) SurroundsWith.

T-SQL片段有兩種類型:(i) 擴展和(ii) SurroundsWith

sqlplus -s命令?Expansion snippets are full T-SQL statements which can be added in your T-SQL scripts. SurroundsWith snippets is code which can surround other T-SQL statements. For example, you can have a snippet that has the “BEGIN…END” block, “IF” block, “WHILE” block, “TRY…CATCH” block, etc.

擴展代碼段是完整的T-SQL語句,可以在您的T-SQL腳本中添加。 SurroundsWith片段是可以包圍其他T-SQL語句的代碼。 例如,您可以使用具有“ BEGIN…END”塊,“ IF”塊,“ WHILE”塊,“ TRY…CATCH”塊等的代碼段。

Even though snippets were first introduced in SQL Server 2012 Management Studio, they existed in Visual Studio long before SQL Server 2012 so their benefits and usage are well known to the technical community.

sql去除空數據? 盡管代碼片段最初是在SQL Server 2012 Management Studio中引入的,但它們早在SQL Server 2012之前就已存在于Visual Studio中,因此它們的優點和用法為技術社區所熟知。

在SSMS中使用T-SQL代碼段 (Using T-SQL snippets in SSMS)

Now let’s see a simple example on how we can use a built-in snippet in SQL Server Management Studio.

現在,讓我們看一個簡單的示例,說明如何在SQL Server Management Studio中使用內置代碼段。

sql將截斷字符串或二進制數據。 If you right click in SQL Server Management Studio’s (SSMS) Query Window, then among other, you will be presented with the following two options:

如果在SQL Server Management Studio(SSMS)的“查詢”窗口中單擊鼠標右鍵,則會為您提供以下兩個選項:

  • Insert Snippet…

    插入代碼段…
  • Surround With…

    環繞…


sql數據庫交易代碼。 If you select the “Insert Snippet…” action, you are presented with the available snippet categories from which you can use the snippet to use.

如果選擇“插入代碼段…”操作,則會顯示可用的代碼段類別,您可以從中使用代碼段來使用。


sql偽代碼、 In this example I used the “Inline Table Function” snippet and here’s what I got in my query window:

在此示例中,我使用了“ Inline Table Function”代碼段,這是在查詢窗口中獲得的內容:


If you select the “Surround With…” action, you are then presented with the below built-in snippets:

如果選擇“環繞……”操作,則會顯示以下內置片段:


So if you have a statement in your query window (i.e. “SELECT * FROM tbl1”) and select it, and then you use the SurroundWith snippet “If”, your T-SQL statement will be automatically surrounded by an IF block.

因此,如果您在查詢窗口中有一條語句(即“ SELECT * FROM tbl1”)并選擇它,然后使用SurroundWith片段“ If”,則T-SQL語句將自動被IF塊包圍。


創建自定義的T-SQL代碼段–手動方式 (Creating custom T-SQL snippets – The manual way)

When I first checked out T-SQL snippets in SQL Server Management Studio 2012, I was excited as it was a long-awaited feature. However, right after the excitement, I wondered how it could be possible to create my own snippets. I found out that this was not very straightforward as in order to create a custom T-SQL snippet for SSMS you need to write XML code. Below you can see the XML template for creating a T-SQL snippet.

當我第一次在SQL Server Management Studio 2012中簽出T-SQL代碼片段時,我很興奮,因為它是期待已久的功能。 但是,在興奮之后,我想知道如何創建自己的摘錄。 我發現這并不是很簡單,因為要為SSMS創建自定義T-SQL代碼段,您需要編寫XML代碼。 在下面,您可以看到用于創建T-SQL代碼段的XML模板。


As you can see, even it’s not difficult, each time you want to create or modify a custom T-SQL snippet, you will need to write or modify XML code. For example, consider that we want to create the corresponding T-SQL snippet for the below T-SQL script:

如您所見,即使不難,每次您要創建或修改自定義T-SQL代碼段時,都需要編寫或修改XML代碼。 例如,考慮我們要為以下T-SQL腳本創建相應的T-SQL代碼段:


If we were about to create the snippet manually, we would have to write the below XML code:

如果要手動創建代碼段,則必須編寫以下XML代碼:


Even though I enjoy writing code, because the purpose of snippets is to make our life easier, I would prefer an easier way as well to create and manage my custom T-SQL Snippets. For this reason, within the context of my initiative SQLArtBits, I have developed a free tool called “Snippets Generator” which can be used for this exact purpose.

盡管我喜歡編寫代碼,但由于代碼片段的目的是使我們的生活更輕松,所以我也希望使用更簡單的方法來創建和管理自定義T-SQL代碼片段。 因此,在我的倡議SQLArtBits的背景下,我開發了一個免費的工具,稱為“ 片段生成器 ”,可用于此確切目的。

使用片段生成器創建自定義的T-SQL片段 (Creating custom T-SQL snippets with Snippets Generator)

Snippets Generator is a free, lightweight program which makes it easy for anyone to create a T-SQL snippet for SSMS 2012 or later.

Snippets Generator是一個免費的輕量級程序,它使任何人都可以輕松地為SSMS 2012或更高版本創建T-SQL代碼段。



As you can see in the above screenshot, Snippets Generator provides a convenient GUI environment in which you just need to complete 5 simple fields:

從上面的屏幕截圖中可以看到,Snippets Generator提供了一個便捷的GUI環境,您只需完成5個簡單的字段即可:

  • Title: The title of your custom T-SQL snippet. 標題 :您的自定義T-SQL代碼段的標題。
  • Description: Your custom T-SQL snippet’s description. Description :您的自定義T-SQL代碼段的描述。
  • Author: The snippet’s author name. 作者 :摘錄的作者姓名。
  • Snippet Type: The snippet’s type, meaning whether is an “Expansion” or “SurroundsWith” snippet.
  • 代碼類型 :代碼段的類型,即是“擴展”代碼段還是“ SurroundsWith”代碼段。
  • Snippet T-SQL Code: As the name implies, in this textbox you write the T-SQL statement that you want to be included in the snippet. 片段T-SQL代碼:顧名思義,在此文本框中,您將編寫要包含在片段中的T-SQL語句。

In order to further assist you in the process of creating a new T-SQL snippet, Snippets Generator provides different code formatting options as well as the below additional functionality:

為了進一步幫助您創建新的T-SQL代碼段,Snippets Generator提供了不同的代碼格式設置選項以及以下附加功能:

  • Open existing SQL files in order to generate snippets based on the SQL files’ contents.

    打開現有SQL文件,以便根據SQL文件的內容生成摘要。
  • The ability to set a “Default Author” name in order not to have to manually enter it each time.

    可以設置“默認作者”名稱,而不必每次都手動輸入名稱。
  • A “Snippet Templates” library. This library contains built-in snippet templates which you can use them to create your own T-SQL snippets.

    “代碼段模板”庫。 該庫包含內置的片段模板,您可以使用它們來創建自己的T-SQL片段。
  • The ability to save your T-SQL snippet as template and thus add it to the Snippet Templates library for future use.

    可以將您的T-SQL代碼段另存為模板,然后將其添加到代碼段模板庫中以供將來使用。

Below you can see a screenshot of the Snippet Templates library.

您可以在下面看到Snippet Templates庫的屏幕截圖。



Now let’s create the snippet for the query of Listing 1 using Snippets Generator.

現在,使用片段生成器為清單1的查詢創建片段。



If you compare the two methods for creating the snippet, I’m sure you will agree with me that using Snippets Generator is a much easier and faster method for creating the snippet.

如果您比較兩種創建代碼段的方法,我相信您會同意我的觀點,即使用代碼段生成器是一種更輕松,更快速的代碼段創建方法。

Now let’s import the snippet into SSMS in order to verify its correctness.

現在,讓我們將代碼段導入SSMS,以驗證其正確性。



We will import the snippet in the snippets category ‘My Code Snippets’:

我們將在“我的代碼片段”片段中導入該片段:


Now that the snippet was imported in SSMS, let’s use it in a Query Window:

現在,該代碼段已導入SSMS中,讓我們在查詢窗口中使用它:





結論 (Conclusion)

Code reuse in SQL Server Management Studio with the use of T-SQL snippets is a handy way to be more productive with less effort. Snippets Generator is a free, lightweight program which makes it easy for anyone to create a T-SQL snippet. With Snippet Generator’s assistance, you can easily create snippets for your everyday T-SQL scripting needs, so that whenever you need to run certain queries, to just call them via the snippets functionality in SSMS. Therefore, with the use of snippets, you will no longer need to remember complex T-SQL queries, as you will just be able to use them via your custom T-SQL snippets.

使用T-SQL代碼段在SQL Server Management Studio中進行代碼重用是一種便捷而又省力的方法。 Snippets Generator是一個免費的輕量級程序,它使任何人都可以輕松創建T-SQL代碼段。 借助Snippet Generator的幫助,您可以輕松創建滿足您日常T-SQL腳本需求的代碼段,以便在需要運行某些查詢時,只需通過SSMS中的代碼段功能對其進行調用。 因此,使用代碼片段,您將不再需要記住復雜的T-SQL查詢,因為您將能夠通過自定義的T-SQL代碼片段使用它們。

翻譯自: https://www.sqlshack.com/how-to-create-and-manage-t-sql-code-snippets/

sql過濾代碼段

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/144165.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息