2011-04-11 52 views
0

我没有与MS SQL生成SYS表一个很好的经验如INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
我可以DBName\System Views\下找到这些观点复制到另一个数据库,并处理它们像任何其他表(处理它们我的意思是问题的SQL查询)?
有没有我应该关心的任何考虑?用SYS表处理任何其他表

编辑
我需要这样做的原因是因为SQL Azure不支持sys表。我在我的应用程序中使用sys表,因此如果我将它们作为普通表复制到SQL Azure,是否一切正常?

+1

您不应该复制任何内容!那些系统目录视图随处可用 - 无需复制.... – 2011-04-11 11:00:37

回答

3

SQL Azure不支持information_schema,但它支持sys。*表(至少是您需要的)。

http://msdn.microsoft.com/en-us/library/ee336238.aspx#catalog

您可以使用以下方法:

Select * from sys.foreign_keys fk 
inner join sys.foreign_key_columns fkc on fkc.constraint_object_id = fk.object_id 

为了让您获得列参与或索引信息查看以下表格SYS SYS.TABLES,SYS.INDEXES和sys 。列。标准的sys目录视图比使用ANSI标准的视图要难一些。

我在评论中谈到的数据库触发器如下。这并不处理在表create上创建fk的实例,但可以轻松扩展以处理其他情况。这将确保此信息始终准确实施为ddl触发器

--drop trigger test on database 
create trigger test on DATABASE 
for ALTER_TABLE,RENAME 
AS 
    set nocount on 

    --get XML event and extract table and schema 
    DECLARE @data XML 
    SET @data = EVENTDATA() 

    declare @idoc int 
    EXEC sp_xml_preparedocument @idoc OUTPUT, @data 

    --indicate if table needs to be reloaded 
    declare @boolean bit 
    set @boolean = 0 

    --check conditions where keys change 
    SELECT @boolean = 1 
    FROM  OPENXML (@idoc, '/EVENT_INSTANCE/AlterTableActionList/Create/Constraints',2) x 
    SELECT @boolean = 1 
    FROM  OPENXML (@idoc, '/EVENT_INSTANCE/AlterTableActionList/Drop/Constraints',2) x 

    if @boolean =1 
    begin 
     --Create the table initially with below line 
     --select * into MyInformationSchemaFKs from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
     --Reloads the table if there are constraint changes 
     truncate table MyInformationSchemaFKs 
     insert into MyInformationSchemaFKs 
     select * from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
    end 

    EXEC sp_xml_removedocument @idoc 


go 
+0

我主要对information_schema感兴趣。我可以将这些视图内的数据复制到Azure表并使用它们吗? – scatman 2011-04-11 12:00:21

+0

一种选择是创建一个将信息插入实际表中的数据库ddl触发器。只要你不需要所有的模式信息,你就可以创建一个精确的副本。 – JStead 2011-04-11 12:35:14