Showing posts with label instead. Show all posts
Showing posts with label instead. Show all posts

Friday, March 30, 2012

Inserting a 0

Hi,
Im having trouble with the money data type, for instance I have a column that calculates a price but it will output the price as 470.2 instead of 470.20 which is how I want it displayed on a web page.

Anyone know how to automatically insert a zero on the end of the price?

THanksNoone knows how to insert zeros on the end of numbers??|||I'm gettin '470.2000'
from this simple query I ran from query analyser

declare @.dollar as money
set @.dollar=470.2
select @.dollar

I can't understand why your only getting 470.2. Maybe you can write your calculation query for us to figure?|||I figured it out but thanks anyhow : )sql

Wednesday, March 28, 2012

insert/update NULL instead of ''

Hi,
due to special reasons I have to ensure, that in insert and update
statements for varchar-columns (which allow NULL-values) the value ''
automatically becomes replaced by NULL before the records have been
inserted/updated. (This because I have to convert a large application from
another database -which automatically substituted '' by NULL- to SqlServer
2000 SP4).
My first thought was to find a setting in SqlServer server. But I didn't
found one. Is there any?
My second thought was to find a setting in the OLEDB-provider. But I didn't
found one. Is there any?
(I'm using OLEDB, but not ADO)
My third thought has been to define triggers for that case (my very first
ones). I did it as listed below. Is this the correct way? Or is there a
better way? Or perhaps a way with better performance?
create table test (primkey integer not null, testcol1 varchar(10), testcol2
varchar(10))
create trigger test_trigger_u on test instead of update
as
update test set primkey = i.primkey,
testcol1 = nullif(i.testcol1, ''),
testcol2 = nullif(i.testcol2, '')
from test t inner join inserted i
on t.primkey = i.primkey
create trigger test_trigger_i on test instead of insert
as
insert into test (primkey,
testcol1,
testcol)
select primkey,
nullif(testcol1, ''),
nullif(testcol2, '')
from inserted
Regards,
RainerRainer,
Why not just evaluate the incoming value (i.e., stored procedure parameter)
if the value IS NULL replace it with ''. You could optionally use a trigger
as well but the former would probably give better performance and should
probably be used unless you can't control the input method/application.
HTH
Jerry
"Rainer Ebert" <rainer_ebert_at_arcor.de> wrote in message
news:%23127HJC1FHA.2884@.TK2MSFTNGP09.phx.gbl...
> Hi,
> due to special reasons I have to ensure, that in insert and update
> statements for varchar-columns (which allow NULL-values) the value ''
> automatically becomes replaced by NULL before the records have been
> inserted/updated. (This because I have to convert a large application from
> another database -which automatically substituted '' by NULL- to SqlServer
> 2000 SP4).
> My first thought was to find a setting in SqlServer server. But I didn't
> found one. Is there any?
> My second thought was to find a setting in the OLEDB-provider. But I
> didn't found one. Is there any?
> (I'm using OLEDB, but not ADO)
> My third thought has been to define triggers for that case (my very first
> ones). I did it as listed below. Is this the correct way? Or is there a
> better way? Or perhaps a way with better performance?
> create table test (primkey integer not null, testcol1 varchar(10),
> testcol2 varchar(10))
> create trigger test_trigger_u on test instead of update
> as
> update test set primkey = i.primkey,
> testcol1 = nullif(i.testcol1, ''),
> testcol2 = nullif(i.testcol2, '')
> from test t inner join inserted i
> on t.primkey = i.primkey
> create trigger test_trigger_i on test instead of insert
> as
> insert into test (primkey,
> testcol1,
> testcol)
> select primkey,
> nullif(testcol1, ''),
> nullif(testcol2, '')
> from inserted
> Regards,
> Rainer
>|||Jerry,
our application does not use stored procedures. It uses sql-statements to
select, insert, update and delete data. The application should run against
the previous database (Gupta SQLBase) and against MS SqlServer (depending on
the customer). This goal should be reached with as few source code
modifications as possible. I know, that I can change each affected
insert/update statement in the sourcecode and replace '' by NULL. But I'm
looking for a way to avoid doing this.
Do you think, the triggers are o.k.?
Do you know a better way?
regards,
Rainer
P.S.: By the way, I want to replaye '' by NULL, not NULL by ''
"Jerry Spivey" <jspivey@.vestas-awt.com> schrieb im Newsbeitrag
news:ObsNBRC1FHA.3124@.TK2MSFTNGP12.phx.gbl...
> Rainer,
> Why not just evaluate the incoming value (i.e., stored procedure
> parameter) if the value IS NULL replace it with ''. You could optionally
> use a trigger as well but the former would probably give better
> performance and should probably be used unless you can't control the input
> method/application.
> HTH
> Jerry
> "Rainer Ebert" <rainer_ebert_at_arcor.de> wrote in message
> news:%23127HJC1FHA.2884@.TK2MSFTNGP09.phx.gbl...
>|||I believe Rainer is actually looking for a way to *insert* null values
instead of the supplied empty strings rather than trying to prevent null
values from being inserted.
NULLIF is the way to go. Yet, I'd suggest handling that in the insert
procedures, rather than in the triggers, and use the triggers if changing th
e
procedures cannot be done (i.e. if there aren't any).
Or did I miss something?
ML|||Functionality wise...a trigger should work fine.
HTH
Jerry
"Rainer Ebert" <rainer_ebert_at_arcor.de> wrote in message
news:uzfHiaC1FHA.904@.tk2msftngp13.phx.gbl...
> Jerry,
> our application does not use stored procedures. It uses sql-statements to
> select, insert, update and delete data. The application should run against
> the previous database (Gupta SQLBase) and against MS SqlServer (depending
> on the customer). This goal should be reached with as few source code
> modifications as possible. I know, that I can change each affected
> insert/update statement in the sourcecode and replace '' by NULL. But I'm
> looking for a way to avoid doing this.
> Do you think, the triggers are o.k.?
> Do you know a better way?
> regards,
> Rainer
> P.S.: By the way, I want to replaye '' by NULL, not NULL by ''
> "Jerry Spivey" <jspivey@.vestas-awt.com> schrieb im Newsbeitrag
> news:ObsNBRC1FHA.3124@.TK2MSFTNGP12.phx.gbl...
>|||I believe Rainer is actually looking for a way to *insert* null values
instead of the supplied empty strings rather than trying to prevent null
values from being inserted.
NULLIF is the way to go. Yet, I'd suggest handling that in the insert
procedures, rather than in the triggers, and only use triggers if changing
the procedures is not an option (i.e. if - for some insane reason - there
aren't any).
Or am I missing something?
ML|||Ahh...ML...ok.
Something like:
CREATE TABLE #TEST
(ID INT NOT NULL,
VAL VARCHAR(10))
DECLARE @.VAL VARCHAR(10)
SET @.VAL = ''
INSERT #TEST(ID,VAL)
VALUES(1,NULLIF(@.VAL,''))
SELECT * FROM #TEST
--DROP TABLE #TEST
then?
HTH
Jerry
"ML" <ML@.discussions.microsoft.com> wrote in message
news:D577E511-2347-4A2B-9F26-FA578CFEF71B@.microsoft.com...
>I believe Rainer is actually looking for a way to *insert* null values
> instead of the supplied empty strings rather than trying to prevent null
> values from being inserted.
> NULLIF is the way to go. Yet, I'd suggest handling that in the insert
> procedures, rather than in the triggers, and only use triggers if changing
> the procedures is not an option (i.e. if - for some insane reason - there
> aren't any).
> Or am I missing something?
>
> ML|||As the modern German would say: Wonderbra!
ML|||Or as Cosmo would say: Wonderbro! ;-)
Jerry
"ML" <ML@.discussions.microsoft.com> wrote in message
news:1D8DEB5B-C763-4B1F-A18E-2E87411B674A@.microsoft.com...
> As the modern German would say: Wonderbra!
>
> ML|||Hmm... which one? :)
http://en.wikipedia.org/wiki/Cosmo
MLsql

Friday, March 23, 2012

Insert Trigger on every row

Hi,
I want to insert some hundred rows into a table, an
instead of trigger should be executed for every insert.
up to now only the last insert executes the trigger.Steffen
I am not sure what are you trying to do?
Trigger executes a statement against the table .
Just a guess
INSERT INTO Table SELECT * FROM inserted
Note: Number of columns in target and destination tables should be matched.
"Steffen Steinert de Lima" <steffen_steinert_de_lima@.hotmail.com> wrote in
message news:059b01c3d368$9646fcc0$a101280a@.phx.gbl...
> Hi,
> I want to insert some hundred rows into a table, an
> instead of trigger should be executed for every insert.
> up to now only the last insert executes the trigger.|||Hi, here is the trigger
the insert is a normal insert like
select * into stoerung_2day from ...
when i insert the rows from ms-access via odbc the
trigger is executed for every inserted row, in sqlserver
it's only executed for the last row.
**************************************************
CREATE TRIGGER stoerung_more_day ON [dbo].
[stoerung_2day]
instead of INSERT, update
AS
declare @.cnt integer
declare @.id integer
declare @.day_beginn as datetime
declare @.day_ende as datetime
declare @.gueltig as datetime
select @.id = [id],
@.day_beginn = beginn,
@.day_ende = ende
from inserted
set @.cnt = day(@.day_beginn)
set @.gueltig = @.day_beginn
while @.cnt <= day(@.day_ende)
begin
insert into stoerung_2day values (@.id, @.day_beginn,
@.day_ende, @.gueltig)
set @.cnt = @.cnt +1
set @.gueltig = @.gueltig + 1
end
********************************************************
>--Original Message--
>Steffen
>I am not sure what are you trying to do?
>Trigger executes a statement against the table .
>Just a guess
>INSERT INTO Table SELECT * FROM inserted
>Note: Number of columns in target and destination tables
should be matched.
>
>"Steffen Steinert de Lima"
<steffen_steinert_de_lima@.hotmail.com> wrote in
>message news:059b01c3d368$9646fcc0$a101280a@.phx.gbl...
>> Hi,
>> I want to insert some hundred rows into a table, an
>> instead of trigger should be executed for every insert.
>> up to now only the last insert executes the trigger.
>
>.
>|||Steffen
Have you read what BOL says?
Trigger executes a statement against the table
In your case you have multi-insert so you have to cary out in inserted
table .
INSERT INTO Table SELECT * FROM inserted
What is goal of using these statements?
> set @.cnt = @.cnt +1
> set @.gueltig = @.gueltig + 1
"Steffen Steinert de Lima" <steffen_steinert_de_lima@.hotmail.com> wrote in
message news:021601c3d36e$653596e0$a001280a@.phx.gbl...
>
> Hi, here is the trigger
> the insert is a normal insert like
> select * into stoerung_2day from ...
> when i insert the rows from ms-access via odbc the
> trigger is executed for every inserted row, in sqlserver
> it's only executed for the last row.
> **************************************************
> CREATE TRIGGER stoerung_more_day ON [dbo].
> [stoerung_2day]
> instead of INSERT, update
> AS
> declare @.cnt integer
> declare @.id integer
> declare @.day_beginn as datetime
> declare @.day_ende as datetime
> declare @.gueltig as datetime
> select @.id = [id],
> @.day_beginn = beginn,
> @.day_ende = ende
> from inserted
> set @.cnt = day(@.day_beginn)
> set @.gueltig = @.day_beginn
> while @.cnt <= day(@.day_ende)
> begin
> insert into stoerung_2day values (@.id, @.day_beginn,
> @.day_ende, @.gueltig)
> set @.cnt = @.cnt +1
> set @.gueltig = @.gueltig + 1
> end
> ********************************************************
> >--Original Message--
> >Steffen
> >I am not sure what are you trying to do?
> >Trigger executes a statement against the table .
> >
> >Just a guess
> >INSERT INTO Table SELECT * FROM inserted
> >
> >Note: Number of columns in target and destination tables
> should be matched.
> >
> >
> >"Steffen Steinert de Lima"
> <steffen_steinert_de_lima@.hotmail.com> wrote in
> >message news:059b01c3d368$9646fcc0$a101280a@.phx.gbl...
> >> Hi,
> >>
> >> I want to insert some hundred rows into a table, an
> >> instead of trigger should be executed for every insert.
> >> up to now only the last insert executes the trigger.
> >
> >
> >.
> >|||>--Original Message--
>Steffen
>Have you read what BOL says?
where ?
>Trigger executes a statement against the table
>In your case you have multi-insert so you have to cary
out in inserted
>table .
>INSERT INTO Table SELECT * FROM inserted
>What is goal of using these statements?
>> set @.cnt = @.cnt +1
>> set @.gueltig = @.gueltig + 1
the sense of the whole trigger is to multiply the rows if
the from-date and the until-date in the record are not
the same. so i create a new record for every day of the
period.
example: id date_from date_until gueltig cnt
1 05-01-2004 07-01-2004
result: 1 05-01-2004 07-01-2004 05-01-2004 1
1 05-01-2004 07-01-2004 06-01-2004 2
1 05-01-2004 07-01-2004 07-01-2004 3
for inserting one record the trigger works well. perhaps
there's missing a commit after every insert ?
>"Steffen Steinert de Lima"
<steffen_steinert_de_lima@.hotmail.com> wrote in
>message news:021601c3d36e$653596e0$a001280a@.phx.gbl...
>>
>> Hi, here is the trigger
>> the insert is a normal insert like
>> select * into stoerung_2day from ...
>> when i insert the rows from ms-access via odbc the
>> trigger is executed for every inserted row, in
sqlserver
>> it's only executed for the last row.
>> **************************************************
>> CREATE TRIGGER stoerung_more_day ON [dbo].
>> [stoerung_2day]
>> instead of INSERT, update
>> AS
>> declare @.cnt integer
>> declare @.id integer
>> declare @.day_beginn as datetime
>> declare @.day_ende as datetime
>> declare @.gueltig as datetime
>> select @.id = [id],
>> @.day_beginn = beginn,
>> @.day_ende = ende
>> from inserted
>> set @.cnt = day(@.day_beginn)
>> set @.gueltig = @.day_beginn
>> while @.cnt <= day(@.day_ende)
>> begin
>> insert into stoerung_2day values (@.id, @.day_beginn,
>> @.day_ende, @.gueltig)
>> set @.cnt = @.cnt +1
>> set @.gueltig = @.gueltig + 1
>> end
********************************************************
>> >--Original Message--
>> >Steffen
>> >I am not sure what are you trying to do?
>> >Trigger executes a statement against the table .
>> >
>> >Just a guess
>> >INSERT INTO Table SELECT * FROM inserted
>> >
>> >Note: Number of columns in target and destination
tables
>> should be matched.
>> >
>> >
>> >"Steffen Steinert de Lima"
>> <steffen_steinert_de_lima@.hotmail.com> wrote in
>> >message news:059b01c3d368$9646fcc0$a101280a@.phx.gbl...
>> >> Hi,
>> >>
>> >> I want to insert some hundred rows into a table, an
>> >> instead of trigger should be executed for every
insert.
>> >> up to now only the last insert executes the trigger.
>> >
>> >
>> >.
>> >
>
>.
>|||Unlike Oracle, SQL-server does not have a trigger which
triggers once for every row. The trigger only 'fires' once for
the complete statement.
So you have to revert to solutions allready offered in the
other anwsers.
Have a nice 2004,
Ben Brugman
"Steffen Steinert de Lima" <steffen_steinert_de_lima@.hotmail.com> wrote in
message news:059b01c3d368$9646fcc0$a101280a@.phx.gbl...
> Hi,
> I want to insert some hundred rows into a table, an
> instead of trigger should be executed for every insert.
> up to now only the last insert executes the trigger.

Friday, February 24, 2012

insert problem

hi
when i tried to store Arabic chars (from a textbox) in a db table all i got is "?" chars instead of the actual letters.that happens on my database on the server
but in my machine everything works fine
i changed the responseEncoding/requestEncoding/fileEncoding to "windows-1256" but there is no benefit .

any idea ?

thanks in advanceis the db column an nvarchar or ntext ?
what is the default encoding of the db itself ?|||thanks for replying

the db column is nvarchar

the default encoding of the db exactly i don't know ? how can i figure it out ?