User Functions in batch inserts
From: TJoker .NET (nonono_at_nononono.no)
Date: 02/24/04
- Next message: Brandon Lilly: "Re: Keeping a Running Count"
- Previous message: Joe Celko: "Re: problem using identity column as primary key"
- Next in thread: oj: "Re: User Functions in batch inserts"
- Reply: oj: "Re: User Functions in batch inserts"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 24 Feb 2004 09:01:25 -0800
I'm having some trouble with an Insert statement.
Here are my table definifions:
table: test_id
rowId -> INT - NOT NULL
name -> CHAR(10) - NOT NULL
date -> DATETIME - NOT NULL
uid -> UNIQUEIDENTIFIER - NOT NULL
table: test_id_source
name -> CHAR(10)
I populated the test_id_source table with a bunch of rows
of sample data.
In the test_id table, the rowId column cannot be an
identity column but it has a similar purpose of being the
PK of the table.
I removed the PK constraint for this test.
The value for the rowId, when inserting a new row, comes
from the following user function:
CREATE FUNCTION GetNextTestId() RETURNS INT
AS
BEGIN
DECLARE @Res INT
SELECT @Res=1 + ISNULL(MAX(rowId), 0) FROM test_id
RETURN @Res
END
So, if I do a simple statement like the following it works
fine:
INSERT INTO test_id ( rowid, [name], [date], uid )
VALUES (dbo.GetNextTestId(), 'some data', getdate(),
newid())
My problem is during the folowing batch insert:
INSERT INTO test_id ( rowid, [name], [date], uid )
SELECT dbo.GetNextTestId(), [name], getdate(), newid()
FROM test_id_source
I got something like this:
[rowId][name] [date] [uid]
31 a 2004-02-24 10:27:01.523 73ED52BC-
BADF-42AF-B5F4-140E15D914B2
31 b 2004-02-24 10:27:01.523 A6D6D13F-
7CB2-4328-80EA-8E16E1B2BC8B
31 c 2004-02-24 10:27:01.523 0B04F89A-
5866-45FF-9B0E-A376FFC8F615
31 d 2004-02-24 10:27:01.523 4322486A-
4F5E-4918-AE68-F5CD1C60E492
31 e 2004-02-24 10:27:01.523 A4F52511-
F13C-4D2E-9B81-618F8C062538
31 f 2004-02-24 10:27:01.523 4E877AA2-
A1A4-42A5-A07A-AA36DFD6EC0A
So, the NEWID() function was evaluated once for each row,
but my function was not!
I wish it was numbered 31, 32, 33 .. etc ..
Is this the correct behavior? Is there another way of
doing this without adding extra columns to
the table (this is a production database, so I really
can't mess with it a lot)
Thanks a lot
TJ!
-- TJoker MVP: Paint, Notepad, Solitaire ****************************************
- Next message: Brandon Lilly: "Re: Keeping a Running Count"
- Previous message: Joe Celko: "Re: problem using identity column as primary key"
- Next in thread: oj: "Re: User Functions in batch inserts"
- Reply: oj: "Re: User Functions in batch inserts"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|