如何在临时表中显示存储过程的结果?

时间:2019-01-29 11:45:18

标签: sql-server stored-procedures

我想在临时表中显示存储过程的结果。

我有以下代码:

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

我没有得到表的输出。

2 个答案:

答案 0 :(得分:2)

我可以看到的一些问题:

  • 不向SP提供参数。
  • 不需要的SP中间的独立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]