sql 函数执行动态sql_为什么sql函数会引发此错误

sql 函数执行动态sql

You shouldn't go through the bitter experience I had while learning to write a function query in SQL. Before I finally found out that a parenthesis was what took me time, I’d looked hard at the query I wrote for a long time.

您不应经历在学习用SQL编写函数查询时遇到的痛苦经历。 在我最终发现括号是花了我很多时间之前,我认真研究了很长时间的查询。

In this article, through query samples, you will learn how to write a function in SQL with or without a parameter. I pinpointed that simple error that wasted my time.

在本文中,通过查询示例,您将学习如何在SQL中编写带有或不带有参数的函数。 我查明了浪费时间的那个简单错误。

The below query is a function without a parameter that sums up the numbers of sales in a sales column. I used AdvertureWorks 2012 database — Sales.SalesTerritory table — in this tutorial.

以下查询是一个不带参数的函数,该参数求和了sales列中的销售数量。 在本教程中,我使用了AdvertureWorks 2012数据库— Sales.SalesTerritory表。

Note: you can use any other database to follow this tutorial.

注意:您可以使用任何其他数据库来学习本教程。

如何在SQL中创建不带参数的函数 (How to Create a Function Without a Parameter in SQL)

Line one creates a function then named the function “YTDSALES()”. You can give your function any name. Remember to add parenthesis to the name of the function when without a parameter.

第一行创建一个函数,然后将该函数命名为“ YTDSALES()”。 您可以给函数起任何名字。 没有参数时,请记住在函数名称上添加括号。

In the next line, write the word “RETURNS” followed by any name you want. I used “MONEY” here since I want to get the total sum of money. Write the keywords “AS” and “BEGIN” next.

在下一行中,输入单词“ RETURNS”,后跟您想要的任何名称。 我在这里使用了“金钱”,因为我想得到总金额。 接下来,写关键词“ AS”和“ BEGIN”。

“DECLARE @YTDSALES AS MONEY” follows in line 5. You can use “@” with any word you choose and not necessarily “@YTDSALES”.

第5行显示“ DECLARE @YTDSALES AS MONEY”。您可以在选择的任何单词中使用“ @”,而不必使用“ @YTDSALES”。

Start the sixth line with “SELECT @YTDSALES =” followed by the column, (SalesYTD), you want to sum up having “SUM” before the bracket. You should include the table (Sales.SalesTerritory) that contains the column (SalesYTD).

第六行以“ SELECT @YTDSALES =”开头,然后是(SalesYTD)列,您想要总结一下在括号前有“ SUM”。 您应该包括包含列(SalesYTD)的表(Sales.SalesTerritory)。

Then write “RETURN @YTDSALES” and “END” to complete the function. Run your query. You will see “Commands completed successfully.” down on your screen.

然后输入“ RETURN @YTDSALES”和“ END”以完成功能。 运行查询。 您将看到“命令已成功完成。” 在屏幕上。

You just created a function. To run your function, run the query below:

您刚刚创建了一个函数。 要运行您的函数,请运行以下查询:

In the first line above, you can give “@RESULT” any name.

在上面的第一行中,您可以给“ @RESULT”指定任何名称。

Remember to include “dbo.” to the name of your function. In this instance, it is dbo.YTDSALES(). Check the first line while creating your function to know the name of your function. Here, YTDSALES() is the name of the function.

请记住包括“ dbo”。 功能名称。 在这种情况下,它是dbo.YTDSALES()。 创建函数时检查第一行,以了解函数名称。 在这里,YTDSALES()是函数的名称。

Write “PRINT @RESULT” as your last line of query.

输入“ PRINT @RESULT”作为最后一行查询。

Run the query and you will get the total sum of numbers in your specified column.

运行查询,您将在指定的列中获得总数的总和。

如何在SQL中使用参数创建函数 (How to Create a Function With a Parameter in SQL)

A function with a parameter makes it possible to extract an exact information straightaway. Writing a function with a parameter is almost the same with a function without a parameter. The differences between the two are pointed out below.

具有参数的函数可以直接提取准确的信息。 使用参数编写函数与不使用参数编写函数几乎相同。 下文指出了两者之间的区别。

Run the above query first to create a function.

首先运行上面的查询以创建一个函数。

Run this query to see the result:

运行此查询以查看结果:

One of the differences between a SQL function with a parameter and that without a parameter is a parenthesis — (). Note the first line of the query above, unlike a query function with a parenthesis, it has no parenthesis! If you mistakenly add a parenthesis, you will get this error message:

带参数SQL函数与不带参数SQL函数之间的区别之一是括号()。 请注意,上面查询的第一行与带括号的查询函数不同,它没有括号! 如果您错误地添加了括号,则会收到以下错误消息:

To experience the error yourself, add parenthesis to the first line of the function created above.

要亲自体验该错误,请在上面创建的函数的第一行添加括号。

The other differences are the words “GROUP” and “Pacific” used here. For clarification, “GROUP” is the name of the column, and “Pacific” is one of the names of places in the “GROUP” column you want to get data from.

其他区别是此处使用的“ GROUP”和“ Pacific”这两个词。 为了澄清起见,“ GROUP”是列的名称,“ Pacific”是要从中获取数据的“ GROUP”列中的位置名称之一。

When the query is in order — without parenthesis, the created function with the parameter gives the total number in the “Pacific” area.

当查询按顺序进行时-不带括号,带有参数的已创建函数将给出“太平洋”区域中的总数。

结论 (Conclusion)

The differences between a function with a parameter and that without a parameter are slight. The first line of a function without a parameter ends with an empty parenthesis. The first line of a function with a parameter doesn’t have a parenthesis. Note this difference and the other differences mentioned in this article to avoid spending hours debugging a function query.

具有参数的功能与不具有参数的功能之间的差异很小。 没有参数的函数的第一行以空括号结尾。 具有参数的函数的第一行没有括号。 请注意此区别和本文中提到的其他区别,以避免花费大量时间调试函数查询。

翻译自: https://towardsdatascience.com/why-sql-functions-raise-this-error-494cc4de5666

sql 函数执行动态sql

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值