如何从存储的进程中将第二个表数据插入临时表 [英] How to insert second table data into temporary table from stored proceedure

查看:79
本文介绍了如何从存储的进程中将第二个表数据插入临时表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



当存储过程中的两个表时,如何将第二个表数据插入临时表。



示例代码:



创建表#temp(数字int)



插入#temp(数字)



exec proc_testing



其中proc_testing将两个表重新作为结果集。



i希望将结果中的第二个表结果插入到临时表中



什么我试过了:



创建表#temp(数字int)



插入#temp(数字)



exec proc_testing

Hi all,

How to insert second table data into temporary table when two tables as a result from stored proceedure.

Sample Code:

Create table #temp(number int)

insert into #temp(number)

exec proc_testing

where as proc_testing retruns two tables as result set .

i want to insert second table result from the result in to temporary table

What I have tried:

Create table #temp(number int)

insert into #temp(number)

exec proc_testing

推荐答案

第二个表格将被插入到#temp中您拥有的代码。



请注意,这仅适用于两个记录集都有匹配列的情况。



例如(使用Microsoft的Northwind数据库)

The second table will be inserted into #temp with the code you have.

Note that this will only work if both record sets have matching columns.

For example (using Microsoft's Northwind database)
CREATE PROCEDURE [dbo].[GetAllNames] 
AS 
    SELECT ContactName FROM dbo.Customers
    SELECT FirstName + ' ' + LastName FROM dbo.Employees

第一个查询返回91条记录,第二条返回9.



如果我像这样运行SP

The first query returns 91 records and the second returns 9.

If I run the SP like this

create table #temp([name] varchar(125))

insert into #temp
exec GetAllNames



然后发出


and then issue

select * from #temp

将返回100条记录。



如果我更改SP

100 records will be returned.

If I change the SP

ALTER PROCEDURE [dbo].[GetAllNames] 
AS 
    SELECT ContactName FROM dbo.Customers
    SELECT FirstName,LastName FROM dbo.Employees -- note 2 columns

并尝试运行它我收到错误

and attempt to run it I get an error

Quote:

Msg 213,Level 16,State 7,Procedure GetAllNames,Line 11

列名或者提供的值的数量与表定义不匹配。

Msg 213, Level 16, State 7, Procedure GetAllNames, Line 11
Column name or number of supplied values does not match table definition.



您无法在T-SQL中单独访问结果集,但可以将结果加载到数据中在C#或VB.NET中设置并以这种方式单独访问表


You can't access the resultsets separately in T-SQL, but you can load the results into a DataSet in C# or VB.NET and access the tables separately that way


这篇关于如何从存储的进程中将第二个表数据插入临时表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆