Friday, March 23, 2012

Replace NULL with "-" using Trigger

Can a Trigger be created to automatically replace NULL values with
"-"? Suppose I have an Entry Form with 20 Text Boxes. I don't want to
code in my application. I want that when an Insert Query is executed,
a BEFORE/AFTER TRIGGER should be executed to replace the NULL values
coming from Insert Query to "-".
I also want to know whether to use a Trigger or a Function/Stored
Procedure.
RP
1) You can create table that contains a column with default value ='-'
create table #t (c int, c1 char(1) default '-')
go
insert into #t (c) values (10)
go
select * from #t
2) Use COALESCE funtion to dispaly a result to the client
create table #t1 (c int, c1 char(1))
go
insert into #t1 (c,c1) values (10,null)
go
select c,coalesce(c1,'-') from #t1
"RP" <rpk.general@.gmail.com> wrote in message
news:1187163145.162514.295500@.g12g2000prg.googlegr oups.com...
> Can a Trigger be created to automatically replace NULL values with
> "-"? Suppose I have an Entry Form with 20 Text Boxes. I don't want to
> code in my application. I want that when an Insert Query is executed,
> a BEFORE/AFTER TRIGGER should be executed to replace the NULL values
> coming from Insert Query to "-".
> I also want to know whether to use a Trigger or a Function/Stored
> Procedure.
>
|||It is most efficient to code this type of data validation on the front end,
since that will distribute the load. If you have a middle tier, that is the
second best place to do it, again so you can distribute the load. If you do
it on the database server, all of the work occurs there and could lead to
scalability limitations in a heavily used app.
In any case, you could use a store proc for your insert and simply use
COALESCE on all fields in the INSERT statement inside that sproc. A trigger
would work too, but would be by far the least scalable of the methods. I
would avoid a function for this need.
TheSQLGuru
President
Indicium Resources, Inc.
"RP" <rpk.general@.gmail.com> wrote in message
news:1187163145.162514.295500@.g12g2000prg.googlegr oups.com...
> Can a Trigger be created to automatically replace NULL values with
> "-"? Suppose I have an Entry Form with 20 Text Boxes. I don't want to
> code in my application. I want that when an Insert Query is executed,
> a BEFORE/AFTER TRIGGER should be executed to replace the NULL values
> coming from Insert Query to "-".
> I also want to know whether to use a Trigger or a Function/Stored
> Procedure.
>

No comments:

Post a Comment