Re: Use of "USE databasename" Within a Stored Procedure
- From: Hugo Kornelis <hugo@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 06 Dec 2005 22:55:50 +0100
On Tue, 6 Dec 2005 08:22:03 -0800, RitaG wrote:
>Hi.
>
>I have a common stored procedure (SP) that has a variable containing a
>database name passed to it. Within the SP I have the statement "Use
>@databasename" but get an error when I run the SP. I then defined a variable
>that holds all the SQL statements (including the ?USE @databasename?) and
>then in the SP used ?Exec sp_executesql !@SQL?.
>
>This works fine but I now have to create another much more complicated SP
>and I find it tricky to work this way especially when I'm passing several
>variables to the SP.
>
>Is there another way I can use the "Use" statment within a SP without having
>to bundle all the SP code into a variable?
>
>Any suggestions will be greatly appreciated :-).
>Rita
Hi Rita,
First of all: why do you need to use the same procedure in different
databases? The only situation I can imagine if it's a procedure that
performs general DB-maintenance tasks. If you use this in a procedure
that performs business logic, than you might have a flawed design. If
you post more information, we can help you improve your design.
That being said, the answer to your question is that the only better way
than using dynamic SQL to do this is to not use it at all. Related data
should be in one database.
Are you aware of the risks of dynamic SQL? I heartily recommend you to
read Erland's site: http://www.sommarskog.se/dynamic_sql.html
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)
.
- Prev by Date: Re: Question...
- Next by Date: Re: Use of "USE databasename" Within a Stored Procedure
- Previous by thread: Re: Question...
- Next by thread: Re: Use of "USE databasename" Within a Stored Procedure
- Index(es):
Relevant Pages
|
|