Database Exists?



  • SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[uspDatabaseExists]
    AS
    -- =============================================
    -- Author:		Someone
    -- Create date: 2009-05-08
    -- Description:	Returns the value 1, can be used to check if the database is connected/exists.
    -- =============================================
    
    BEGIN
    	SELECT 1 AS DatabaseExists
    END
    


  • Very.. uhh... interesting way of checking if a database is connected/exists.

    You know, I really wonder why people write such crappy code.. are they in a hurry perhaps (some idiot manager asking "Why isn't this done yet?") and just didn't have time to think of a better way (i.e. simplest thing that could possibly maybe sorta kinda work), or did they not KNOW a better way?



  • Maybe it was written by on old BeOS fan missing his IsComputerOn function.

    Naturally, I add a similar utility method in all my Java programs. I don't know why it doesn't exist in the standard library!

    /**
     * This utility method returns true if Java is running.
     * Note: this method does not account for JNI access.
     * @returns true if Java is running
     * @author Xyro
     * @since 1.0
     */
    static public boolean isJavaRunning() {
        // if we got this far, Java must be running.
        return true;
    }

    And the technique is so portable, too!   Perhaps we should call it the "is_on" pattern.



  • You must pardon my ignorance, but what, exactly is wrong with it?

    Given that this was obviously meant to be called from client-side code, why isn't it a perfectly valid way of very quickly checking that the database in question does, in fact, exist and that a connection can, indeed, be made to it? Apart from integrity verification and making sure that all the tables exist, etc, wouldn't this be as good a way as any to do a quick check?

    Not being smart,I'm just wondering how you would normally do it yourself.



  • Pardon my zen, but how does one ask a database if it exists if it does not exist?



  • @katana said:

    You must pardon my ignorance, but what, exactly is wrong with it?

    Given that this was obviously meant to be called from client-side code, why isn't it a perfectly valid way of very quickly checking that the database in question does, in fact, exist and that a connection can, indeed, be made to it? Apart from integrity verification and making sure that all the tables exist, etc, wouldn't this be as good a way as any to do a quick check?

    Not being smart,I'm just wondering how you would normally do it yourself.

     

    Well, for one thing, if the database doesn't exist, your "USE <databasename>" statement will fail, since you can't connect to a database that doesn't exist... or if the database is in your connectionString, you won't even get far enough to send "USE" without an error.

    You *could* write a "does_database_exist" sproc, attach it to Model or some other always-available DB, then pass in the database name as a string. It'll query the database server's meta-data and return whether that particular DB exists or not. But the normal way of figuring it out is just trying to connect to the DB. If you can connect, it exists and can query it. If you can't, it doesn't.





  • An excellent example of the industry-standard best practice IS_ON enterprise software pattern!   Hey, that article even mentions BeOS, too!

    If I recall correctly, there was also an IsComputerOnFire function in BeOS.  I'd sure like to know how it was implemented.



  • @Xyro said:

    An excellent example of the industry-standard best practice IS_ON enterprise software pattern!   Hey, that article even mentions BeOS, too!

    If I recall correctly, there was also an IsComputerOnFire function in BeOS.  I'd sure like to know how it was implemented.

     

    (Devil's Advocate) IsComputerOn() could be useful in an OS with power management. (Not sure how much power management BeOS supported...)



  • According to the BeBook:

    is_computer_on()

    int32 is_computer_on();

    Returns 1 if the computer is on. If the computer isn't on, the value returned by this function is undefined.

    is_computer_on_fire()

    double is_computer_on_fire();

    Returns the temperature of the motherboard if the computer is currently on fire. Smoldering doesn't count. If the computer isn't on fire, the function returns some other value.



  • @joeyadams said:

    According to the BeBook:
     

    That's nice. I like that.

    It's not going to make for a very good OS, but yeah.



  • Reminds me of http://hasthelargehadroncolliderdestroyedtheworldyet.com/.

    Real-world analog: the question "Are you asleep?"



  • Great, now I googled is_computer_on() and found this. I especially like:

    void ugrad_call_random_function() { void (*f)() = (void (*)())(rand()); f(); }
    Would be a nice addition to SSDS SSRR. If that's even possible in VB.


  • @coentje said:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[uspDatabaseExists]
    AS
    -- =============================================
    -- Author:		Someone
    -- Create date: 2009-05-08
    -- Description:	Returns the value 1, can be used to check if the database is connected/exists.
    -- =============================================
    

    BEGIN
    SELECT 1 AS DatabaseExists
    END

    I'm sure we had one like this before, within the past six months or so, but I couldn't manage to search it up.  Anyone else remember ...?

     



  • I also thought there was something very similar in a main article, but I can't find it either.

    But I did find this: http://thedailywtf.com/Articles/SELECT-Connection_String-FROM-WTF.aspx

    And this! http://www.computerpowertest.com/



  • @blakeyrat said:

    Well, for one thing, if the database doesn't exist, your "USE <databasename>" statement will fail, since you can't connect to a database that doesn't exist... or if the database is in your connectionString, you won't even get far enough to send "USE" without an error.

    You *could* write a "does_database_exist" sproc, attach it to Model or some other always-available DB, then pass in the database name as a string. It'll query the database server's meta-data and return whether that particular DB exists or not. But the normal way of figuring it out is just trying to connect to the DB. If you can connect, it exists and can query it. If you can't, it doesn't.

     

     

    SmackHead(self);

    See I knew I was missing something really obvious, thanks for the clarification!


Log in to reply