I have a variable in some code.
DECLARE @.LastExportDateTime datetime
I then set the variable.
SET @.LastExportDateTime = (SELECT TOP 1 ExportDatetime FROM MyTable ORDER BY
ExportDatetime DESC)
The first time I run this in production @.LastExportDateTime will be null and
I'm concerned that my code will not like this. I rather set it to date in
the past, '20000101'
Can I set the default value in the DECLARE statement?
I see the default clause in BOL but I would like a syntax example.
Thanks> Can I set the default value in the DECLARE statement?
No.
But you can say
SELECT @.LastExportDateTime = MAX(ExportDateTime) FROM MyTable;
SET @.LastExportDateTime = COALESCE(@.LastExportDateTime, '20000101');|||"Terri" <terri@.cybernets.com> wrote in message
news:duhoc2$rf4$1@.reader2.nmix.net...
>I have a variable in some code.
> DECLARE @.LastExportDateTime datetime
> I then set the variable.
> SET @.LastExportDateTime = (SELECT TOP 1 ExportDatetime FROM MyTable ORDER
> BY
> ExportDatetime DESC)
> The first time I run this in production @.LastExportDateTime will be null
> and
> I'm concerned that my code will not like this. I rather set it to date in
> the past, '20000101'
> Can I set the default value in the DECLARE statement?
> I see the default clause in BOL but I would like a syntax example.
> Thanks
No.
Here are 2 ways you can do this:
SET @.LastExportDateTime = (SELECT TOP 1 ExportDatetime FROM MyTable ORDER BY
ExportDatetime DESC)
IF @.LastExportDateTime IS NULL
SET @.LastExportDateTime = '20000101'
SET @.LastExportDateTime = ISNULL((SELECT TOP 1 ExportDatetime FROM MyTable
ORDER BY ExportDatetime DESC), '20000101')|||> But you can say
> SELECT @.LastExportDateTime = MAX(ExportDateTime) FROM MyTable;
Is MAX preferable to TOP 1...ORDER BY DESC from a performance perspective?|||> Is MAX preferable to TOP 1...ORDER BY DESC from a performance perspective?
I don't think it will make a difference, but it is a lot shorter to type.
No comments:
Post a Comment