Sql server 带select as参数的Exec存储过程

Sql server 带select as参数的Exec存储过程,sql-server,stored-procedures,exec,Sql Server,Stored Procedures,Exec,我在sql server上有一个存储过程,它接受2个参数。我想为select返回的每一行运行stp CREATE PROCEDURE stpMySTP @param1, @param2 AS BEGIN --select something from database regarding this parameters END 我想运行这个stp并传递select语句返回的@param2的参数列表 比如: EXEC stpMySTP '',(SELECT ....) 有什么想法吗

我在sql server上有一个存储过程,它接受2个参数。我想为select返回的每一行运行stp

CREATE PROCEDURE stpMySTP @param1, @param2
AS BEGIN
   --select something from database regarding this parameters
END
我想运行这个stp并传递select语句返回的@param2的参数列表

比如:

   EXEC stpMySTP '',(SELECT ....)

有什么想法吗?

如果我没看错这个问题,我可能不会::

这是其中一种可能的解决方案,光标就是其中之一。但是,游标通常是糟糕的解决方案

现在是光标:<

Use Northwind

GO


declare @PROC_fetch_status int


declare @CustomerID varchar(12)
declare @ContactName varchar(24)


--    Select * from Customers
DECLARE curCustomers CURSOR FAST_FORWARD FOR select CustomerID , ContactName
from Customers Order by ContactName
OPEN curCustomers


-- Perform the first fetch.
fetch curCustomers into @CustomerID , @ContactName


select @PROC_fetch_status = @@fetch_status

    IF @PROC_fetch_status <> 0
 begin
  print 'No Records'
 end

    WHILE @PROC_fetch_status = 0
    BEGIN

        print 'Current Customer  = ' + @CustomerID + '  :  ' + @ContactName
        print '------------------------------------'
        print ''
        EXEC dbo.uspCustomerGetSingleWithOrders @CustomerID
        print ''
        print ''

        FETCH NEXT FROM curCustomers INTO @CustomerID , @ContactName
        select @PROC_fetch_status = @@fetch_status

    END
CLOSE curCustomers
DEALLOCATE curCustomers
但我建议大家读一读基于场景而不是一行一行的令人痛苦的行哲学。
如果您感兴趣,请留下一张便条。

如果我没有看错这个问题,我可能不会::

这是其中一种可能的解决方案,光标就是其中之一。但是,游标通常是糟糕的解决方案

现在是光标:<

Use Northwind

GO


declare @PROC_fetch_status int


declare @CustomerID varchar(12)
declare @ContactName varchar(24)


--    Select * from Customers
DECLARE curCustomers CURSOR FAST_FORWARD FOR select CustomerID , ContactName
from Customers Order by ContactName
OPEN curCustomers


-- Perform the first fetch.
fetch curCustomers into @CustomerID , @ContactName


select @PROC_fetch_status = @@fetch_status

    IF @PROC_fetch_status <> 0
 begin
  print 'No Records'
 end

    WHILE @PROC_fetch_status = 0
    BEGIN

        print 'Current Customer  = ' + @CustomerID + '  :  ' + @ContactName
        print '------------------------------------'
        print ''
        EXEC dbo.uspCustomerGetSingleWithOrders @CustomerID
        print ''
        print ''

        FETCH NEXT FROM curCustomers INTO @CustomerID , @ContactName
        select @PROC_fetch_status = @@fetch_status

    END
CLOSE curCustomers
DEALLOCATE curCustomers
但我建议大家读一读基于场景而不是一行一行的令人痛苦的行哲学。
如果您感兴趣,请留下一张便条。

我建议您:

如果参数列表非常简单,那么使用字符串@param2。比如“1 | 2 | 100500 |”。然后使用过程拆分字符串并使用这些参数 如果您的过程返回一个值,则转到存储函数并在SELECT语句中使用函数调用 使用一个专用表,您可以在其中推送感兴趣的值。比如SessionID、paramId、paramValue。在thios的情况下,@param2是这个额外表中的SessionId
我建议如下:

如果参数列表非常简单,那么使用字符串@param2。比如“1 | 2 | 100500 |”。然后使用过程拆分字符串并使用这些参数 如果您的过程返回一个值,则转到存储函数并在SELECT语句中使用函数调用 使用一个专用表,您可以在其中推送感兴趣的值。比如SessionID、paramId、paramValue。在thios的情况下,@param2是这个额外表中的SessionId
解决方案的可能副本将起作用。非常慢,我有53601张唱片,但我认为它会起作用。同时,我创建了字符串形式的执行,并使用了2个临时表。是的。因此,我最初的注释游标通常是糟糕的解决方案。您的解决方案将起作用。非常慢,我有53601张唱片,但我认为它会起作用。同时,我创建了字符串形式的执行,并使用了2个临时表。是的。因此,我最初的注释游标通常是糟糕的解决方案。