Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.ms-sqlserver > #1162

Re: SS2005, test that object exists / OBJECT_ID() NOT NULL, reliable?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-out.readnews.com!transit3.readnews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From rja.carnegie@gmail.com
Newsgroups comp.databases.ms-sqlserver
Subject Re: SS2005, test that object exists / OBJECT_ID() NOT NULL, reliable?
Date Thu, 5 Jul 2012 06:02:15 -0700 (PDT)
Organization http://groups.google.com
Lines 86
Message-ID <e35493c0-5a56-4384-83e2-9c29fd1b2029@googlegroups.com> (permalink)
References <32eaaf26-9cca-4dbc-a4e7-e5bf92635876@googlegroups.com> <XnsA084C2D1F8717Yazorman@127.0.0.1>
NNTP-Posting-Host 194.83.173.135
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1341494325 24542 127.0.0.1 (5 Jul 2012 13:18:45 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Thu, 5 Jul 2012 13:18:45 +0000 (UTC)
In-Reply-To <XnsA084C2D1F8717Yazorman@127.0.0.1>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=194.83.173.135; posting-account=dELd-gkAAABehNzDMBP4sfQElk2tFztP
User-Agent G2/1.0
Xref csiph.com comp.databases.ms-sqlserver:1162

Show key headers only | View raw


On Monday, July 2, 2012 6:09:05 PM UTC+1, Erland Sommarskog wrote:
> > I have had an odd error message today from 
> > "Microsoft SQL Server 2005 - 9.00.4035.00 (X64)"
> > that suggests that the following test did not work 
> > as intended, which was to generate and execute 
> > dynamic SQL code to create a table (by SELECT INTO) 
> > if the table does not already exist.  The table 
> > does exist, so I would expect OBJECT_ID(...) NOT NULL.
> > The "path" consists of a database name and ".[dbo].",
> > I'm reasonably sure.  And apparently it works /now/.
> > 
> > Should I be using a different "object there" test?
> > 
> > ----
> > 
> > SET @tblPeriod_fq = 
> >         @pathTarget_q + N'[period]'
> > 
> > IF ( OBJECT_ID(@tblPeriod_fq) IS NULL )
> > BEGIN
> > ....
>  
> What is the odd error message? How did the complete batch look like?

Sorry for the late reply.  That isn't the error
message, it wasn't really odd - just, that table 
"period" already existed, when the code was trying 
to create it.  The odd thing was that it happened -
and then that it didn't happen when I re-ran 
the procedure. 

Below is more of the code that I think generated 
the error message.  The inside part should only
be running after the "OBJECT_ID() IS NOT NULL" 
test, which is why I was thinking - apart from 
some stupid mistake elsewhere in the program - 
OBJECT_ID() might be sometimes unreliable when
used this way - and then I'd have to stop 
laughing at colleagues' other designs for 
"does the object exist" routines.  But maybe 
I'll just stay puzzled.

...and I wonder why we haven't installed 
Microsoft SQL Server 2005 Service Pack 4
(December 2010).

-----------------------------------------

/* Determine fully qualified table name of period table.  Copy if missing.
*/

SET @tblPeriod_fq = 
        @pathTarget_q + N'[period]'

IF ( OBJECT_ID(@tblPeriod_fq) IS NULL )
BEGIN
    /* Copy latest row of [BO-StudNN]..period to create [BO-SurveyNN]..period.
    */
    IF ( @spverbose = 1 ) PRINT 'Creating period table (years)'

    SET @template = 
    N'
    USE @{dbStud};
    DECLARE @year int;
    SET @year = (SELECT MAX(year) FROM period);
    SELECT * INTO @{tblPeriod} FROM period WHERE ( year = @year );
    --X'
    
    SET @workstring = 
            REPLACE(REPLACE(
                      @template
                    , N'@{dbStud}',    @dbStud_q    )
                    , N'@{tblPeriod}', @tblPeriod_fq)
    ;
    
    IF (RIGHT(@workstring, 3) = N'--' + N'X' )
    BEGIN
        PRINT @workstring
        EXEC sp_executesql @workstring
        IF ( @@ERROR <> 0 ) GOTO failure
    END
    ELSE BEGIN
        RAISERROR(N'ERROR - constructing workstring', 16, 1)
        GOTO failure
    END
END

Back to comp.databases.ms-sqlserver | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

SS2005, test that object exists / OBJECT_ID() NOT NULL, reliable? rja.carnegie@gmail.com - 2012-07-02 09:35 -0700
  Re: SS2005, test that object exists / OBJECT_ID() NOT NULL, reliable? Erland Sommarskog <esquel@sommarskog.se> - 2012-07-02 19:09 +0200
    Re: SS2005, test that object exists / OBJECT_ID() NOT NULL, reliable? rja.carnegie@gmail.com - 2012-07-05 06:02 -0700
      Re: SS2005, test that object exists / OBJECT_ID() NOT NULL, reliable? Erland Sommarskog <esquel@sommarskog.se> - 2012-07-05 23:45 +0200

csiph-web