Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
T-SQL (SS2K5)
»
dynamic object(database/schema/obj) in stored...
dynamic object(database/schema/obj) in stored procedure
Rate Topic
Display Mode
Topic Options
Author
Message
bbaley
bbaley
Posted Wednesday, November 12, 2008 11:09 AM
Grasshopper
Group: General Forum Members
Last Login: Monday, May 13, 2013 1:38 PM
Points: 14,
Visits: 116
Hello,
I am trying to figure out how to reference multiple database/objects in stored procedures.
I know how to create dynamic SQL and exec() etc. but I don't think that will help here as I am dealing with cursors etc. (although I could do without the cursors with MUCH rewriting).
Essentially, a given server will have multiple databases that are identical schemas, different data, and, a utilities schema/db where the generic objects live.
i.e. (greatly simplified and renamed, etc.)
CompanyDB1
CompanyDB2
CompanyDB3
CompanyDB4
Utilities
I would like to have a single stored procedure in [Utilities] that takes a parameter (or more likely reads from a table) for which Company Db will be referenced....
The sp pulls and relates data from Utilities to CompanyX data, and does a bunch of stuff with it, and populates [Utilities] tables....
so right now, I have a (nearly) identical sp in each db....
I would like to have one to maintain/test in [Utilities] and do;
exec [utilities]..[MyProc] 'CompanyDb1', 'a value', 'anotherparamvalue'....
exec [utilities]..[MyProc] 'CompanyDb2', 'a value', 'anotherparamvalue'....
exec [utilities]..[MyProc] 'CompanyDb3', 'a value', 'anotherparamvalue'....
How should I do this ?
Thanks !!!!!
Post #601564
Lynn Pettis
Lynn Pettis
Posted Wednesday, November 12, 2008 11:48 AM
SSC-Insane
Group: General Forum Members
Last Login: Today @ 8:34 AM
Points: 21,588,
Visits: 27,385
If you have to have one stored proc in the Utilities database, then you will need to go with dynamic sql. I'd also look at writing your procedures so that they don't use cursors unless absolutely necessary.
I'd also look at how you write your dynamic sql. If the differences are actually in the where clauses and the values passed to it, I'd look at use sp_executesql instead of execute(). sp_executesql can use cached plans where as execute() can't.
Lynn Pettis
For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here
or
when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here
and
here
Managing Transaction Logs
SQL Musings from the Desert
Fountain Valley SQL
(My Mirror Blog)
Post #601603
ALI-408105
ALI-408105
Posted Wednesday, November 12, 2008 2:02 PM
SSC Rookie
Group: General Forum Members
Last Login: Monday, April 23, 2012 11:52 AM
Points: 27,
Visits: 50
Does this help???
table variables provide the following benefits:
A table variable behaves like a local variable. It has a well-defined scope. This is the function, stored procedure, or batch that it is declared in.
Within its scope, a table variable can be used like a regular table. It may be applied anywhere a table or table expression is used in SELECT, INSERT, UPDATE, and DELETE statements. However, table cannot be used in the following statement:
SELECT select_list INTO table_variable
table variables are automatically cleaned up at the end of the function, stored procedure, or batch in which they are defined.
CHECK constraints, DEFAULT values and computed columns in the table type declaration cannot call user-defined functions.
table variables used in stored procedures cause fewer recompilations of the stored procedures than when temporary tables are used.
Transactions involving table variables last only for the duration of an update on the table variable. Therefore, table variables require less locking and logging resources.
http://msdn.microsoft.com/en-us/library/ms175010.aspx
Post #601702
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.