Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何为select语句中的每一行执行存储过程?_Sql_Sql Server_Stored Procedures - Fatal编程技术网

Sql 如何为select语句中的每一行执行存储过程?

Sql 如何为select语句中的每一行执行存储过程?,sql,sql-server,stored-procedures,Sql,Sql Server,Stored Procedures,我有一个返回唯一Id的存储过程。我需要调用此sp以获取每行的唯一Id。我必须使用此SP,因为应用程序也使用此SP 如何为每行选择从SP返回的ID CREATE procedure [dbo].[SelectNextNumber] @TableName nvarchar(255) as begin declare @NewSeqVal int set NOCOUNT ON update Number --This is a table that holds for each t

我有一个返回唯一Id的存储过程。我需要调用此sp以获取每行的唯一Id。我必须使用此SP,因为应用程序也使用此SP

如何为每行选择从SP返回的ID

CREATE procedure [dbo].[SelectNextNumber]

@TableName nvarchar(255)
as
begin

  declare @NewSeqVal int

  set NOCOUNT ON

  update Number --This is a table that holds for each table the max ID

  set @NewSeqVal = Next = Next + Increase

  where TableNaam= @TableName

  if @@rowcount = 0 
  begin
        Insert into Number VALUES (@TableName, 1, 1) 
        return 1
  end

  return @NewSeqVal
数字表:

CREATE TABLE [dbo].[Number](
    [TableName] [varchar](25) NOT NULL,
    [Next] [int] NULL,
    [Increase] [int] NULL

我知道While循环可以用于此目的,但在我的情况下,我不知道如何使用While循环。

不能在SELECT语句中使用存储过程,只能使用函数。 如果确实必须使用存储过程,则可以使用光标在结果集上迭代:

编辑: 老实说,我不太确定你是否理解了你真正需要的东西,看起来你正在自己建立一个身份(); 不过,如果确实需要运行游标,下面是一个使用存储过程的示例:


不能在SELECT语句中使用存储过程,只能使用函数。 如果确实必须使用存储过程,则可以使用光标在结果集上迭代:

编辑: 老实说,我不太确定你是否理解了你真正需要的东西,看起来你正在自己建立一个身份(); 不过,如果确实需要运行游标,下面是一个使用存储过程的示例:


将单数插入。。分开选择:

暂时存储选择结果

 declare @rc int, @NewSeqVal int;
 SELECT ..
   INTO #tmp -- add this
   FROM ..
存储行数并获取那么多的数字

 set @rc = @@rowcount;
您必须直接使用SP中的代码:

 update Number --This is a table that holds for each table the max ID
 set @NewSeqVal = Next = Next + @rc
 where TableNaam= 'sometbl';
最后,插入

 INSERT ... 
 SELECT ID = @NewSeqVal + 1 - row_number() over (ORDER BY col1)
       , {all the other columns}
 FROM #tmp;

orderbycol1
是任意的,选择一些合理的,或者如果你不在乎的话,让它成为
orderbynewid()

将单数插入。。分开选择:

暂时存储选择结果

 declare @rc int, @NewSeqVal int;
 SELECT ..
   INTO #tmp -- add this
   FROM ..
存储行数并获取那么多的数字

 set @rc = @@rowcount;
您必须直接使用SP中的代码:

 update Number --This is a table that holds for each table the max ID
 set @NewSeqVal = Next = Next + @rc
 where TableNaam= 'sometbl';
最后,插入

 INSERT ... 
 SELECT ID = @NewSeqVal + 1 - row_number() over (ORDER BY col1)
       , {all the other columns}
 FROM #tmp;

orderbycol1
是任意的,选择一些合理的,或者如果你不在乎的话,让它成为
orderbynewid()

如果你能帮助它,尽量避免循环;基于集合的运算通常要快很多。我建议查看
Row\u Number()
或排名函数来生成序列号,但我不确定您在这里想要实现什么;听起来好像你在试图重新发明标识列。如果你能帮助的话,可能会重复尝试避免循环;基于集合的运算通常要快很多。我建议查看
Row\u Number()
或排名函数来生成序列号,但我不确定您在这里想要实现什么;听起来你在试图重新发明身份栏。可能是我尝试过的重复,但我不知道如何。。你能举个例子说明我的情况吗?@Ahmet:MSDN网站上有很多关于SQL Server的内容……我试过了,但我不知道如何。。你能举个例子说明我的情况吗?@Ahmet:MSDN网站上有很多关于SQL Server的内容。。。。