SQL GETDATE() in insert operations using Linq To Sql - sql-server

Say i have the following table:
create table T (
ID int not null primary key,
Name varchar(10) not null,
CreatedAt datetime not null default GETDATE(),
UpdatedAt datetime not null default GETDATE()
)
and want to use with Linq 2 Sql. It perfectly generates type safe class with both CreatedAt and UpdatedAt properties non-nullable. Now i want to insert a new entry and want them both to be populated by a value from Sql Server, not with DateTime.Now which may differ. Is there a way to somehow send null to Sql Server without making the properties nullable? Also, i don't want to follow this solution as it requires additional network trip. It's very easy with good old SQL - just omit CreatedAt/UpdatedAt columns in the insert statement and you're fine but what are my options with Linq 2 Sql?

Can you check this link. there is a attribute called IsDbGenerated which can be annotated with your LINQ partial classes.
http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.columnattribute.isdbgenerated.aspx#Y456

Related

Can SQL Server timestamp type column be NULL?

I read the document on "CREATE TABLE" at https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql?view=sql-server-ver15
It said
timestamp data types must be NOT NULL.
However, when I create a table, I can create a field with timestamp type and make it nullable. So, what is the problem?
Update
When using the following query:
USE MyDB6;
CREATE TABLE MyTable (Col1 timestamp NULL);
I expect an error saying the column Col1 cannot be NULL but nothing happens.
After creating the table, I run the following query:
USE MyDB6
SELECT COLUMNPROPERTY(OBJECT_ID('MyTable', 'U'), 'Col1', 'AllowsNull');
I expect the result is 0, but actually it is 1.
So, my question is, though the document has said "timestamp data types must be NOT NULL.", and in the real cases, this data type will also not be NULL, why the create table query does not prevent me from setting it to nullable and the system still save the column as nullable?
Like marc_s said in their comment, this datatype is handled internally and will never be null. Try the following:
declare #test timestamp = null -- ROWVERSION would be less confusing
select #test
It does not return NULL
As to why you're allowed to set it to nullable; what would be won by creating this deviation from the standard? You cannot INSERT NULL into a TIMESTAMP/ROWVERSION column, you cannot UPDATE it at all. I imagine it is quite a lot of trouble to alter the CREATE syntax to make certain datatype not nullable; more trouble than its worth.

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.

EF - pass null value to SQL column so default value gets inserted

SQL Server table T2 has 2 columns:
Id INT NOT NULL
CreateDate DateTime NOT NULL, default = (getdate())
This statement inserts the CreateDate value correctly because it uses (getdate()) as default.
Insert T2 (Id)
Values (1)
So far so good. The problem is when I use Entity Framework to insert a row and still wish to use the default (getdate()) value.
Because the CreateDate is defined as NOT NULL, I cannot leave it blank or leave out of the Insert statement when using EF. But I want SQL to generate the timestamp on the server/database side.
Is there a way to handle this?
Thanks to squillman's reference to another SO post, I was able to find the answer.
Go to EDMX diagram, and you can set the StoreGeneratedPattern property to achieve what I am trying to do.
There are three Database Generated Options
Computed : The database generates a value when a row is inserted or updated.
Identity : The database generates a value when a row is inserted.
None : The database does not generate values.
EDIT: Although the picture shows Identity, I had to change it to Computed. The reason is that Identity option only works if the row is Inserted only. If the row is ever updated (other columns updated), then it caused an error. The Computed option seems to work fine with Insert (runs the default script) and Updates (to other columns, default script does not run again).

SQL function in Hibernate insert statement

How do include a function like getdate() in an insert statement for Hibernate query?
Lets say one of the properties of a mapped class is a datetime column how do put specify getdate() in order for the .save() method call to use the server timestamp?
Simple answer is don't include it in the insert statement and let SQL do it.
In the SQL table designer where you have the date column you are inserting into, set the Default Value to GETDATE() in the column properties and SQL Server will handle it for you.
Reference and sample: SQL Server GETDATE() Function
Table create script will look like this:
CREATE TABLE Orders
(
OrderId int NOT NULL PRIMARY KEY,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT GETDATE()
)

SQLserver Timestamp trying to set a default getting unreadable result

I have a time stamp field in a table and I've unticked the box in designer for allowing Nulls
I'm unable to enter anything in default value and binding field ( this is greyed out and doesn't allow you type anything )
I'm trying all my sql experiments out in the query designer of sql server express 2008
If I Insert a new record into the table the timestamp field gives a value that looks like:
0x00000000000007D7
As you can see this is totally unreadable:
How can I get round this/ get a readable time stamp in there?
Use DATETIME with a default constraint of GETDATE
You can do that like this:
CREATE TABLE myTable
(
ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
myTimeStamp datetime NOT NULL DEFAULT GETDATE()
)
TIMESTAMP is a binary field used for row versioning and cannot be edited.
From BOL:
timestamp is a data type that exposes automatically generated binary numbers, which are guaranteed to be unique within a database.
timestamp is used typically as a mechanism for version-stamping table
rows. The storage size is 8 bytes.

Resources