?
Solved

Stored procedure - Today's Date as temp variable.

Posted on 2004-10-05
7
Medium Priority
?
3,692 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 10

Accepted Solution

by:
ALaRiva earned 375 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

649 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