A few T-SQL questions...
- From: Erik Funkenbusch <erik@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 4 Sep 2006 06:32:26 -0500
I have a few T-SQL questions that i'm hoping someone can answer for me.
1) Is there an easy way to do an "insert if no record exists, otherwise
update"? What I do now is query for the record and check recordcount, if
it exists I execute an update, otherwise insert, but this requires 2 round
trips (i'm not using a stored procedure for this for several reasons).
2) What's the best way to write an update statement so that only fields
that have changed are updated? I have a table that has a trigger that does
certain things when certain fields change. I don't want to update the
field with the current value or the trigger will process those values (even
though they didn't change). Additionally, I can't use null fields to
indicate no change because I may have to set the values to null.
I can really only think of two ways to handle this. a) build my sql
dynamically for what I need to update at run-time or b) Create a whole
bunch of specialized stored procedures to update varying permutations of
the data.
I don't like either solution. Is there some better solution here that i'm
missing? I'm hoping there is a technique I can use to effectively cancel
out the update of specific fields based on some criteria (other than a
monster if statement).
Thanks.
.
- Follow-Ups:
- Re: A few T-SQL questions...
- From: Brian Bunin
- Re: A few T-SQL questions...
- From: Ed Murphy
- Re: A few T-SQL questions...
- From: Jens
- Re: A few T-SQL questions...
- Prev by Date: Running SQL Server (2000) Dbase using MSDE 2000
- Next by Date: Re: Running SQL Server (2000) Dbase using MSDE 2000
- Previous by thread: Running SQL Server (2000) Dbase using MSDE 2000
- Next by thread: Re: A few T-SQL questions...
- Index(es):
Relevant Pages
|
Loading