thử này (con trỏ vòng lặp miễn phí):
CREATE TABLE #Results (RowID int identity(1,1), Col1 varchar(5), Col2 int, ...)
DECLARE @Current int
,@End int
DECLARE @Col1 varchar(5)
,@Col2 int
,...
--you need to capture the result set from the primary stored procedure
INSERT INTO #Results
(Col1, COl2,...)
EXEC nameofstoredprocedure_1 arg1, arg2, arg3
SELECT @[email protected]@ROWCOUNT,@Current=0
--process each row in the result set
WHILE @Current<@End
BEGIN
SET @[email protected]+1
SELECT
@Col1=COl1, @Col2=Col2
FROM #Results
WHERE [email protected]
--call the secondary procedure for each row
EXEC nameofstoredprocedure_2 @Col1, @Col2,...
END
dụ làm việc:
CREATE PROCEDURE nameofstoredprocedure_1
(@arg1 int, @arg2 int, @arg3 int)
AS
SELECT 'AAA',@arg1 UNION SELECT 'BBB',@arg2 UNION SELECT 'CCC',@arg3
GO
CREATE PROCEDURE nameofstoredprocedure_2
(@P1 varchar(5), @P2 int)
AS
PRINT '>>'+ISNULL(@P1,'')+','+ISNULL(CONVERT(varchar(10),@P2),'')
GO
CREATE TABLE #Results (RowID int identity(1,1), Col1 varchar(5), Col2 int)
DECLARE @Current int
,@End int
DECLARE @Col1 varchar(5)
,@Col2 int
INSERT INTO #Results
(Col1, COl2)
EXEC nameofstoredprocedure_1 111, 222, 333
SELECT @[email protected]@ROWCOUNT,@Current=0
WHILE @Current<@End
BEGIN
SET @[email protected]+1
SELECT
@Col1=COl1, @Col2=Col2
FROM #Results
WHERE [email protected]
EXEC nameofstoredprocedure_2 @Col1, @Col2
END
OUTPUT:
(3 row(s) affected)
>>AAA,111
>>BBB,222
>>CCC,333
Tôi có thể chuyển liên kết eggheadcafe.com sang mã của mình. thanks –
Bài báo egghead đã làm được điều đó! – PhilNicholas
Câu trả lời này đã bị gắn cờ để xóa vì đó là câu trả lời chỉ liên kết có liên kết bị hỏng. Bạn có thể vui lòng mở rộng câu trả lời để giải quyết đầy đủ câu hỏi mà không yêu cầu người đọc nhấp qua liên kết không? – josliber