Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Monday, March 19, 2012

Maintaining Variable After EXEC

Hello,

I am fairly new at stored procedures. I have created some that will
go through a table and return a start date and an end date that is
dependent upon the fiscal period you want, but I then need to use
those dates in another stored procedure to retrieve the information I
need. My stored procedure looks like this.

================================================== ====================

CREATE PROCEDURE dbo.R920ExtTotal
@.MthsBack Decimal OUTPUT
AS

DECLARE @.sSQL AS NVARCHAR(255), @.StartDate as SMALLDATETIME, @.EndDate
as SMALLDATETIME

Exec @.StartDate = GetMthStart @.MthsBack

Exec @.EndDate = GetMthEnd @.MthsBack

SET @.sSQL = 'Select count(extension) as Total From r920f00 Where
([date] BETWEEN "' +
CONVERT(nvarchar, @.StartDate) +
'" and "' +
CONVERT(nvarchar, @.EndDate) +
'")'

Select @.sSQL

EXEC (@.sSQL)

Return
GO

================================================== ===============

The problem is my variables @.StartDate and @.EndDate do not retain
their values after the EXEC statement and revert to 01/01/1900. How
can I get around this problem?

Thanks!!!!

ChipVariables are only available in the scope within which they are declared. If
you need to move values out of a stored procedure, you must use output
parameters for all of them. Also, why are you building dynamic SQL in your
procedure? You can use the variables directly in SQL queries - there is no
need to convert them to strings, and insert them into a SQL string.

On 6 Jan 2004 11:05:01 -0800, cmayan@.lesliecontrols.com (Chip Mayan) wrote:

>Hello,
>I am fairly new at stored procedures. I have created some that will
>go through a table and return a start date and an end date that is
>dependent upon the fiscal period you want, but I then need to use
>those dates in another stored procedure to retrieve the information I
>need. My stored procedure looks like this.
>================================================== ====================
>CREATE PROCEDURE dbo.R920ExtTotal
>@.MthsBack Decimal OUTPUT
>AS
>DECLARE @.sSQL AS NVARCHAR(255), @.StartDate as SMALLDATETIME, @.EndDate
>as SMALLDATETIME
>Exec @.StartDate = GetMthStart @.MthsBack
>Exec @.EndDate = GetMthEnd @.MthsBack
>SET @.sSQL = 'Select count(extension) as Total From r920f00 Where
>([date] BETWEEN "' +
>CONVERT(nvarchar, @.StartDate) +
>'" and "' +
>CONVERT(nvarchar, @.EndDate) +
>'")'
>Select @.sSQL
>EXEC (@.sSQL)
>Return
>GO
>================================================== ===============
>The problem is my variables @.StartDate and @.EndDate do not retain
>their values after the EXEC statement and revert to 01/01/1900. How
>can I get around this problem?
>Thanks!!!!
>Chip|||Chip Mayan (cmayan@.lesliecontrols.com) writes:
> I am fairly new at stored procedures. I have created some that will
> go through a table and return a start date and an end date that is
> dependent upon the fiscal period you want, but I then need to use
> those dates in another stored procedure to retrieve the information I
> need. My stored procedure looks like this.
>================================================== ====================
> CREATE PROCEDURE dbo.R920ExtTotal
> @.MthsBack Decimal OUTPUT
> AS
> DECLARE @.sSQL AS NVARCHAR(255), @.StartDate as SMALLDATETIME, @.EndDate
> as SMALLDATETIME
> Exec @.StartDate = GetMthStart @.MthsBack
> Exec @.EndDate = GetMthEnd @.MthsBack
> SET @.sSQL = 'Select count(extension) as Total From r920f00 Where
> ([date] BETWEEN "' +
> CONVERT(nvarchar, @.StartDate) +
> '" and "' +
> CONVERT(nvarchar, @.EndDate) +
> '")'
> Select @.sSQL
> EXEC (@.sSQL)
> Return
> GO

I'm afraid that there are a couple of errors or strange things in this
procedure.

First: there is absolutely no reason to use dynamic SQL here. Just write:

SELKCT count(extension) AS Total
FROM r920f00
WHERE [date] BETWEEN @.StartDate AND @.EndDate

Second: the calls to set @.StartDate and @.EndDate looks funny. If
GetMthStart and GetMthEnd are user-defined functions it would be alright,
but you indicated that they were stored procedures. The return value from
a stored procedure is always an integer value, so you cannot return a
date here. And I would strongly recommend you to use return values solely
for indication of success/failure (with 0 meaning success, and about
everything else meaning failure.) So you would have to make the output
parameters:

EXEC GetMthStart @.MthsBack, @.StartDate OUTPUT

Third: the @.MthsBack parameter is declared as output, but you never assign
it any value, you only seem to use it for input.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Maintaining a Date/Time Last Updated Value

Using SQL Server 2000...
In support of a Web site's product catalogs; we will have at least two
tables [Products] and [ProductCatalogs].
The application needs to show the Date/Time any given product catalog was
last updated. For purposes of this "last updated date/time" value, we
consider the catalog as having been updated when [anything about the
catalog, itself] has been updated OR [any items presented in the catalog]
have been updated. This means one Date/Time value when either (1) the given
catalog's row in [ProductCatalogs] or (2) any associated rows in [Products]
have been updated. We do not need a complete update history - we only want
to know the last date/time an update happened to either the given catalog or
to any of the items presented in the catalog (and not any "per item" update
date/time).
One solution would be to have a column in [ProductCatalogs] of the DateTime
data type. The value in this column gets maintained by triggers on both
[Products] and [ProductCatalogs].
While that solution would work, I'd appreciate your perspective, opinions,
and alternatives - if any - before I go off and implement this.
Thanks!As far as I can see, you only need one datetime field on the table
ProductCatalogs. If you have only one way of updating your data (through
stored procedure) I would recommend implementing this logic in the stored
procedure. If not (or you think that someone could change data without using
sp) you would need to implement triggers.
MC
"Jeff" <Jeff@.NoSpam.com> wrote in message
news:u87GGjH9FHA.3020@.TK2MSFTNGP09.phx.gbl...
> Using SQL Server 2000...
> In support of a Web site's product catalogs; we will have at least two
> tables [Products] and [ProductCatalogs].
> The application needs to show the Date/Time any given product catalog was
> last updated. For purposes of this "last updated date/time" value, we
> consider the catalog as having been updated when [anything about the
> catalog, itself] has been updated OR [any items presented in the catalog]
> have been updated. This means one Date/Time value when either (1) the
> given catalog's row in [ProductCatalogs] or (2) any associated rows in
> [Products] have been updated. We do not need a complete update history -
> we only want to know the last date/time an update happened to either the
> given catalog or to any of the items presented in the catalog (and not any
> "per item" update date/time).
> One solution would be to have a column in [ProductCatalogs] of the
> DateTime data type. The value in this column gets maintained by triggers
> on both [Products] and [ProductCatalogs].
> While that solution would work, I'd appreciate your perspective, opinions,
> and alternatives - if any - before I go off and implement this.
> Thanks!
>

Saturday, February 25, 2012

Magic Date?

Anyone know why 1899-12-30 is a special date?
If you put that date with a time in sql server EM and a vb call will return only the time portion...
More background and sample code
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=30709In vb, the date is a floating-point where the integer portion is the date. 12/30/1899 is the base date. So if you enter a time only, the integer portion of the datetime will be 0 and the decimal portion will be the time.|||What's EM Built in? If you just enter a time through EM, it'll ne 1899-12-30...

And isn't 1900-01-01 the 0 date for sql server?

SELECT CONVERT(datetime,0,101)

???

And why, if you call sql server from vb through ado, does it pass back just the time component...with no conversion function?|||I wonder if there is a magic time, that just returns the date...

Why the original developer didn't use CONVERT is betond me...|||Do a query against that datetime column that only has a time and add 1 to it - your question will be answered.|||SELECT CONVERT(datetime,-2,101)
gives '1899-12-30 00:00:00.000'|||Originally posted by rnealejr
Do a query against that datetime column that only has a time and add 1 to it - your question will be answered.

What does that mean?

Datetime is stored as a number...4 before the decimal, 4 after...

What do you mean time only?|||So what does this prove?

SELECT DATEADD(d,1,CONVERT(datetime,0.1))

Add 1 what?

What do you mean with no date? There's always a date component?

0 is the default

(Unless of course you add through EM then it's -2)

huh?

And why, if you make a sql call from vb, and the date is 1899-12-30, it only returns the time..VERY bizzare|||Just had the developer do it from an Excel workbook too.

It puts just the time in the Cell with that 1899-12-03 date

It doesn't return the date...bizzaro

Anyone else seen this?|||http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27101

Hey Brett ... some more with excel problems ...

I believe the reason is that while SQL takes the default date to be 1900-01-01

and other MS applications use 1899-12-30

Here is a link
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbadev/html/whatisdatehowdiditgetthere.asp|||Thanks...forgot all about that thread...

Still doesn't explain you only get the decimal portion of the datetime field though (that's the time component)