Solved

Stored procedure - Today's Date as temp variable.

Posted on 2004-10-05
7
3,678 Views
Last Modified: 2007-11-27
i'm creating a stored proc.

CREATE PROCEDURE test

AS
      select convert(varchar(10), getdate(), 101) as TodaysDate

      INSERT INTO  TableName ( startDate, endDate )
      VALUES( TodaysDate, TodaysDate-5 )


how can i get this to work?
0
Comment
Question by:efarhat
7 Comments
 
LVL 10

Accepted Solution

by:
ALaRiva earned 125 total points
ID: 12232105
CREATE PROCEDURE test

AS
     DECLARE @TodaysDate DATETIME
     SET @TodaysDate = GETDATE()

     INSERT INTO  TableName ( startDate, endDate )
     VALUES( TodaysDate, TodaysDate-5 )

However, one thing I would like to mention is that you should not be storing the endDate.  This is a calculated value and shouldn't be stored in the database, but rather calculated on demand.

HTH

-Anthony
0
 
LVL 4

Expert Comment

by:strangelynormal1
ID: 12232171
CREATE PROCEDURE test
AS
DECLARE @TodaysDate DATETIME
SELECT   @TodaysDate = CONVERT(DATETIME,convert(varchar(10), getdate(), 101))

     INSERT INTO  TableName ( startDate, endDate )
     VALUES( @TodaysDate , @TodaysDate -5 )

This seems awkward since you're @TodaysDate-5 = 5 days ago, so the endDate would be earlier than the startDate.

Also, realize that when you convert to VARCHAR(5), then it's not a datetime...
0
 

Author Comment

by:efarhat
ID: 12232322
ok, got it to work but i ran into a problem.  i had to delete a entry in the table and because the ID for one of the columns is an IDENTITY, I now have a missing ID. It doesn't recalculate?

| ID |  field2  |
   1      data1
   3      data3
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 10

Expert Comment

by:ALaRiva
ID: 12232338
An IDENTITY Field is not intended to be a sequential number.  It's simply put, an IDENTITY for that record.  It can't be duplicated and doesn't necessarily have to be sequential without gaps.  

HTH

-Anthony
0
 
LVL 9

Expert Comment

by:xenon_je
ID: 12232353
First of all you don't need any variable to do this, just a simple insert...like in the example bellow:


CREATE PROCEDURE test

AS
     INSERT INTO  TableName ( startDate, endDate )
     VALUES( convert(varchar(10), getdate(), 101), convert(varchar(10), DateAdd(dd, -5, getdate()), 101))

Second as sugested above, you tried to use variables, and the syntax of variables is that they all start with an @... as it was sugested above.. :))


regards,
  xenon
0
 
LVL 34

Expert Comment

by:arbert
ID: 12232773
If you want to "reset" your identity, you can use the DBCC CHECKIDENT function.  However, like was already said above, you're not guaranteed to have sequences without gaps in the future because of deletes and rolled back transactions...
0
 
LVL 9

Expert Comment

by:xenon_je
ID: 12242985
identity is more for having a unique key, and a short one (is of int type)..... It does not guarantee that the values are like 1,2,3,4...... Why? because for example you delete a few records, and in the table you have 1 billion records... You think in this case it will update the other records just to have all consecutive values? Also as above sugested there are rollbacks...

So identity is an easy way to ensure that you have a field that have unique values, and that is short, and so the index on it will work very fast...

regards,
 xenon
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question