Home All Groups Group Topic Archive Search About

Explicit insert for Timestamp column in Insert Trigger

Author
26 Aug 2006 6:21 PM
Joy
Hi all,
I am writing a Insert trigger but to my dismay it is not able to insert
explicitly into columns of type Timestamp.

Please let me know the work around for this.

regards,
Joy

Author
26 Aug 2006 6:57 PM
David Portas
Joy wrote:
> Hi all,
> I am writing a Insert trigger but to my dismay it is not able to insert
> explicitly into columns of type Timestamp.
>
> Please let me know the work around for this.
>
> regards,
> Joy

The point of a TIMESTAMP is that it is updated automatically for you.
If you want to insert some particular value then use a different
datatype (BINARY for example).

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--
Author
26 Aug 2006 7:05 PM
Joy
Dear David,
It would be great if you could provide me with some example.

regards,
Joy

Show quote
"David Portas" wrote:

> Joy wrote:
> > Hi all,
> > I am writing a Insert trigger but to my dismay it is not able to insert
> > explicitly into columns of type Timestamp.
> >
> > Please let me know the work around for this.
> >
> > regards,
> > Joy
>
> The point of a TIMESTAMP is that it is updated automatically for you.
> If you want to insert some particular value then use a different
> datatype (BINARY for example).
>
> --
> David Portas, SQL Server MVP
>
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
>
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>
>
Author
26 Aug 2006 10:04 PM
Erland Sommarskog
Joy (J**@discussions.microsoft.com) writes:
> Dear David,
> It would be great if you could provide me with some example.

Example of what?

Maybe you should clarify why you want to insert an explicitl value into
a timestamp column?

Beware that timestamp is a data type that bears no relation to date and
time. It's an 8-byte binary value which is guaranteed to be unique within
the database, and every time you update a row with a timestamp column,
the column is set to the current timestamp value. The main purpose of
timestamp values is to support optimistic concurrency.

--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Author
27 Aug 2006 9:06 AM
Joy
Hi Erland,
Well i want to insert explicit value into timestamp values because i am
writing a process that is similar to replication meaning i have front end and
back end database and the front end has some tables that have columns of type
timestamp and when i run my process the respective rows at both the databases
should have the same value.

shall be waiting for your feedback at the earliest.

thanks,
Joy

Show quote
"Erland Sommarskog" wrote:

> Joy (J**@discussions.microsoft.com) writes:
> > Dear David,
> > It would be great if you could provide me with some example.

> Example of what?
>
> Maybe you should clarify why you want to insert an explicitl value into
> a timestamp column?
>
> Beware that timestamp is a data type that bears no relation to date and
> time. It's an 8-byte binary value which is guaranteed to be unique within
> the database, and every time you update a row with a timestamp column,
> the column is set to the current timestamp value. The main purpose of
> timestamp values is to support optimistic concurrency.
>
> --
> Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se
>
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
>
Author
27 Aug 2006 9:31 AM
Erland Sommarskog
Joy (J**@discussions.microsoft.com) writes:
> Well i want to insert explicit value into timestamp values because i am
> writing a process that is similar to replication meaning i have front
> end and back end database and the front end has some tables that have
> columns of type timestamp and when i run my process the respective rows
> at both the databases should have the same value.

Why would you want the timestamp value to be same value? Does that serve
any purpose, or is just because it looks nice?

You cannot explicitly set a timestamp value. If you really need to replicate
the timestamp value, you need to change the column on the target side to
binary(8).

--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Author
27 Aug 2006 10:23 AM
Joy
Hi,
What if i cannot change the tagret column to binary(8), is there still a way
for explicit insert, because the business proposition is such i cannot alter
the table schema.

regards,
Joy

Show quote
"Erland Sommarskog" wrote:

> Joy (J**@discussions.microsoft.com) writes:
> > Well i want to insert explicit value into timestamp values because i am
> > writing a process that is similar to replication meaning i have front
> > end and back end database and the front end has some tables that have
> > columns of type timestamp and when i run my process the respective rows
> > at both the databases should have the same value.

> Why would you want the timestamp value to be same value? Does that serve
> any purpose, or is just because it looks nice?
>
> You cannot explicitly set a timestamp value. If you really need to replicate
> the timestamp value, you need to change the column on the target side to
> binary(8).
>
> --
> Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se
>
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
>
Author
27 Aug 2006 11:52 AM
Erland Sommarskog
Joy (J**@discussions.microsoft.com) writes:
> What if i cannot change the tagret column to binary(8), is there still a
> way for explicit insert,

No, there isn't. Again, I ask you to consider if you really need to
duplicate the timestamp value.

> because the business proposition is such i cannot alter the table
> schema.

You have two options:
1) Don't duplicate the timestamp value.
2) Change the target table.
No matter what your business proposition is.
--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Author
27 Aug 2006 12:22 PM
Joy
Hi,
Now i am faced with a situation where i need to update a table that doesn't
have a primary key how do i make sure i have the least number of columns in
where clause.

regards,
Joy

Show quote
"Erland Sommarskog" wrote:

> Joy (J**@discussions.microsoft.com) writes:
> > What if i cannot change the tagret column to binary(8), is there still a
> > way for explicit insert,

> No, there isn't. Again, I ask you to consider if you really need to
> duplicate the timestamp value.
>
> > because the business proposition is such i cannot alter the table
> > schema.
>
> You have two options:
> 1) Don't duplicate the timestamp value.
> 2) Change the target table.
> No matter what your business proposition is.
> --
> Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se
>
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
>
Author
27 Aug 2006 12:41 PM
Roy Harvey
On Sun, 27 Aug 2006 05:22:02 -0700, Joy
<J**@discussions.microsoft.com> wrote:

>Hi,
>Now i am faced with a situation where i need to update a table that doesn't
>have a primary key how do i make sure i have the least number of columns in
>where clause.
>
>regards,
>Joy

If the table does not have a primary key, or a unique constraint or
index, it may not be possible to update a single row.  This
demonstrates at a very practical level why every table needs a PK.

When faced with a table that does not have a PK, the question should
be "Is there a potential PK?"  Ideally a person familiar with the data
will be able to suggest an answer, which will be one or more columns.
That answer can be tested with the following query:

SELECT <list of candidate key columns>, count(*) as Duplication
FROM <table without PK>
GROUP BY <list of candidate key columns>
HAVING COUNT(*) > 1

If this query returns anything the columns will not work as a PK.

Now, even if there are no results there may still be problems.  A PK
can only include columns that do not allow NULL.; the alternative for
that is a UNIQUE contraint or index, which do not have that
restriction.  What can be a much bigger problem is when the data in
the table at the time the test above is run is not representative of
the data that can occur.

Roy Harvey
Beacon Falls, CT
Author
26 Aug 2006 8:19 PM
Mike C#
"David Portas" <REMOVE_BEFORE_REPLYING_dpor***@acm.org> wrote in message
news:1156618663.592022.61210@74g2000cwt.googlegroups.com...
> Joy wrote:
>> Hi all,
>> I am writing a Insert trigger but to my dismay it is not able to insert
>> explicitly into columns of type Timestamp.
>>
>> Please let me know the work around for this.

How about using a DATETIME column with a default value of GETDATE() ?

AddThis Social Bookmark Button