[英]How to pass the table variable to a stored procedure in SQL-Server 2014
[英]How to pass each value of the column from a table to a stored procedure and save the result in a table in SQL-Server
我有一個帶有用戶名字段的表,如下所示。 我有一個存儲過程,它接收用戶名參數並顯示結果。 目前,我正在通過指定參數為每個用戶名手動執行存儲過程,並在執行復制后將其結果粘貼到excel中。 用戶名表為:
╔════╦══════════╦══╗
║ ID ║ Username ║ ║
╠════╬══════════╬══╣
║ 1 ║ abc ║ ║
║ 2 ║ def ║ ║
║ 3 ║ ghi ║ ║
║ 4 ║ jkl ║ ║
║ 5 ║ mno ║ ║
║ 6 ║ xyz ║ ║
╚════╩══════════╩══╝
存儲過程為:
spCalculateSomeValuesForThePassedUsername 'abc'
存儲過程的輸出為:
╔════╦══════════╦═══════╗
║ ID ║ Username ║ Value ║
╠════╬══════════╬═══════╣
║ 1 ║ abc ║ 100 ║
╚════╩══════════╩═══════╝
我可以創建一個包含所有存儲過程的父存儲過程,並執行它以獲取結果,但是我仍然必須復制粘貼結果。 例如
CREATE PROCEDURE SPparent
AS
BEGIN
spCalculateSomeValuesForThePassedUsername 'abc'
spCalculateSomeValuesForThePassedUsername 'def'
spCalculateSomeValuesForThePassedUsername 'ghi'
spCalculateSomeValuesForThePassedUsername 'jkl'
.
.
.
END
我想知道是否可以獲取用戶名並將輸出自動粘貼到結果表中。
所需結果表
╔════╦══════════╦═══════╗
║ ID ║ Username ║ Value ║
╠════╬══════════╬═══════╣
║ 1 ║ abc ║ 100 ║
║ 2 ║ def ║ 200 ║
║ 3 ║ ghi ║ 150 ║
║ 4 ║ jkl ║ 300 ║
║ 5 ║ mno ║ 700 ║
║ 6 ║ xyz ║ 1000 ║
╚════╩══════════╩═══════╝
Declare @UserName NVarChar(128);
Create Table #Results(Id Int, UserName NVarChar(128), Value Int);
Declare curSO Cursor Local
Fast_Forward
Read_Only
For
Select UserName
From mySchema.myTable
;
Open curSO;
Fetch Next From curSO
Into @UserName;
While @@Fetch_Status = 0
Begin
Insert Into #Results
Exec spCalculateSomeValuesForThePassedUsername @UserName;
Fetch Next From curSO
Into @UserName;
End
Close curSO;
Deallocate curSO;
-- Or do something different here. I used a temporary table. You can use whatever
Select *
From #Results
一種這樣做的方式是這樣的-
CREATE PROCEDURE SPparent
AS
BEGIN
DECLARE @id INT, @UserName VARCHAR(100)
DECLARE @TempTable TABLE (id INT,UserName VARCHAR(100),Value INT)
DECLARE sample_cursor CURSOR FOR
SELECT ID,UserName
FROM <yourtable>
OPEN sample_cursor
FETCH NEXT FROM sample_cursor
INTO @id, @UserName
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO @TempTable
Exec spCalculateSomeValuesForThePassedUsername @UserName
FETCH NEXT FROM sample_cursor
INTO @id, @UserName
END
CLOSE sample_cursor
SELECT id,UserName,Value FROM @TempTable
END
因此,這里您遍歷表中的初始記錄,並為所有UserName調用sp spCalculateSomeValuesForThePassedUsername
,並將它們存儲在臨時表中。 完成后,您只需選擇臨時表即可以表格式獲取所有記錄。
這是一個簡短得多的面向集合的解決方案,它不使用循環或游標:
CREATE TABLE #Results(ID INT, Username SYSNAME, Value INT);
DECLARE @sql AS NVARCHAR(MAX) = N'';
SELECT @sql = @sql + N'
INSERT #Results EXEC spCalculateSomeValuesForThePassedUsername N'''+Username+''';'
FROM <yourTable>;
EXEC(@sql);
Select * from #Results;
另一個解決方案是使用類似while
循環。
DECLARE @id INT, @UserName VARCHAR(100)
SELECT TOP (1) @id = id ,@UserName = UserName
FROM myTable;
WHILE @@rowcount > 0
BEGIN
EXEC sp_executesql N'Exec spCalculateSomeValuesForThePassedUsername @UserName;'
,N'@UserName VARCHAR(100)'
,@UserName = @UserName;
SELECT TOP (1) @id = id ,@UserName = UserName
FROM myTable
WHERE id > @id;
END;
SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO
CREATE PROCEDURE spCalculateSomeValuesForThePassedUsername AS RETURN
GO
ALTER PROCEDURE spCalculateSomeValuesForThePassedUsername (
@Username varchar(255)
)
AS
SELECT @Username, 0
GO
CREATE PROCEDURE spParent AS RETURN
GO
ALTER PROCEDURE spParent AS
BEGIN
SET NOCOUNT ON
DECLARE @t AS table (id int IDENTITY(1,1), Username varchar(255), Value int)
DECLARE @num int = 0
DECLARE @Username varchar(255)
DECLARE @ret int = 1
-- DROP TABLE #Users
CREATE TABLE #Users (
id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
Username varchar(255) NOT NULL
)
INSERT INTO #Users (Username)
SELECT UserName FROM (VALUES ('abc'), ('def'), ('ghi')) AS t (UserName)
WHILE @ret > 0
BEGIN
IF @UserName IS NOT NULL
INSERT INTO @t (Username, Value)
EXEC sp_executesql N'Exec spCalculateSomeValuesForThePassedUsername @Username;', '@Username varchar(255)', @Username = @Username;
SELECT TOP(1) @Username = UserName, @num = id FROM #Users WHERE id > @num ORDER BY id ASC
SET @ret = @@ROWCOUNT
END
SELECT * FROM @t
END
GO
EXEC spParent
GO
DROP PROCEDURE spParent
GO
DROP PROCEDURE spCalculateSomeValuesForThePassedUsername
GO
SET QUOTED_IDENTIFIER OFF
SET ANSI_NULLS ON
GO
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.