【问题标题】:SQL QUERY for insert statement in select query选择查询中插入语句的 SQL QUERY
【发布时间】:2017-04-23 15:18:57
【问题描述】:

输出格式:

INSERT INTO Table (columns) VALUES (records of this table)

例子:

INSERT INTO Table1 (columns of Table1 ) VALUES (records of this table1)
INSERT INTO Table2 (columns of Table2) VALUES (records of this table2)
INSERT INTO Table3 (columns of Table3) VALUES (records of this table3)
INSERT INTO Table4 (columns of Table4) VALUES (records of this table4)
.
.
.
.
.
. etc....

注意:OUTPUT 应该是这样的.. 它应该一个一个地显示数据库/模式中的所有表数据.. 我需要一个 SQl 查询来满足这个要求???

【问题讨论】:

  • 输出格式:INSERT INTO Table (columns) VALUES (records of this table) 例子:INSERT INTO Table1 (columns of Table1 ) VALUES (records of this table1); INSERT INTO Table2(Table2的列)VALUES(这个table2的记录); INSERT INTO Table3(Table3的列)VALUES(这个table3的记录); INSERT INTO Table4(Table4 的列) VALUES(此 table4 的记录); . . . . . .等等......输出应该是这样的......
  • 您是在询问准备部署类型的脚本吗?哪个会为其所有行准备 INSERT 命令?
  • 你的问题是什么?
  • s.. 这些脚本应该在服务器中执行以将值存储在生产服务器中
  • 请看第一条评论..我提到了要求..一旦查看评论

标签: sql postgresql


【解决方案1】:

试试这个:用你的表替换表名

Declare @table varchar(100)=<tablename>
Declare @count int,@query varchar(maX),@quer varchar(maX)
declare @col table(col varchar(100),id int identity(1,1))
insert into @col
select name from master.sys.columns 
where object_id=(select object_id from master.sys.tables where name=@table)


set @count=0
set @query='INSERT INTO '+@table+' ('
while @count<(select count(*) from @col)
begin
set @query=@query+(select col from @col where id=(@count+1))+','
set @count=@count+1
end

set @query=substring(@query,0,len(@query))+') values ('


set @count=0
set @quer='select '''+@query+'''''''+'
print @quer
while @count<(select count(*) from @col)
begin
set @quer=@quer+'cast('+(select col from @col where id=(@count+1))+' as varchar(maX))+'''''',''''''+'
set @count=@count+1
end
set @quer=substring(@quer,0,len(@quer)-6)+''''')'' from '+@table
exec (@quer)

【讨论】:

  • 我的架构中有 71 个表.. 所以我想显示所有表数据.. 我的要求格式我已经提到过.. 但是... 我们可以为我的问题编写简单的 sql 查询吗?有可能吗?
  • 从您的要求中我能想到的就是这些了。
  • 我正在使用 PostgreSQL 数据库。我应该在哪里运行这些完整的代码?
  • SELECT 'INSERT INTO tablename(SELECT column_name FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'tablename') VALUES(SELECT * FROM tablename)';'FROM pg_tables where schemaname='public ' ... * 我不知道这个查询是 crct.. 但我想要一个这样的查询.. 我的意思是简单查询..*
  • @SaiTharun 那么它是哪个 DBMS? MySQL?甲骨文?还是PostgreSQL?你提到了所有这些。
【解决方案2】:

postgresql.

    create language plpythonu;
    create or replace function geninsert1(_table text)
    returns setof  text
    as
    $$
    def isstr(v):
        if v == None:
            return 'NULL'
        elif isinstance(v,str):
            return "'%s'" % v
        else:
            return str(v)

    alist=[]
    result = plpy.execute("select * from "+_table)
    for x in result:
        fieldlist=x.keys()
        fields=','.join(fieldlist)
        fvalues=','.join([isstr(x[y]) or 'NULL' for y in fieldlist])
        sql="insert into %s (%s) VALUES(%s)" % (_table,fields,fvalues)
        alist.append(sql)
    return alist
    return ''
    $$
    language plpythonu;
    select * from geninsert1('mutable');

【讨论】:

    猜你喜欢
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    相关资源
    最近更新 更多