简体   繁体   English

将表结构和数据复制到其他用户

[英]Copy tables structure and data to different user

I have a set of tables with data in one user schema. 我有一组在一个用户模式中包含数据的表。 I want a copy of those to another user. 我想要将这些副本复制给另一个用户。

Can this be done with SQL? 可以用SQL完成吗?

Thanks 谢谢

If the two users are on the same database, and provided sufficient GRANTS are present, then just run the below statement in the new user schema: 如果两个用户都在同一数据库上,并且提供了足够的GRANTS ,则只需在新的用户模式中运行以下语句:

create table <tablename> as select * from olduser.<tablename>;

EDIT: 编辑:

Copy the below set of statements in a text file. 将以下一组语句复制到一个文本文件中。 Replace <oldueser> with the existing schema name and <path> with a path in the unix/windows machine where you want the spool to be written. <oldueser>替换为现有的架构名称,并将<path>替换为您要在其中写入假脱机的UNIX / Windows机器中的路径。 Run this file in the existing schema. 在现有架构中运行此文件。 A spool file will be written in the path specified with the name tbls.sql . 假脱机文件将被写入名称为tbls.sql指定路径中。 Take this tbls.sql and run it in the new schema, where you want the tables to be replicated. 使用此tbls.sql并在要复制表的新架构中运行它。

set head off
set line 100000
set line 100000
set feedback off
spool <path>/tbls.sql
select 'CREATE TABLE ' || TABLE_NAME ||' AS SELECT * FROM <OLDUSER>.'||TABLE_NAME||';'
FROM user_tables; 
spool off
set feedback on

Yes you can by using export command in command prompt ie 是的,您可以在命令提示符下使用export命令,即

c:\>exp userid=userid(sql admin)/password owner=username(from which user) direct=y file=filename.dmp

to import this by using 通过使用导入

c:\>imp userid=userid(sql admin)/password file=filename.dmp fromuser=username(from which user) touser=username(imported username)

by using this you have to export data into a dump file and after taht import it into another user 通过使用此方法,您必须将数据导出到转储文件中,然后再将其导入到另一个用户中

create schema2.tablename as select * from schema1.tablename

  1. You can export the first schema and import in the second schema - I would go for this if there are not any reasons preventing you to do that. 您可以导出第一个模式并导入第二个模式-如果没有任何理由阻止您这样做,我会这样做。 Documentation is here. 文档在这里。

  2. Provided you have access grants, you can write a small script to do that for you. 如果您拥有访问权限,则可以编写一个小脚本来为您完成此任务。 You can select all your source user's tables using the query: 您可以使用查询选择所有源用户的表:

    SELECT table_name FROM all_tables WHERE owner = <source_schema>

Loop through all of them and create the new tables by using execute immediate. 循环浏览所有表,并使用立即执行创建新表。 ( Documentation here ) 此处的文档

execute immediate 'create <dest>.<tablename> as select * from <source>.<tablename>';

dest and source are the name of the schema. destsource是模式的名称。 tablename is the name of the table as taken from the loop. tablename是从循环中获取的表的名称。

May be "CREATE TABLE ... AS SELECT" will be useful for you? 可能是“ CREATE TABLE ... AS SELECT”对您有用吗?

See docs 查看文件

Here is a good article 这是一篇好文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM