Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 28th, 2005, 10:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default Conditional INSERT

I have a stored procedure that inserts a record, but I only want it to execute if the record is not a duplicate......how can I do the check, and the insert all in one sp?

-------------------------
Beware of programmers with screwdrivers...
__________________
-------------------------
Beware of programmers with screwdrivers...
 
Old October 28th, 2005, 11:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

What defines a duplicate?

I think something like:
Code:
    INSERT yourtable (<columnlist>)
    VALUES (<parameterlist>)
    WHERE NOT EXISTS (SELECT * FROM yourtable
                        WHERE <col1> = <parameter1>
                          AND <col2> = <parameter2>
                          AND ... );
The list of WHERE predicates is the list that determines what a duplicate is.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 28th, 2005, 11:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default

Well, that gave me an error "incorrect syntax near the keyword 'where'" but I got this to work...

INSERT yourtable (<columnlist>)
SELECT 'val1', val2', etc.
WHERE NOT EXISTS (SELECT * FROM yourtable
                        WHERE <col1> = <parameter1>
                          AND <col2> = <parameter2>

Thanks Jeff!

                          AND ... );

-------------------------
Beware of programmers with screwdrivers...





Similar Threads
Thread Thread Starter Forum Replies Last Post
conditional css sbkumar CSS Cascading Style Sheets 3 July 15th, 2008 04:32 PM
Conditional transformation vvenk XSLT 3 July 10th, 2008 09:09 AM
conditional formatting jeremy1048 Access 4 July 3rd, 2008 11:20 AM
regarding conditional statements abhi.prince4u XSLT 1 July 21st, 2007 06:00 AM
conditional IF roy_mm Reporting Services 0 October 30th, 2006 07:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.