Solved

Stored procedure - Today's Date as temp variable.

Posted on 2004-10-05
7
3,674 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 10

Expert Comment

by:ALaRiva
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now