Home All Groups Group Topic Archive Search About

Stored Procedure for update : update selected fields depending on indicators

Author
1 Jul 2005 1:16 PM
sven.maes
Hello,


is it possible to create a stored procedure with parameters,
so that you can say for example :

update table

if @indicator1 = 1  then set field1 = @field1
if @indicator2 = 1  then set field2 = @field2
.....

where keyfield = @keyfield ?


thank you,
kind regards ,

sven

Author
1 Jul 2005 1:28 PM
Cowboy (Gregory A. Beamer) - MVP
Something like ...

CREATE PROCEDURE TestProc
(
@indicator1 int
,@indicator2 int
,@field1 varchar(50)
,@field2 varchar(50)
,@keyfield int
)
AS

IF @indicator1 = 1 THEN
   UPDATE MyTable SET Field1 = @field1
   WHERE keyfield = @keyfield

IF @indicator2 = 1 THEN
   UPDATE MyTable SET Field1 = @field2
   WHERE keyfield = @keyfield


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************


Show quote
"sven.m***@gmail.com" wrote:

> Hello,
>
>
> is it possible to create a stored procedure with parameters,
> so that you can say for example :
>
> update table
>
> if @indicator1 = 1  then set field1 = @field1
> if @indicator2 = 1  then set field2 = @field2
> .....
>
> where keyfield = @keyfield ?
>
>
> thank you,
> kind regards ,
>
> sven
>
>
Author
1 Jul 2005 3:19 PM
Jeremy Williams
How about something like this:

UPDATE MakeBelieveTable
SET Column1 = CASE WHEN @indicator1 = 1 THEN @Value1 ELSE Column1 END,
SET Column2 = CASE WHEN @indicator2 = 1 THEN @Value2 ELSE Column2 END,
......

The idea is to set the column to either the passed in value, or the column's
current value, depending on the indicator value. Note, since you did not
provide any DDL, this is completely untested, but you should be able to
modify it to make it work for you.

IHTH
Jeremy

<sven.m***@gmail.com> wrote in message
Show quote
news:1120223798.864151.260550@g43g2000cwa.googlegroups.com...
> Hello,
>
>
> is it possible to create a stored procedure with parameters,
> so that you can say for example :
>
> update table
>
> if @indicator1 = 1  then set field1 = @field1
> if @indicator2 = 1  then set field2 = @field2
> ....
>
> where keyfield = @keyfield ?
>
>
> thank you,
> kind regards ,
>
> sven
>

AddThis Social Bookmark Button