How to use TIMESTAMP option in sybase - database

Hope all are doing great,
I want to create a table in sybase database and the table should have a column with current system time, So i plan to go with TIMESTAMP datatype.
But it not let me to create the table, tried with different way of declaring.
So please provide me a simple table creation with timestamp options..
(Note : i dont want to manually insert system time into the column while inserting, it should be auto update the column )
Tried with TIMESTAMP, Default timestamp, current timestamp,, everything i found by surfing..
so please suggest with any simple table creation query..
THanks in advance

According to me Curent Timestamp in Sybase is inserted using GETDATE() rahter than any other method.

Related

Adding last change timestamp to a table in snowflake

I have a lots of tables in Snowflake that I am updating them ( basically re-creating them) every day with a python script.
I can see the timestamp of the last time those tables have been changed in information schema of my database but how can I add the column or that information to one of our tables?
Assume that I have a table customer and I want to be able to see when was the last time that each row of that table has been changed. I can see this timestamp here:
SELECT CONVERT_TIMEZONE('Etc/GMT+9','UTC',last_altered) AS last_changed
FROM "XXXX"."INFORMATION_SCHEMA"."TABLES"
WHERE table_name='CUSTOMERS';
how to add this information to customer table?
If you would like to see that information your python program should add that information as additional columns in each row. We used to call these columns as 'WHO COLUMNS', below are the WHO COLUMNS that we added to each table in the final schema
Last Updated TimeStamp
Last Updated User
Creation Timestamp
The best option would be to add an additional audit column to the customer table with a default value as current_timestamp
Example:
CREATE TABLE CUSTOMER (column1 varchar, insert_date timestamp default current_timestamp())
In this example you can use insert_date to track when that record is inserted. The column would be auto-populated whenever you are inserting a row like this.
INSERT INTO CUSTOMER(column1) VALUES ('test')

Adding table records with type Date manually

I'm using Visual Studio connected to my SQL Server to create a new database and populate a table with some mock data for application development testing. I created a table with 5 fields, an auto-increment PK, three nvarchar(50) fields and a date. When I view the table data and attempt to add a row, it doesn't allow me to type into the Date field nor give me any way to insert a date into the field. How can I accomplish this?
I was not descriptive enough and it turns out it was a confusion between a timestamp and a datetime datatype. I was trying to use timestamp thinking when I did an insert it would give the CURRENT_TIMESTAMP. As it turns out the timestamp is really rowversion and has nothing to do with an actual datetime. I have since changed the datatype to datetime and have had no problems.

Is there any way to update the modified date time automatically in SQL Server?

Is there any way to update the modified date time automatically in SQL Server.
I do not want to use Triggers. Also I want to avoid providing the value through application while calling SQL query.
Is there any support in SQL or in Dapper etc.
If you want to keep track of the changes in database you can use a feature called
System-Versioned Temporal Table as explained here.
Using a Temporal Table, you will be able to query the recent state of the row as usual, in addition to the ability to query the full history of that row
It's very handy if you are interested in keeping a history of data changes
I am able to solve the problem using Temporal Table. I am not sure is this a elegant solution. Here is how i solved.
Create Table:
CREATE TABLE extable4 (PriKey int PRIMARY KEY, ColValue varchar(200)
, [ModifiedDateTime] datetime2 (2) GENERATED ALWAYS AS ROW START
, [ModifiedExpiryDateTime] datetime2 (2) GENERATED ALWAYS AS ROW END HIDDEN
, PERIOD FOR SYSTEM_TIME (ModifiedDateTime,[ModifiedExpiryDateTime])
) ;
Insert a record with out providing input to ModifiedDatetime.
insert into extable4(PriKey,ColValue) values(1,'Ver 1');
ModifiedDateTime Populated with systime.
update extable4 set ColValue='Ver 1.1' where PriKey=1;
ModifiedDateTime updated now. :)

Identity column in a budget table

Programming noob here, and DBA I am not..
I am creating a table called 'budget' on a 2005 sqlserver databases. The purpose of this table is to simply store the monthly $ allowed to departments for budgeting purposes.
The essential columns in this table will be month, year, dept, amount. I am migrating this from an old foxpro table which did not have an identity/primary key column.
My question is, for my purposes do I need to worry about creating an identity column? I am having a hard time importing the data into SQLserver and having it populate the ID column, so I am inclined to just skip it if it's not needed. Thanks for your $.02
If you're specifying a PK in your insert statement, you'll need to use SET IDENTITY_INSERT <tableName> ON at the beginning of your query.
For more information, look here.

Compute hash-value of entered value when insert or update using sql server 2008 by triggers

I have a table with two columns {FlatContent, HashedContent}. Now I want to automatically compute the hash value of FlatContent when new row was inserted or an existing row was updated. To date, I've never used from trigger, so I can't do this by trigger or another approach which is exist to solve this issue.
Thanks if anybody can help me ;)
Instead of using a trigger, make HashedContent a persisted computed column in your table definition.
ALTER TABLE YourTable
ADD HashedContent AS HashBytes('SHA1', FlatContent) PERSISTED

Resources