Calling Sp or function without knowing the database name !

Last post 06-13-2008, 11:24 AM by randyvol. 4 replies.
Sort Posts: Previous Next
  •  06-12-2008, 4:17 AM Post number 59528

    Calling Sp or function without knowing the database name !

    Ex :
    I have a database name Test .
    And create a procedure or a function there name sp_test which will return 123 .
    The question is : how can i call this procedure/function without using this syntax Test.dbo.sp_test ?
    Regards .

  •  06-12-2008, 10:51 AM Post number 59555 in reply to post number 59528

    • randyvol is not online. Last active: 11-12-2008, 4:06 PM randyvol
    • Top 25 Contributor
    • Joined on 04-04-2007
    • Columbia, SC (USA)
    • Level 2: Deep Blue

    Re: Calling Sp or function without knowing the database name !

    normal way is exec <sprocname>

     

    example: exec sp_test

     

    Have you tried this?

  •  06-12-2008, 7:48 PM Post number 59587 in reply to post number 59555

    Re: Calling Sp or function without knowing the database name !

    I'm sorry , but may be you have misunderstood my problem coz of my lousy English .
    I don't want to do this way :
    Use test
    exec sp_test ..v..v.
    I mean  , i have a alot  of Sql functions for my software . And in the code I call the function by this syntax " databasename.dbo.functionname() " . And for each customer i will provide the different database name , like customer A is database Cus_A, customer B is database Cus_B, so i must change the code in the functions for different customers , that's a waste of time !
  •  06-13-2008, 11:15 AM Post number 59621 in reply to post number 59587

    • randyvol is not online. Last active: 11-12-2008, 4:06 PM randyvol
    • Top 25 Contributor
    • Joined on 04-04-2007
    • Columbia, SC (USA)
    • Level 2: Deep Blue

    Re: Calling Sp or function without knowing the database name !

    Ok, you're right, I do not understand your problem.

    "I have a lot of SQL functions for my software"...  What language are you developing in?  At first I thought you were looking for T-SQL, but your further description leads me to believe that is not the case.

    Assuming your call to 'databasename.dbo.functionname() actually works, then all you need is to know what the database name should be?  Have you considered a variable of  'databasename' and a means whereby you can set this varable to the database you want?  After that, it is a matter of creating the actual execution statement...

    If this is still not on track, perhaps you can expand the problem description a little more?

     

    randyvol

  •  06-13-2008, 11:24 AM Post number 59622 in reply to post number 59621

    • randyvol is not online. Last active: 11-12-2008, 4:06 PM randyvol
    • Top 25 Contributor
    • Joined on 04-04-2007
    • Columbia, SC (USA)
    • Level 2: Deep Blue

    Re: Calling Sp or function without knowing the database name !... more....

    What I'm getting at is something like this... (Assume VB.Net)

    dim conPubs as SQLConnection

    dim strSelect as String

    dim dbname as String

    dim cmdSelect as SqlCommand

    <embed your logic to decide what the dbname should be here>

    conPubs = New SqlConnection ( "server=localhost;UID=sauser;PWD=sauser123!#;Database=Pubs )

    strSelect = "exec @dbname.dbo.functionname()"

    cmdSelect = New SqlCommand( strSelect, conPubs )

    cmdSelect.Parameters.Add( "@dbname", <object where you stored the dbname>)

    conPubs.Open()

    blah, blah, blah.

    Does that help?

     

View as RSS news feed in XML