T-SQL通过另一个数据库的过程填充数据库

时间:2012-12-28 09:38:27

标签: sql-server tsql

我在DB1中有一个过程调用FILLTABLE,该过程控制DB1.table1中的插入,并且我在DB2.table1中有所有数据,所以我需要从BD2.table1中通过procededure FILLTABLE填充DB1.table1但是过滤数据DB2.table1

例如:

Execute BD1.dbo.FILLTABLE (
                select db2.dbo.table1.name as @name, 
                       db2.dbo.table1.phonea s @phone 
                from table1 
                where citycode = 'ca')

2 个答案:

答案 0 :(得分:0)

语法为:

INSERT DB1.dbo.Table1
(Column1, Column2)
SELECT name, phonea
FROM db2.dbo.table1 
WHERE citycode = 'ca'

INSERT的文档中对此进行了描述。

答案 1 :(得分:0)

如果我说得对,你应该使用光标

DECLARE @Name varchar(1000); -- change to type of db2.dbo.table1.name
DECLARE @Phone varchar(1000); -- change to type of db2.dbo.table1.phone

DECLARE t_Cursor CURSOR FOR
select db2.dbo.table1.name, 
       db2.dbo.table1.phone
    from table1 
    where citycode = 'ca';

OPEN t_Cursor;
FETCH NEXT FROM t_Cursor INTO @Name,@Phone;
WHILE @@FETCH_STATUS = 0
   BEGIN
      Execute BD1.dbo.FILLTABLE @Name,@Phone;
      FETCH NEXT FROM t_Cursor INTO @Name,@Phone;

   END;
CLOSE t_Cursor;
DEALLOCATE t_Cursor;