Passing Table Valued parameter to stored procedure across different databases

后端 未结 2 1987
感情败类
感情败类 2020-12-19 01:18

I\'m using SQL Server 2008.

How can I pass Table Valued parameter to a Stored procedure across different D

2条回答
  •  难免孤独
    2020-12-19 02:14

    Table UDTs are only valid for stored procs within the same database.

    So yes you would have to create the type on each server and reference it in the stored procs - e.g. just run the first part of this example in both DBs http://msdn.microsoft.com/en-us/library/bb510489.aspx.

    If you don't need the efficency you can always use other methods - i.e. pass an xml document parameter or have the s.p. expect a temp table with the input data.

    Edit: added example

    create database Test1
    create database Test2
    go
    use Test1
    create type PersonalMessage as TABLE
    (Message varchar(50))
    go
    create proc InsertPersonalMessage @Message PersonalMessage READONLY AS
      select * from @Message
    go
    use Test2
    create type PersonalMessage as TABLE
    (Message varchar(50))
    go
    create proc InsertPersonalMessage @Message PersonalMessage READONLY AS
      select * from @Message
    go
    use Test1
    declare @mymsg PersonalMessage
    insert @mymsg select 'oh noes'
    exec InsertPersonalMessage @mymsg 
    go
    use Test2
    declare @mymsg2 PersonalMessage
    insert @mymsg2 select 'oh noes'
    exec InsertPersonalMessage @mymsg2 
    

    Disadvantage is that there are two copies of the data. But you would be able to run the batch against each database simultaneously. Whether this is any better than using a table table is really down to what processing/data sizes you have - btw to use a temp table from an s.p. you just access it from the s.p. code (and it fails if it doesn't exist).

提交回复
热议问题