company's data load is different so it has not been an issue before. We now
and stored procedures. Rather than duplicate the logic 30 times in each of
place but load the data into their separate databases. We have a whole system
"Hugo Kornelis" wrote:
> 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)