Is exists() more efficient than this? SET NOCOUNT ON Select keywordid from keywords where keyword =@keyword if @@ROWCOUNT <> 1 begin Insert into keywords(keyword) values(@keyword) end SET NOCOUNT OFF
I'm using a stored procedure for the tiny performance savings I may get. On Thu, Jan 27, 2011 at 6:21 PM, Michael Grant <mgr...@modus.bz> wrote: > > I generally prefer validation rather than an expectation of exceptions. > What > about using the EXISTS? > > I think this should work > > if EXISTS (select keyword from table where keyword = '#keyword#') > -- do nothing > ELSE > -- do insert > > > > On Thu, Jan 27, 2011 at 6:06 PM, Michael Dinowitz < > mdino...@houseoffusion.com> wrote: > > > > > I'm inserting a keyword into a table that only contains unique keywords > (db > > constraint). I'm currently doing a select to see if the keyword already > > exists and then an insert if it does not. This 2 step process is what I > > think of as 'correct'. First check, then act. > > > > After a little thought, the 'wrong' way might be better in this case. The > > wrong way is to just try the insert without first checking if the value > > already exists. If it fails, a try/catch deals with it. As messy as this > > sounds, it saves a full step and is probably the more efficient of the > two. > > > > Am I wrong to think that the second method is wrong? Should I do it this > > way? > > > > Thanks > > > > Michael Dinowitz > > > > > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:341581 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm