have a table with a transaction date in it. It is set as a timestamp and basically I want on creation of a new record that field automatically inserts the current date. I am using visual studio 2010 and ASP.net SQL not sure how to go about it. Dont need SQL injection protection for this just a simple way of doing it. Any ideas?
Mark
Oracle has a SYSDATE() function,
SqlServer has GETDATE()
MySQL has NOW()
Use it in your DML (SELECT, INSERT) statements
INSERT INTO [AdventureWorks].[dbo].[ErrorLog]
([ErrorTime]
,[UserName]
,[ErrorNumber]
,[ErrorMessage]
)
VALUES
(GetDate() --This will work for MSSQL
,'userName'
,7551
,'I inserted the current date/Time'
)
or (again for SQL server)
ALTER TABLE dbo.ErrorLog ADD CONSTRAINT
DF_ErrorLog_ErrorTime_current_DateTime DEFAULT (getdate()) FOR ErrorTime)
If the above code is in a stored procedure, then getDate() should work. If it is a sql command string that you are sending via ado.net, then you need to do this (example in VB):
sqlString="Insert into myTable (myDateTimeColumn) Values ('" & now() & "')"
myCommand.Execute(sqlString) ... etc.
Related
I am using SQL Server 2008.
This is my Table definition.
create table myTable (nid int identity(1,1),content nvarchar(max));
when I execute these two queries
insert into myTable(content)values(N'हिंदी में परीक्षण');
update myTable set content=N'हिंदी में परीक्षण' where nid=1;
the other language data is inserted/updated in the table. I want to do the same in a stored procedure. This is the definition of my stored procedure.
create proc myProc
#nid int,
#content nvarchar(max)
as
begin
update myTable set content=#content where nid=#nid;
end
it just doesn't work the column is updated with ????????????? value. What should I do. I tried to do this
update myTable set content=N''+#content where nid=#nid;
But this doesn't work as well. Please Help.
I forgot to tell one thing
exec myProc 1,N'हिंदी में परीक्षण'
this does work of course it does the problem is i send data from an asp .net web application where i use this syntax.
public void myProcCall()
{
dbcommand = db.GetStoredProcCommand("myProc", nid, content);
ds = db.ExecuteDataSet(dbcommand);
}
So when I send other language content from the web application the value is updated as ??????????. Do I have to make some changes in my asp .net code.
This will work
EXEC myProc 1, N'हिं में पदी रीक्षण'
This won't
EXEC myProc 1, 'हिं में पदी रीक्षण'
Ergo, the parameter is sent as non-unicode
You need to ensure that the code that populate the parameter sends unicode
I am using OrmLite Oracle in C#. I want to insert current sysdate instead of DateTime.Now in column having date data type, i.e. taking the date at the database end and not the calling code. How can I do this?
I have a web form that inserts a row into an SQL Server 2008 table. Table has a field which must be unique, but for some reasons we are not allowed to use autoincrement utility. Instead of this I select the maximum of this field and increment it from code and insert the row with that id.
The problem is, if more than one person uses this form simultaneously, there occurs a concurrency problem and same id will be inserted to the table. I do not know how to solve this issue in a asp.net web project because all users have their own session and threads.
Any idea on how to manage concurrency problems in asp.net project will be very useful.
Thanks.
If you perform the insert into a stored procedure, or in a transaction in the code, I don't see a reason to have problems with concurrency. Something like:
BEGIN TRAN
DECLARE #MAX int
SELECT #MAX = MAX(ID) FROM MyTable
INSERT INTO MyTable (#MAX, VAlue, Value2..)
COMMIT TRAN
or
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
transaction = connection.BeginTransaction("InsertRow");
... perform insert using command object ...
transaction.Commit();
The same query ran in PL/SQL Developer shows time, however it does not show in Oracle SQL Developer. Is there a way to get the time in SQL Developer?
In SQL Developer:
In PL/SQL:
This is the details of the date field:
You can change this in the Tools / Preferences dialog of SQL developer:
Select Database / NLS Parameters in the tree view on the left.
Then put dd/mm/yyyy hh24:mi:ss into the Date Format field.
Press OK to close the dialog.
To make it simple:
use to_char() function, it will work with SQL developer as well as Pl/sql.
select to_char(sysdate,'MM-DD-YYYY HH:Mi:SS') from table_name;
Can't see your query, but in oracle, it would look like this:
select sysdate from dual
I'm building a website using ASP.NET and SQL Server, and I use
SELECT PK FROM Table WHERE PK = ##identity
My question is which is better and more reliable to retrieve the last inserted PK for multiuser website, using ##identity or using this:
SELECT MAX(PK) FROM Table WHERE PK = Session ("UserID")
I'm not sure exactly what you want to achieve, but the recommended way to retrieve the primary key value of the last statement on a connection is to use SCOPE_IDENTITY()
##Identity is particularly risky where you are using triggers, since it returns the last generated identity value, including those generated by triggers flowing on from a statement.
MSDN has the following to say:
SCOPE_IDENTITY and ##IDENTITY return
the last identity values that are
generated in any table in the current
session. However, SCOPE_IDENTITY
returns values inserted only within
the current scope; ##IDENTITY is not
limited to a specific scope.
You should certainly use SCOPE_IDENTITY() in favour of the MAX(PK) approach - any number of possible future changes could invalidate this method.
For SQL Server 2005 and above...
You can do the INSERT and SELECT in one call using the OUTPUT clause...
INSERT MyTable (col1, col2, ..., coln)
OUTPUT INSERTED.keycol, INSERTED.col1, INSERTED.col2, ..., INSERTED.coln
VALUES (val1, val2, ..., valn)
Otherwise, you only use SCOPE_IDENTITY()
As mentioned by #David Hall the ##IDENTITY keyword returns the most recently created identity for your current connection, not always the identity for the recently added record in your query and may return an incorrect value. Using MAX(PK) there is a higher chance for an incorrect value and I'd strongly recommend against using it. To avoid the any race conditions I'd suggest that you use SCOPE_IDENTITY() to return the identity of the recently added record in your INSERT SQL Statement or Stored Procedure.
Depends on what you're trying to accomplish. If you want to return the just-generated ID to the ASP.NET code (a typical scenario), then ##identity is your friend. In a high-concurrency situation, mak(PK) is not even guaranteed to be the PK you're after.