我有三个不同的SQL脚本,我能够执行它们并获得所需的结果。我想将它们全部放入Temp Table中,并将其用作存储过程,并获取临时表上显示的数据。以下是我创建的程序。
我想知道如何通过执行SP来创建临时表并将此数据插入其中。
CREATE PROCEDURE [dbo].[sp_SurveyTracking]
AS
BEGIN
SET NOCOUNT ON;
-- AVAILABLE
SELECT DISTINCT P.Clinic
, P.LastName
, P.FirstName
, SA.SI AS SID
, ser.SD
-- , 'Available' as Status
, ss.SDate
, ss.SDtTm
FROM
Table_Name SA
Inner join Schedule ss on sa.SIID = ss.SIID
Inner join Patient p on p.PID = ss.PID
Inner join Clinical.SSJun ssj on ssj.SSJID = ss.SSJID
Inner join Clinical.SS ser on ser.SSID = ssj.SSID
WHERE
SS.SDtTm IS NOT NULL
AND (SS.SDtTm < SS.SDate)
AND (SS.SDate > GETDATE())
AND SS.Canc = 0
AND sa.AD = 'Completed'
AND GETDATE() > ss.SDate
Order by p.CID;
--COMPLETED
SELECT DISTINCT p.Clinic
, p.LastName
, p.FirstName
, ser.SD
--, 'Completed' as Status
, ss.SDate
FROM
Table_Name SA
Inner join Schedule ss on sa.SIID = ss.SIID
Inner join Patient p on p.PID = ss.PID
Inner join Clinical.SSJun ssj on ssj.SSJID = ss.SSJID
Inner join Clinical.SS ser on ser.SSID = ssj.SSID
WHERE
sa.AD = 'Completed' and ss.Cancelled = 0
order by p.CID;
-- OVERDUE
SELECT DISTINCT p.CD
,p.LastName
,p.FirstName
,sa.SIID
,ser.SD
,'Overdue' as Status
,ss.SDate
,GetDate() as CurrentDate
FROM
Table_Name SA
Inner join Schedule ss on sa.SIID = ss.SIID
Inner join Patient p on p.PID = ss.PID
Inner join Clinical.SSJun ssj on ssj.SSJID = ss.SSJID
Inner join Clinical.SS ser on ser.SSID = ssj.SSID
WHERE
GetDate() > ss.SDate and ss.Cancelled = 0
order by p.CID;
END
答案 0 :(得分:0)
您可以在开头定义#table,然后使用
INSERT INTO
将值添加到其中。
类似的东西:
CREATE TABLE #tmp
(
SomeFields VARCHAR(MAX)
);
INSERT INTO #tmp
SELECT * FROM table2
如果您喜欢答案,请勾选勾选按钮。
感谢。
答案 1 :(得分:0)
要将Storec过程中的数据插入表/临时表,首先需要创建表定义
以下是您可以做的样本。
create proc dbo.test
as
select 1 as b
create table #tmp(b int)
insert into #tmp exec dbo.test