Reputation: 814
I want to display the result of a stored procedure in a temporary table.
I have the following code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[insertpropertylisting]
@propertyname VARCHAR(150),
@propertyaddress VARCHAR(250),
@propertyprice MONEY,
@availableunits VARCHAR(100),
@propertyid INT OUTPUT
AS
BEGIN
INSERT INTO propertylisting (propertyname, propertyaddress, propertyprice, availableunits)
VALUES (@propertyname, @propertyaddress, @propertyprice, @availableunits)
PRINT @propertyname
SELECT @propertyaddress AS 'address'
SET @propertyid = SCOPE_IDENTITY()
SELECT
@propertyname = propertyname,
@propertyaddress = propertyaddress,
@propertyprice = propertyprice,
@availableunits = availableunits
FROM
propertylisting
WHERE
property_id = @propertyid
RETURN @propertyid
END
CREATE TABLE #propertylisting
(
propertyname VARCHAR(150),
propertyaddress VARCHAR(250),
propertyprice MONEY,
availableunits VARCHAR(100),
propertyid INT
)
INSERT INTO #propertylisting
EXEC [dbo].[insertpropertylisting]
SELECT
propertyname, propertyaddress, propertyprice, availableunits
FROM
#propertylisting
DROP TABLE #propertylisting
I am not getting the output as table.
Upvotes: 1
Views: 2032
Reputation: 14209
A few problems that I can see:
SELECT
in the middle of SP that you don't want (also a PRINT
).ALTER PROCEDURE
command.INSERT
statements, and specially when doing INSERT INTO EXEC
.First make some changes to the SP; remove unused statements and parameters (including the OUT
property of the parameter since it seems you aren't using it). You can return inserted values with the OUTPUT clause of the INSERT
statement, including computed and IDENTITY
columns.
ALTER procedure [dbo].[insertpropertylisting]
@propertyname varchar(150),
@propertyaddress varchar(250),
@propertyprice money,
@availableunits varchar(100)
as
BEGIN
insert into propertylisting (
propertyname,
propertyaddress,
propertyprice,
availableunits)
OUTPUT
inserted.propertyname,
inserted.propertyaddress,
inserted.propertyprice,
inserted.availableunits,
inserted.property_id -- Can return IDENTITY and computed columns
values(
@propertyname,
@propertyaddress,
@propertyprice,
@availableunits)
END
After you run this ALTER
, you can bring inserted records with a INSERT INTO EXEC
. Make sure to pass proper values to the SP.
Create table #propertylisting
(
propertyname varchar(150),
propertyaddress varchar(250),
propertyprice money,
availableunits varchar(100),
propertyid int
)
Insert into #propertylisting (
propertyname,
propertyaddress,
propertyprice,
availableunits,
propertyid)
Exec [dbo].[insertpropertylisting]
@propertyname = 'Value',
@propertyaddress = 'Value',
@propertyprice = 999,
@availableunits = 'Value'
select
propertyname,
propertyaddress,
propertyprice,
availableunits,
propertyid
from
#propertylisting
Drop table #propertylisting
Upvotes: 2
Reputation: 5893
Try to use like this first create temp table and then execute the sql
procedure
USE [Joins]
GO
/****** Object: StoredProcedure [dbo].[insertpropertylisting] Script Date: 1/29/2019 3:55:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[insertpropertylisting]
@propertyname varchar(150),
@propertyaddress varchar(250),
@propertyprice money,
@availableunits varchar(100),
@propertyid int OUTPUT
as
BEGIN
insert into propertylisting(propertyname,propertyaddress,propertyprice,availableunits)
values(@propertyname,@propertyaddress,@propertyprice,@availableunits)
print @propertyname
select @propertyaddress as 'address'
SET @propertyid=SCOPE_IDENTITY()
SELECT @propertyname=propertyname,@propertyaddress=propertyaddress,@propertyprice=propertyprice,
@availableunits=availableunits ,@propertyid FROM propertylisting WHERE property_id=@propertyid
-- Return @propertyid u can write in selecting it self
END
Create table #propertylisting
(
propertyname varchar(150),
propertyaddress varchar(250),
propertyprice money,
availableunits varchar(100),
propertyid int
)
Insert into #propertylisting
Exec [dbo].[insertpropertylisting]
Upvotes: 1