85

複数のテーブルに挿入する必要のあるレコードがいくつかあります。1つおきの列は定数になります。

以下の貧弱な擬似コード-これは私がやりたいことです:

create table #temp_buildings
(
    building_id varchar(20)
)
insert into #temp_buildings (building_id) VALUES ('11070')
insert into #temp_buildings (building_id) VALUES ('11071')
insert into #temp_buildings (building_id) VALUES ('20570')
insert into #temp_buildings (building_id) VALUES ('21570')
insert into #temp_buildings (building_id) VALUES ('22570')

insert into property.portfolio_property_xref
        ( portfolio_id ,
          building_id ,
          created_date ,
          last_modified_date
        )
values
        ( 
            34 ,
            (
                select  building_id
                from    #temp_buildings
            ) ,
            getdate() ,
            null
        )

目的: #temp_buildingsの各レコードのproperty.portfolio_property_xrefへの挿入を実行します

カーソルでこれを行うことができると思いますが、これはひどく遅いと思います。この演習は将来的に繰り返されるので、もっと速い方法でこれに取り組みたいと思いますが、方法がわかりません。フィードバックをいただければ幸いです。

4

5 に答える 5

159
INSERT INTO table1 ( column1 )
SELECT  col1
FROM    table2

好き:

insert into property.portfolio_property_xref
( 
    portfolio_id ,
    building_id ,
    created_date ,
    last_modified_date
)
select
    34,
    building_id,
    getdate(),
    null
from
    #temp_buildings
于 2012-08-01T00:40:10.403 に答える
12

使用する必要がありますINSERT INTO SELECT FROMデモ付きのSQLフィドルを参照)

insert into property.portfolio_property_xref
( 
    portfolio_id ,
    building_id ,
    created_date ,
    last_modified_date
)
SELECT 34 ,
       building_id,
       getdate(),
       null
from    #temp_buildings
于 2012-08-01T00:41:51.787 に答える
2

一種のランダムですが、これはこの質問にここに来る人には役立つかもしれないと思います。時々、私はMicrosoft Excel VBAを使用して、上記のようなSQLステートメントの一部を生成します。これは、新しいジョブを設定するためにテーブルの構築とデータ変換を行っている状況で非常に役立ちます。これは本当に簡単な例です。それは2つの別々の無関係なシステム間のリンクを作成しました。次に、このリンクにより、3つの無関係なシステムを結び付けたウェアハウス環境で新しいテーブルを作成できました。とにかく、5000行を超えるSQLを数秒で作成できました(1回限りの使用で、はるかに大きなETLタスクのごく一部です)。

Option Explicit

Dim arow As Integer
Dim acol As Integer
Dim lrow As Integer
Dim IsCellEmpty As String
Dim CustNo As Integer
Dim SkuLevel As Integer


Sub SkuLevelUpdate()

'find end ouf input file
arow = 1
acol = 1

Do
    IsCellEmpty = Cells(arow, acol).Value
    arow = arow + 1
Loop Until IsCellEmpty = ""

lrow = arow - 1

'Write SQL
arow = 2
acol = 5

Do
    CustNo = Cells(arow, 1)
    SkuLevel = Cells(arow, 4)
    Cells(arow, acol) = "INSERT INTO dbo.#TempSkuLevelRelationships (CustNo, SkuLevel) VALUES (" & CustNo & ", " & SkuLevel & ");"
    arow = arow + 1
Loop Until arow = lrow

End Sub

はい、SQLインジェクションなどについてはすべて知っています。スプレッドシートを作成し、データが現在SQLテーブルに存在しない場合は、データをコピーしてより大きなSQLコードに貼り付け、新しい構築やテーブルの変更などを行います。

于 2016-12-05T16:53:19.700 に答える
0

これを試して

   insert into property.portfolio_property_xref
   ( 
      portfolio_id ,
      building_id ,
      created_date ,
      last_modified_date
   )
   Select
      34,
      building_id,
      GETDATE(),
      NULL
   From #temp_buildings
于 2012-08-01T00:41:41.090 に答える
0

あなたはカーソルでそれを行うことができると言っています。他の答えが示すように、あなたはそれをする必要はありません。SQL ServerはセットベースのRDMSであり、データのセットを処理してから、単一行を処理する能力が高くなります。

于 2012-08-01T06:08:35.757 に答える