Showing posts with label description. Show all posts
Showing posts with label description. Show all posts

Friday, March 23, 2012

Problem with a Validation

Hi...

I was with a problem....

I have a table with this model:

Create Table Test
(
Id_Charge Int Not Null,
Description Varchar Not Null
)

For the rule, this column Id_Charge must be incremental and when the number arrive to 999 he must be reseted to 1, with this storage for exemple:

1 AAA
2 BBB
(...)
998 YYY
999 ZZZ
1 AAB
2 BBC
(...)

I would like to keep this control for the database system, but I don't know when I make this, somebody has a idea?

Tks...

Bruno,

Here's what I've come up with. It's not very pretty but works (for single inserts). If you want to handle mutlirow inserts you shoudl get a cursor on inserted and then handle them one by one the same way as done here.

You really should some unique colomn on the table though....

Hope this helps,

Regards John

Code Snippet

create Table Test

(

Id_Charge Int Not Null,

Description Varchar(50) Not Null

)

-- tavle to keep track of last issued ID

create table LastId

(

Id int not null

)

-- set first value

insert LastID (Id) values( 1)

go

-- trigger to handle inserts

create trigger Test_NextID on Test instead of insert

as

declare @.NextId int,

@.Desc varchar(50),

@.InsertedCount int

-- make sure we're dealng with just 1 row being inserted (otherwise get a cursor on Inserted and handle them one by one)

select @.InsertedCount = Count(*) from inserted

if @.InsertedCount > 1

begin

rollback tran

raiserror ('Can only handle single inserts', 16, 1)

return

end

-- update table to next Id

update LAstID

set Id = case Id when 999 then 1 else Id + 1 end

-- get last id

select @.NextId = Id

from LastID

-- find the value that was inserted

select @.Desc = Description from inserted

insert Test (Id_Charge, Description)

values (@.NextID, @.Desc)

go

-- insert some testdata

insert TEst (Description) values ('AAA')

insert TEst (Description) values ('BBB')

insert TEst (Description) values ('CCC')

insert TEst (Description) values ('DDD')

-- skip ahead some

update lastID set Id = 997

insert TEst (Description) values ('AAA')

insert TEst (Description) values ('BBB')

insert TEst (Description) values ('CCC')

insert TEst (Description) values ('DDD')

select * from test

-- throws error

insert TEst (Description)

select description from TEst

|||

John, thank you very much... your solution is perfect for my problem...

Bruno.

Problem with a SP. Help!

Why does only the first INSERT statement work?

CREATE PROCEDURE insert_inventory
@.item_name varchar(20),
@.description varchar(100),
@.notes varchar(255),
@.amount varchar(8)
AS
DECLARE @.item_id int
DECLARE @.transaction_date datetime

IF (@.item_name = '') SET @.item_name = NULL
IF (@.description = '') SET @.description = NULL
IF (@.notes = '') SET @.notes = NULL
IF (@.amount = '') SET @.amount = NULL

SET @.item_id = IDENT_CURRENT('inventory')
SET @.transaction_date = GETDATE()

INSERT INTO inventory (item_name, item_description, notes) VALUES (@.item_name, @.description, @.notes)

INSERT INTO expenditure (item_id, transaction_date, amount) VALUES (@.item_id, @.transaction_date, CAST(@.amount AS money))

Also, where I have IF(@.blah = '') SET...

I use these to convert blank fields in VB6 to NULL values, is there any easier way?do you get any error on the second insert?

if @.blah='' set... can be removed, but you will have to check for the value somewhere if you care for it: either in your vb6, in the if as you do now, or in the values clause (...values (nullif(@.blah, ''), ...)|||Try to change your SP this way (you have to get id after insert - not before):

SET @.transaction_date = GETDATE()
begin tran
INSERT INTO inventory (item_name, item_description, notes) VALUES (@.item_name, @.description, @.notes)
if @.@.error<>0 begin
rollback
return
end
SET @.item_id = IDENT_CURRENT('inventory')

INSERT INTO expenditure (item_id, transaction_date, amount) VALUES (@.item_id, @.transaction_date, CAST(@.amount AS money))
commit|||i think ident_current(..) returns the _last_ new identity value for the specified table, not the identity value generated by the current scope. in this case if your insert occurred before another insert in another scope you may acquire the value that is not yours. scope_identity() guarantees that identity value belongs to insert from your session.

also, it's better to write the error handler this way:

declare @.error int, @.id int
...insert operation
select @.error = @.@.error, @.id = scope_identity()
if @.error <> 0 begin
raiserror (...)
rollback tran
return (1)
end
commit tran|||Cheers, for those, they were all good recommendations which I am now using, but it still would only execute the first INSERT statement. That is until I used this:

SET NOCOUNT ON

Works beautifully now!!!

Rayden