سؤال

If I create a stored procedure in the master database, and I want to execute it from any of my databases I just follow this link:

Making a Procedure Available in all Databases

that give me this code example:

enter image description here

Just by following the example above, I can call my procedure from any database.

what about if I create a table data type in master, how can I use it in any of my databases?

use master

        IF NOT EXISTS (select * from sys.types where name = 'theReplicatedTables') 
                CREATE TYPE theReplicatedTables AS TABLE 
                (  OBJ_ID INT NOT NULL,
                  PRIMARY KEY CLUSTERED (OBJ_ID)
                );

use APIA_Repl_Sub
go
declare @the_tables [dbo].[theReplicatedTables]

enter image description here

هل كانت مفيدة؟

المحلول

There is no way to do that with table types. You will need to replicate that in all databases. But, if you want this table type available while creating a new database, you can just add it in the model database. There are a couple restrictions to using table-valued parameters. You can't even use table-valued parameters across databases. Check some details here.

نصائح أخرى

Types don't have the ability to cross database thresholds or to be marked as system. So you'll need to create that type in each database where you want to use it. (I'm not a big fan on relying on things like data or types in master, because they disappear when you move to a different system.)

For existing databases, you can just create the type in a loop. For new databases (through CREATE DATABASE), create the type in model.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى dba.stackexchange
scroll top