I have this stored procedure that I'd like improved. I have changed the DB name and the procedure name. I am working on SQL Server 2000.
USE [DBName]
ALTER PROCEDURE [dbo].[Table_GetSomething]
@name nvarchar(50),
@Cooltree nvarchar(10),
@aID int,
@bID int = 0
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @bCE bit = 0
declare @aCE bit = 0
declare @bCE2 bit = 0
declare @aCE2 bit = 0
declare @tableName varchar(60)
declare @table table
(
FA nvarchar(255),
FN nvarchar(255),
[Seeboj] nvarchar(1024),
Brotty nvarchar(4000),
CD nvarchar(4000)
)
if( select count(*)
from ECCB
where aID = @aID
and bID = @bID
and [Name] = @name) = 1
set @bCE = 1
else
if( select count(*)
from ECCA
where aID = @aID
and [Name] = @name) > 0
set @aCE = 1
if( select count(*)
from ECCDB
where aID = @aID
and bID = @bID) > 0
set @bCE2 = 1
else
if( select count(*)
from ECCDA
where aID = @aID) > 0
set @aCE2 = 1
if @bCE = 1
insert into @table (FA, FN, [Seeboj], Brotty)
select FA, FN, [Seeboj], Brotty
from ECCB
where aID = @aID
and bID = @bID
and [Name] = @name
else
if(@aCE = 1)
insert into @table (FA, FN, [Seeboj], Brotty)
select FA, FN, [Seeboj], Brotty
from ECCA
where aID = @aID
and [Name] = @name
else
insert into @table (FA, FN, [Seeboj], Brotty)
select FA, FN, [Seeboj], Brotty
from EDC
where Cooltree = @Cooltree
and [Name] = @name
if(@bCE2 = 1)
update @table
set CD =
(select CD
from ECCDB
where aID = @aID
and bID = @bID)
else
if(@aCE2 = 1)
update @table
set CD =
(select CD
from ECCDA
where aID = @aID)
else
update @table
set CD =
(select CD
from EDCCD
where Cooltree = @Cooltree)
select * from @table
END