Article View: borland.public.interbase.sql
Article #425Re: Is this safe?
From: Wayne Niddery (TeamB)
Date: Fri, 07 Mar 2008 23:13
Date: Fri, 07 Mar 2008 23:13
51 lines
2841 bytes
2841 bytes
"Dan K" <koodanospam@icsusa.com> wrote in message
news:47d18e36$1@newsgroups.borland.com...
>
> I need to write some SQL to flip the values in a table or two. Here's an
> example of what I want to do. It works, but I'm not sure if it's safe.
> All
> of our users are using Interbase 7.1. In my example, I'm flipping tax2
> with
> tax1 and using tax3 as a placeholder. I know that tax 3 is null, so that
> part is safe. Will the order of operation always be the same?
>
> update taxes set
> tax_tax3desc = tax_tax1desc,
> tax_tax1desc = tax_tax2desc,
> tax_tax2desc = tax_tax3desc,
> tax_tax3desc = null
> where storenumber_Id = 3
>
> Strange looking, but it seems to work.
It's *probably* safe, however SQL is not a procedural language and I'm not
aware of anything in either the SQL standard or in InterBase that guarantees
a statement will executed as such. Therefore there is at least some risk in
depending on such behaviour.
A much better idea would be to use a stored procedure for operations such as
this, and that has the added benefit of not needing a dummy field in the
record definition to accomodate such operations. The exact process above can
be done in a proc as follows (not tested of course):
create procedure SwapTaxes (
storeid integer
)
as
declare variable tax1desc varchar(30); // whatever correct definition is
declare variable tax2desc varchar(30); // whatever correct definition is
begin
select tax_tax1desc, tax_tax2desc from taxes
where storenumber_Id = :storeid
into tax1desc, tax2desc;
update taxes set
tax_tax1desc = :tax2desc,
tax_tax2desc = :tax1desc
where storenumber_Id = :storeid;
end;
--
Wayne Niddery - TeamB (www.teamb.com)
Winwright, Inc. (www.winwright.ca)
Technical Details
Message-ID:
<47d1cc21$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<47d18e36$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 114a0e,fbab0f944ae6f5eb X-Google-Attributes: gid114a0e,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "Wayne Niddery \(TeamB\)" <wniddery@chaffaci.on.ca> Newsgroups: borland.public.interbase.sql References: <47d18e36$1@newsgroups.borland.com> In-Reply-To: <47d18e36$1@newsgroups.borland.com> Subject: Re: Is this safe? Date: Fri, 7 Mar 2008 18:13:32 -0500 Lines: 52 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Windows Mail 6.0.6000.16480 X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6000.16545 NNTP-Posting-Host: 24.137.214.213 Message-ID: <47d1cc21$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1204931617 24.137.214.213 (7 Mar 2008 15:13:37 -0700) X-Authenticated-User: wniddery Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail