我正在研究SP,我想在结果集中添加一列。通常这不会有问题,但在这里我使用Exec来填充一个临时表。对于那个临时表,我想添加一列。
某些prestuff将数据放入其中一个具有某些条件的临时表中
declare @RowCount int
set @RowCount = 1
create table #Temp_HS (row int IDENTITY (1, 1) NOT NULL, h varchar(30))
Create table #tmpS (K varchar(100),
U varchar(100), Counter int, H varchar(100))
--Puts data in one temp_table with employees
insert into #Temp_HS (h)
select Login from database.dbo.Users
where Old <> 1
and TC in ('A_1', 'A_2')
and Login not in ('Steve', 'Peter', 'Gabs')
--Declaring my counter here, it sets the MaxRow which is 19 in this case
declare @Counter int
set @Counter = (select Max(row) from #Temp_HS)
select * from #Temp_HS
-- Looping, That my RowCount must be less or Equal to Counter which is 19.
while @RowCount <= @Counter
begin
设置最初来自数据库登录的用户,在临时表中声明为H.
declare @user varchar(30)
select @user = h from #Temp_HS where row = @RowCount
这是棘手的部分,这是将3列插入临时的存储过程 table,这里我想添加一个colum,在这种情况下是从Temp_HS到结果集的h。
INSERT INTO #tmpS
EXEC Database.dbo.getListCount @user,
param,
param,
param,
'something',
param
set @RowCount = @RowCount +1
end
drop table #Temp_HS
如果您需要任何进一步的信息,请询问! :)
基本上我想在我的Exec SP的结果中再添加一列,将结果插入temp_table
答案 0 :(得分:1)
INSERT INTO .. EXEC
要求您插入的表已存在,例如
-- Given this preexisting proc
CREATE PROC dbo.getListCount @user INT, -- other params
AS
SELECT @User as Col1,
'SomeVarChar' as Col2
FROM [SomeTable];
-- In your code, create the temp table to hold the data
CREATE TABLE #tmpS
(
Col1 INT,
Col2 NVARCHAR(100),
NewColumnH VARCHAR(30) -- Add the additional column up front
-- etc.
);
这称为
INSERT INTO #tmpS(Col1, Col2)
EXEC dbo.getListCount, @User;
如果您需要在临时表上进行进一步处理,请在PROC调用后执行此操作:
UPDATE ts
SET NewColumnH = t.h
FROM #tmpS ts INNER JOIN #Temp_HS th on th.row = @RowCount;
答案 1 :(得分:0)
实际上内部连接在临时表上不能正常工作,这就是我使用此解决方案的原因。由于我已经在变量中使用@User,因此我选择执行此更新。
UPDATE ts
SET NewColumnH = @User
FROM #tmpS ts
where ts.Column is null