【发布时间】:2016-01-04 14:23:59
【问题描述】:
我想知道如果我在 if 语句下声明变量和表变量是否会引发错误,是否会有所不同。因此,如果作为参数给出的@key 无效,则 sp 会抛出错误并且在没有为变量分配内存的情况下完成,或者它是否仍然分配内存?
create procedure dbo.FooSelect
@key uniqueidentifier
as
begin
set nocount on
declare @count bigint
declare @temp table ( FooID bigint,
Name nvarchar(100)
primary key (FooID))
if not exists ( select 1
from dbo.Foo f
where f.Key = @key)
begin
;throw 50000, 'Invalid key, permission denied!', 1
end
--#####################################################################
-- declare better here (below the possible error) so in case of an error no memory is allocated?
--#####################################################################
select @count = count(*)
from dbo.Foo
if @count > 10
begin
insert into @temp (FooID, Name)
select
from dbo.Foo f
where f.Key = @key
and f.FooID > 100
end
else
begin
insert into @temp (FooID, Name)
select
from dbo.Foo f
where f.Key = @key
and f.FooID < 100
end
select *
from @temp
return 1
end
感谢您的帮助
【问题讨论】: