473,503 Members | 2,238 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question of SP technique...

Hi All

I'm creating some SPs and I've got a query which is inserting data
into a table with a a unique constraint:

CREATE TABLE [fil_Films] (
[fil_ID] [int] IDENTITY (1, 1) NOT NULL ,
[fil_Film] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
CONSTRAINT [PK_Tfil_Film] PRIMARY KEY NONCLUSTERED
(
[fil_ID]
) ON [PRIMARY] ,
CONSTRAINT [IX_fil_Films] UNIQUE NONCLUSTERED
(
[fil_Film]
) ON [PRIMARY]
) ON [PRIMARY]
GO
When I insert data, should I check in the SP to see if there is an
existing record or simply catch the error if it already exists? Which
is the better technique? My current SP looks like:

CREATE PROCEDURE spAddFilm (@Type varchar(50)) AS

DECLARE @Count int

SET NOCOUNT ON

SELECT @Count = Count(fil_ID) FROM fil_Films WHERE fil_Films.fil_Film
= @Type
IF @COUNT IS NULL
BEGIN
INSERT INTO dbo.Tfil_Film (Tfil_Type) VALUES (@Type)
RETURN 1 -- OK
END
ELSE
BEGIN
RETURN 2 -- Exists
END
GO


Thanks

Sam
Jul 20 '05 #1
2 1177
Samuel Hon (no*****@samuelhon.co.uk) writes:
I'm creating some SPs and I've got a query which is inserting data
into a table with a a unique constraint:

CREATE TABLE [fil_Films] (
[fil_ID] [int] IDENTITY (1, 1) NOT NULL ,
[fil_Film] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
CONSTRAINT [PK_Tfil_Film] PRIMARY KEY NONCLUSTERED
(
[fil_ID]
) ON [PRIMARY] ,
CONSTRAINT [IX_fil_Films] UNIQUE NONCLUSTERED
(
[fil_Film]
) ON [PRIMARY]
) ON [PRIMARY]
GO
First question: if you have a natural key in fil_Film, why add another
extra key?
When I insert data, should I check in the SP to see if there is an
existing record or simply catch the error if it already exists? Which
is the better technique? My current SP looks like:
Explicit check is better, since else the application must be able to
cope with the error. Better to let the application scream blue murder
if there is an SQL error.

Also, this can be improved:
SELECT @Count = Count(fil_ID) FROM fil_Films WHERE fil_Films.fil_Film
= @Type


IF EXISTS (SELECT * FROM fil_Films WHERE fil_Film = @Type)

There is a slight performance gain here, as the EXISTS query terminates
on first hit.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Thanks Erland
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2863
by: Trent | last post by:
I found an excellent technique in "Eric Meyers on CSS" that I thought some of you might be interested in. The technique is to use "debugging" styles to quickly see the layout of a page. For...
3
2123
by: Jack Klein | last post by:
I'm looking for opinions on a C technique I, and others, have used successfully in the past. While some people swear by, apparently others swear at it. Assume a part of a program too large to...
0
1401
by: deathyam | last post by:
Hi, I am writing an application in Excel 97 in which the users click a button and data is saved/read to and from an Access 97 database on the LAN. I am concerned about performance because there...
2
1459
by: Niklas Norrthon | last post by:
I want to share a technique I recently have found to be useful to get around some obstacles that data protection can raise. Consider the following class: // foo.h #ifndef H_FOO #define H_FOO...
6
1723
by: varois83 | last post by:
Hi I am currently learning the faux columns technique from various tutorials but none of them tells me how to create a 774 px band in 3 colors in photoshop. I understand I can create 3...
0
1340
by: rshekhtm | last post by:
Hi everyone, I would like to get your opinion on a technique I came up with when faced with the problem of redundant code in every web method (authentication, logging, exception handling)....
18
2314
by: xahlee | last post by:
Last year, i've posted a tutorial and commentary about Python and Perl's sort function. (http://xahlee.org/perl-python/sort_list.html) In that article, i discussed a technique known among...
3
4876
by: pragy | last post by:
Hey, can any one help me for writing a program of naive gauss elimintaion technique? It's a technique to solve system of simultaneous linear equations using matrix. thanks
2
1639
by: Bob Alston | last post by:
Recently I have been helping a nonprofit modify a system built in Access, that they acquired from another nonprofit. I am doing this as a volunteer. I would like your perspective on two...
1
368
by: Ben Bacarisse | last post by:
cri@tiac.net (Richard Harter) writes: <snip> I too was going to mention the technique until I saw Eric's reply because in your sketch you said: | we have definitions like | | struct...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.