How to create a table then commit then insert then commit and drop table?

好久不见. 提交于 2019-12-12 03:45:36

问题


Hi I am having a problem committing to a database. I am very new to Firebird SQL. But I have a query that creates a table, then commits it, then inserts it, commits it, view the data, drop the table and then commit it again. I am trying to do this dynamically where I can hit the execute button once and it just follows through. Is there a way to do that? This is the query.

create table bp_customCollections(
       part_number varchar(255)
       ,part_description varchar(255)
       ,guided_reading_level varchar(255)
       ,lexile_level varchar(255)
       ,nonfiction_fiction varchar(255)
       ,on_hand varchar(255)
       ,default_vendor varchar(255)
       ,language varchar(255)
       ,product_price varchar(255)
       )

commit

insert into bp_customcollections(part_number, part_description,guided_reading_level,lexile_level,nonfiction_fiction, on_hand, default_vendor,language,product_price)
SELECT DISTINCT
part.num
,part.description
,COALESCE(grlmin.info,'Undefined')
,getLexile.lexile_level
,COALESCE(fnf.info,'')
,SUM(tag.qty)
,vendor.name
,customvarcharlong.info
,cast(product.price as decimal(10,2))

FROM PART

LEFT JOIN customset AS grlmin ON grlmin.recordid = part.id AND grlmin.customfieldid = 51 -- guided reading level minimum
LEFT JOIN getLexile on getLexile.lexile_partid = part.id and getLexile.lexile_partid = 61 -- lexile level
LEFT JOIN vendorparts on vendorparts.partid = part.id
LEFT JOIN vendor on vendor.id = vendorparts.vendorid
LEFT JOIN customset AS fnf on fnf.recordid = part.id AND fnf.customfieldid = 47  -- fiction/nofiction
LEFT JOIN tag on part.id = tag.partid
LEFT JOIN customvarcharlong on customvarcharlong.recordid = part.id and customvarcharlong.customfieldid = 56 -- language
LEFT JOIN product on product.partid = part.id
LEFT JOIN customset as resourceType on resourcetype.recordid = part.id
where vendorparts.defaultflag = 1
and resourcetype.info = 'Book'
GROUP BY part.num,part.description, COALESCE(grlmin.info,'Undefined'), COALESCE(fnf.info,''),getLexile.lexile_level, vendor.name, customvarcharlong.info,product.price
HAVING SUM(tag.qty) > 0

commit


select * from bp_customcollections
order by part_number

drop table bp_customCollections
commit

回答1:


Would it not be easier to create a view, and then select from it whenever you need to?

create view vw_customCollections as
SELECT DISTINCT
part.num
,part.description
,COALESCE(grlmin.info,'Undefined')
,getLexile.lexile_level
,COALESCE(fnf.info,'')
,SUM(tag.qty)
,vendor.name
,customvarcharlong.info
,cast(product.price as decimal(10,2))
FROM PART
LEFT JOIN customset AS grlmin ON grlmin.recordid = part.id AND grlmin.customfieldid = 51 -- guided reading level minimum
LEFT JOIN getLexile on getLexile.lexile_partid = part.id and getLexile.lexile_partid = 61 -- lexile level
LEFT JOIN vendorparts on vendorparts.partid = part.id
LEFT JOIN vendor on vendor.id = vendorparts.vendorid
LEFT JOIN customset AS fnf on fnf.recordid = part.id AND fnf.customfieldid = 47  -- fiction/nofiction
LEFT JOIN tag on part.id = tag.partid
LEFT JOIN customvarcharlong on customvarcharlong.recordid = part.id and customvarcharlong.customfieldid = 56 -- language
LEFT JOIN product on product.partid = part.id
LEFT JOIN customset as resourceType on resourcetype.recordid = part.id
where vendorparts.defaultflag = 1
and resourcetype.info = 'Book'
GROUP BY part.num,part.description, COALESCE(grlmin.info,'Undefined'), COALESCE(fnf.info,''),getLexile.lexile_level, vendor.name, customvarcharlong.info,product.price
HAVING SUM(tag.qty) > 0

then:

select *
from   vw_customCollections
order by  part_number


来源:https://stackoverflow.com/questions/16795016/how-to-create-a-table-then-commit-then-insert-then-commit-and-drop-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!