我有一个名为(p_Overview)
的存储过程,它需要一个参数ID
,它基本上会进行一些计算,并返回带有数字和总数的4列。
我想编写另一个名为(p_Date)
的存储过程,它使用p_Overview
和4列。我还想使用ID
参数进行一些计算。
我首先创建了一个临时表并执行:INSERT INTO #temp exec p_Overview
。然后我不知道还能做些什么。
答案 0 :(得分:0)
您可以在新SP中执行以下操作,以便使用现有SP的结果集:
Insert into #table exec p_Overview @Id OUTPUT
用法:
ALTER PROC [p_Date]
AS
BEGIN
Insert into #overviewTempTable exec p_Overview @Id OUTPUT
Select * from #overviewTempTable
--Do some other stuff here
END
通过这种方式,您可以使用#overviewTempTable
临时表,并在新SP中执行所需操作。
答案 1 :(得分:0)
根据我的理解,你试图从另一个程序调用参数化的proc。然后这个样本会帮助你。
内部程序:
ALTER PROCEDURE InnerPROC
@Num int = 0
AS
BEGIN
SET NOCOUNT ON;
DECLARE @TAB TABLE(ID INT)
DECLARE @I INT = 0
WHILE(@I<4)
BEGIN
INSERT INTO @TAB VALUES(@I)
SET @I = @I + 1
END
SELECT ID FROM @TAB WHERE ID < @Num
END
外部程序:
ALTER PROCEDURE OuterPROC
AS
BEGIN
SET NOCOUNT ON;
DECLARE @TAB TABLE (ID INT)
INSERT INTO @TAB
EXEC InnerPROC
@Num = 5
SELECT ID FROM @TAB
END
答案 2 :(得分:0)
从您的查询我明白我实现了这一点。 (在SQL Server 2008上) 请参考下文。
- =============================================
- 创建表学生数据(stu_data)
- =============================================
USE [testDb] GO
/ ** 对象:表格[dbo]。[stu_data] ** / SET ANSI_NULLS ON GO
SET QUOTED_IDENTIFIER ON GO
SET ANSI_PADDING ON GO
CREATE TABLE [dbo]。[stu_data](
[id] [int] NOT NULL,
[name] [varchar](50) NULL,
[age] [int] NULL,
[marks1] [int] NULL,
[marks2] [int] NULL,
CONSTRAINT [PK_stu_data] PRIMARY KEY CLUSTERED ( [id] ASC )WITH(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY] )[主要]
GO
设置ANSI_PADDING OFF GO
- =============================================
- 从学生表(stu_data)获取数据的存储过程
- =============================================
USE [testDb]
GO
/ * 对象:StoredProcedure [dbo]。[selectdata] * /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
创建PROCEDURE [dbo]。[selectdata](@sid int)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
设置NOCOUNT ON;
-- Insert statements for procedure here
SELECT id,name,marks1,marks2,(marks1 + marks2) as total, (marks1 + marks2)/2 as average from stu_data where id = @sid;
END
- =============================================
- 用于更新学生表(stu_data)数据的存储过程
- =============================================
USE [testDb]
GO
/ * 对象:StoredProcedure [dbo]。[updatedata] * /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo]。[updatedata](@sid int)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Declare @data table (id int, name varchar(50) , marks1 int, marks2 int, total int, avrg int)
Declare @fid int
Insert into @data execute selectdata @sid;
Select @fid = id from @data;
Select * from @data;
Update stu_data set marks1 = 12, marks2 = 25 where id = @fid;
Select * from stu_data where id = @fid;
END
- =============================================
- 执行update proedure(stu_data)
- =============================================
使用testDb;
执行updatedata 2;