我正在进行sql查询,我将在名为filess的表中插入记录,其中包含字段(ID,Name)
我想在成功插入记录后立即从表“filess”中获取所有记录。为此,我编写了以下存储过程
USE [sps]
GO
/****** Object: StoredProcedure [dbo].[SP_InsTestFile]
Script Date: 25-05-2015 11:21:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[SP_InsTestFile]
@ID int,
@Name varchar(50)
as
begin try
insert into filess(Name) values (@Name)
Declare @IDD int=@@IDENTITY
select 'y'
IF(@IDD>0)
BEGIN
select * from filess
END
ELSE
BEGIN
select 'n'
END
--select 'Y'
end try
begin catch
select 'X'
end catch
但是存储过程中的查询在成功插入后仅返回'y',但我想要表filess和值y或n的所有记录。请帮助我。
答案 0 :(得分:0)
你可以从proc中返回“Y”或“N”作为out参数,并使用select
命令来获取表记录。
答案 1 :(得分:0)
希望filess
表格不包含IDENTITY
列的ID
。
请参阅以下两个示例,找出与列的IDENTITY
有关的区别。
表With out IDENTITY列,将@@IDENTITY
作为null
表With IDENTITY列,返回@@IDENTITY
因此,如果@@IDENTITY
返回null
,则IF(@IDD>0)
失败并且不再执行。