SQL查询和存储过程未产生期望的结果

编程入门 行业动态 更新时间:2024-10-21 14:19:00
本文介绍了SQL查询和存储过程未产生期望的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有此表并存储了proc函数:

I have this table and Stored proc function:

表:

CREATE TABLE _DMigNumbers( Number numeric(20,0) NOT NULL PRIMARY KEY ); INSERT INTO _DMigNumbers VALUES(0)

存储的proc函数:

CREATE FUNCTION read_and_increment() RETURNS NUMERIC(20,0) BEGIN DECLARE @number_just_read NUMERIC(20,0); SELECT number INTO @number_just_read FROM _DMigNumbers; UPDATE _DMigNumbers SET number = number + 1; RETURN @number_just_read; End

我也创建了这个Numbers表

and I create this Numbers table as well

CREATE TABLE _Numbers ( Number int NOT NULL PRIMARY KEY ); INSERT INTO _Numbers VALUES(1) INSERT INTO _Numbers VALUES(2) INSERT INTO _Numbers VALUES(3) INSERT INTO _Numbers VALUES(4)

现在:

当我这样做时:

select f.Number ,read_and_increment() from _Numbers f

我得到:

Number-----Value 1 0 2 0 3 0 4 0

我想要不同的值,例如(0,1,2,3)-我需要怎么做才能实现这一目标?

I want different value like (0,1,2,3) - what do I need to do to achieve this?

我了解由于使用单个Select,我得到了相同的值,但是不确定要做什么才能得到我现在想要的东西。 ...

I understand that I am getting the same values because of the single Select, but not sure what I need to do to get what I am after at the moment......

我无法使用IDENTITY或自动递增,请参见我以前的问题,如果有兴趣的话,可以了解更多详细信息...

I cannot use IDENTITY or autoincrement see my previous question for more details if interested...

谢谢

Voodoo

推荐答案

尝试将函数标记为 NOT确定性,看看是否有帮助。默认情况下,所有功能都是确定性的,这意味着数据库服务器可以在某些情况下缓存结果。以此方式进行标记将强制服务器每次重新评估查询/功能。

Try marking your function as NOT DETERMINISTIC and see if that helps. By default, all functions are deterministic, which means the database server can cache the result under certain circumstances. Marking it this way will force the server to re-evaluate the query/function each time.

CREATE FUNCTION read_and_increment() RETURNS NUMERIC(20,0) NOT DETERMINISTIC BEGIN DECLARE @number_just_read NUMERIC(20,0); SELECT number INTO @number_just_read FROM _DMigNumbers; UPDATE _DMigNumbers SET number = number + 1; RETURN @number_just_read; End

更多推荐

SQL查询和存储过程未产生期望的结果

本文发布于:2023-10-13 10:16:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1487671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:存储过程   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!