?
Solved

Stored procedure - Today's Date as temp variable.

Posted on 2004-10-05
7
Medium Priority
?
3,689 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

764 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