Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Seeking input on First 'complex' proc.

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2011-09-19 : 19:24:34
So I just finished my first proc and tested with everything working. Though I might just post the code for some feed back. It is simple in comparison to what most people here are used to but I am more of a front end programmer so just thought I would get some input.

spAddNewSection code:
create proc spAddNewSection
@FullName varchar(250),
@ShortName varchar(50),
@FolderName varchar(50),
@PrimaryCat char(11),
@SecondaryCat char(11) = null,
@Synopsis varchar(MAX)
as
--Insert new section into DB
insert into dbo.SectionInfo (SectionTitle, FolderName, ShortName, Synopsis) values (@FullName, @FolderName, @ShortName, @Synopsis)
--Declare and set the SectionID varibale with the autoID from the insert above
declare @SectionID int;
select @SectionID = SCOPE_IDENTITY();
--Insert data for primary cat
insert into dbo.SectionCategories (SectionID, Category, Position) values (@SectionID, @PrimaryCat, '0');
--Cehck if secondary cat was set and insert if needed
if @SecondaryCat is not null
begin
insert into dbo.SectionCategories (SectionID, Category, Position) values (@SectionID, @SecondaryCat, '0');
end
--If no errors sofar run spUpdateCount, else return error code
if @@ERROR = 0
begin
exec dbo.spUpdateCount 'NewSection';
return @@ERROR;
end
else
begin
return @@ERROR;
end

spUpdateCount code (called in the above script)
create proc spUpdateCount
@AddType varchar(50)
as
--Decare and get the count of the addtype
declare @Count int;
select @Count = UpdateCount from dbo.UpdateCounts where AddType = @AddType;
--If the count is greater then 0 incrament, else add new entry
if @Count > 0
begin
set @Count = @Count + 1;
update dbo.UpdateCounts set UpdateCount = @Count where AddType = @AddType;
end
else
begin
insert into dbo.UpdateCounts (AddType, UpdateCount) values (@AddType, 1);
end


--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-19 : 19:40:24
You need to add transaction handling. I'd wrap it into TRY/CATCH too.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2011-09-19 : 21:00:43
quote:
Originally posted by tkizer

You need to add transaction handling. I'd wrap it into TRY/CATCH too.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog


I know nothing about transactions yet but I plan on looking into that. I will also look into adding a try catch. I did not relize SQL had it.

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-09-20 : 02:53:05
Notwithstanding that you might use TRY/CATCH I would have done it like this (I've highlighted a few bits in red:

CREATE PROC dbo.spAddNewSection
@FullName varchar(250),
@ShortName varchar(50),
@FolderName varchar(50),
@PrimaryCat char(11),
@SecondaryCat char(11) = null,
@Synopsis varchar(MAX)
AS
SET NOCOUNT ON
SET XACT_ABORT ON
SET ARITHABORT ON
DECLARE @intErrNo int

BEGIN TRANSACTION
--Insert new section into DB
INSERT INTO dbo.SectionInfo (SectionTitle, FolderName, ShortName, Synopsis)
VALUES (@FullName, @FolderName, @ShortName, @Synopsis)
SELECT @intErrNo = @@ERROR
--Declare and set the SectionID varibale with the autoID from the insert above
DECLARE @SectionID int
SELECT @SectionID = SCOPE_IDENTITY()
IF @intErrNo = 0
BEGIN
--Insert data for primary cat
INSERT INTO dbo.SectionCategories (SectionID, Category, Position)
SELECT @SectionID, @PrimaryCat, '0'
UNION ALL
--Cehck if secondary cat was set and insert if needed
SELECT @SectionID, @SecondaryCat, '0'
WHERE @SecondaryCat is not null
SELECT @intErrNo = @@ERROR
END
--If no errors sofar run spUpdateCount, else return error code
IF @intErrNo = 0
BEGIN
EXEC @intErrNo = dbo.spUpdateCount 'NewSection'
END
IF @intErrNo = 0
BEGIN
COMMIT
END
ELSE
BEGIN
ROLLBACK
END

RETURN @intErrNo
GO

CREATE PROC dbo.spUpdateCount
@AddType varchar(50)
AS
SET NOCOUNT ON
SET XACT_ABORT ON
SET ARITHABORT ON
DECLARE @intErrNo int,
@intRowCount int

BEGIN TRANSACTION

UPDATE dbo.UpdateCounts
SET UpdateCount = UpdateCount + 1
WHERE AddType = @AddType
AND UpdateCount > 0
SELECT @intErrNo = @@ERROR, @intRowCount = @@ROWCOUNT

IF @intErrNo = 0 AND @intRowCount = 0 -- No row found with suitable UpdateCount
BEGIN
INSERT INTO dbo.UpdateCounts(AddType, UpdateCount) values (@AddType, 1)
SELECT @intErrNo = @@ERROR, @intRowCount = @@ROWCOUNT
END

IF @intErrNo = 0
BEGIN
COMMIT
END
ELSE
BEGIN
ROLLBACK
END

RETURN @intErrNo
GO
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2011-09-20 : 08:38:26
Kristen, thanks for the tips.

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2011-09-21 : 20:18:18
Kristen,
I just noticed you changed my checking to see if @SecondaryCat is not null from an if statment to just a select statment. Can you explain a but on how that works?

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-09-22 : 02:43:16
You have two inserts, each has an overhead.

I changed it to a single insert that inserts a SELECT - the SELECT contains a pair of selects combined using UNION ALL.

The Second select will select zero rows if @SecondaryCat is null (i.e. it only selects anything if @SecondaryCat is not null)
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-09-22 : 04:50:37
quote:
Originally posted by Eagle_f90


...
--Cehck if secondary cat was set and insert if needed
if @SecondaryCat is not null
begin
insert into dbo.SectionCategories (SectionID, Category, Position) values (@SectionID, @SecondaryCat, '0');
end
--If no errors sofar run spUpdateCount, else return error code
if @@ERROR = 0



The only thing that will check for an error is the update into dbo.SectionCategories. @@error is set to the error code for every single statement.

Read up on TRY...CATCH. Way better error handling than checking @@Error after every line.

--
Gail Shaw
SQL Server MVP
Go to Top of Page
   

- Advertisement -