I hava a stored procedure named PROC_fetchTableInfo (simple select from sys.tables) in a database named
Cust
I would like use this procedure in a another database named
Dept
I tried to execute this sp in this database using command
EXECUTE Cust.dbo.PROC_fetchTableInfo
but as a result I get tables from the database Cust. How to get this procedure to work in database Dept?
A stored procedure is tighly bound to the objects in its code. If
Cust.dbo.PROC_fetchTablereferences a tableT, that is stricly the tableTin schemadboin databaseCust. You can invoke the procedure from any other place, it will always refer to this table.If you need to run the same procedure in another table on another database then the best solution, by far, is to have a new procedure:
Dept.dbo.PROC_fetxTableInfo. This is better than the alternative of using Dynamic-SQL. While this seems counteruintuitive from a DRY and code reuse perspective, T-SQL is a data access language is not a programming language, leave your C/C# mind set at the door when you enter the database. Just have another procedure in theDeptdatabase.