我想在临时表中显示存储过程的结果。
我有以下代码:
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
我没有得到表的输出。
答案 0 :(得分:2)
我可以看到的一些问题:
SELECT
(也是PRINT
)。ALTER PROCEDURE
命令时缺少批处理分隔符。INSERT
语句中包括列列表,尤其是在进行INSERT INTO EXEC
时。首先对SP进行一些更改;删除未使用的语句和参数(包括参数的OUT
属性,因为您似乎没有使用它)。您可以使用INSERT
语句的OUTPUT子句返回插入的值,包括计算列和IDENTITY
列。
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
运行此ALTER
之后,可以将插入的记录与INSERT INTO EXEC
一起使用。确保将正确的值传递给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
答案 1 :(得分:1)
尝试像这样首先使用temp表,然后执行sql
过程
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]