使用内连接在Oracle中更新语句? (将数据从table1.column1复制到table2.column1)

时间:2018-03-20 13:06:42

标签: sql oracle exception

这是我到目前为止所尝试的(Oracle SQL):

UPDATE table1
   SET table1.ADDRESS =
          (SELECT table2.ADDRESS
             FROM table2 INNER JOIN table1 ON table1.ID = table2.ID
            WHERE     table1.ADDRESS <> table2.ADDRESS
                  AND table1.DATE BETWEEN TO_DATE ('9999-12-31')
                                         AND TO_DATE ('9999-01-21'));

(我选择的日期是随机的)

  

获取ORA-1427错误 - 单行子查询返回多个   行..

4 个答案:

答案 0 :(得分:0)

问题在于您的子查询

SELECT table2.column1 
FROM table2 
INNER JOIN table1 ON table1.column1 = table2.column1 
WHERE table1.column1 <> table2.column1 
AND table1.column2 BETWEEN TO-DATE('9999-12-31') AND TO_DATE(9999-01-21)

返回多行(如错误文本所示)。您必须添加WHERE ROWNUM <= 1或其他一些条件才能将所选行减少到一个。

答案 1 :(得分:0)

首先,我删除了WHERE子句,因为它刚刚加入了相同的值,这没有任何意义? (除非你的意思是&lt;&gt; table2.column2)

WHERE table1.column1 <> table2.column1

因此,如前所述,您的查询可以返回多行。尝试使用硬编码值运行select并检查返回的行数:

select * 
from table1 
where table1.column1 = 'TEST' -- same value as table2.column1 that you would join on
    AND table1.column2 BETWEEN TO_DATE ('9999-12-31')
                           AND TO_DATE ('9999-01-21'));

检查结果并将其发回/提供一些示例数据,如果这没有帮助!

谢谢, 布兰登

答案 2 :(得分:0)

普通UPDATE

update table1
set    table1.address =
       ( select table2.address
         from   table2
         where  table2.id = table1.id
         and    table2.address <> table1.address )
where  table1.date between date '9999-01-21' and date '9999-12-31'
and    exists
       ( select 1
         from   table2
         where  table2.id = table1.id
         and    table2.address <> table1.address );

可更新的内联视图(需要table2.id上的唯一索引或约束):

update ( select t1.address as old_address, t2.address as new_address
         from   table1 t1
                join table2 t2 on t2.id = t1.id
         where  t2.address <> t.address
         and    t1.date between date '9999-01-21' and date '9999-12-31' )
set old_address = new_address;

仅限更新MERGE

merge into table1 t1
using table2 t2
on    (      t2.id = t1.id
        and  t2.address <> t.address 
        and  t1.date between date '9999-01-21' and date '9999-12-31' )
when  matched then update set t1.address = t2.address;

答案 3 :(得分:-2)

您使用public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; mMap.getUiSettings().setZoomControlsEnabled(true); mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { @Override public View getInfoWindow(Marker arg0) { return null; } @Override public View getInfoContents(Marker marker) { Context context = getApplicationContext(); LinearLayout info = new LinearLayout(context); info.setOrientation(LinearLayout.VERTICAL); TextView title = new TextView(context); title.setTextColor(Color.BLACK); title.setGravity(Gravity.CENTER); title.setTypeface(null, Typeface.BOLD); title.setText(marker.getTitle()); TextView snippet = new TextView(context); snippet.setTextColor(Color.GRAY); snippet.setText(marker.getSnippet()); info.addView(title); info.addView(snippet); return info; } }); LatLng center = new LatLng(41.385064,2.173403); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(center, 13.0f)); new MapGetNearbyPlacesData().execute(mMap); } private static class MapGetNearbyPlacesData extends AsyncTask<GoogleMap, Void, List<MarkerOptions>> { private GoogleMap map; private String TAG = "so49343164"; @Override protected List<MarkerOptions> doInBackground(GoogleMap... maps) { this.map = maps[0]; List<MarkerOptions> options = new ArrayList<>(); GeoApiContext context = new GeoApiContext.Builder() .apiKey("AIza......") .build(); NearbySearchRequest req = PlacesApi.nearbySearchQuery(context, new com.google.maps.model.LatLng(41.385064,2.173403)); try { PlacesSearchResponse resp = req.keyword("pizza").type(PlaceType.RESTAURANT).radius(2000).await(); if (resp.results != null && resp.results.length > 0) { for (PlacesSearchResult r : resp.results) { PlaceDetails details = PlacesApi.placeDetails(context,r.placeId).await(); String name = details.name; String address = details.formattedAddress; URL icon = details.icon; double lat = details.geometry.location.lat; double lng = details.geometry.location.lng; String vicinity = details.vicinity; String placeId = details.placeId; String phoneNum = details.internationalPhoneNumber; String[] openHours = details.openingHours!=null ? details.openingHours.weekdayText : new String[0]; String hoursText = ""; for(String sv : openHours) { hoursText += sv + "\n"; } float rating = details.rating; String content = address + "\n" + "Place ID: " + placeId + "\n" + "Rating: " + rating + "\n" + "Phone: " + phoneNum + "\n" + "Open Hours: \n" + hoursText; options.add(new MarkerOptions().position(new LatLng(lat, lng)) .title(name) .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeStream(icon.openConnection().getInputStream()))) .snippet(content) ); } } } catch(Exception e) { Log.e(TAG, "Error getting places", e); } return options; } @Override protected void onPostExecute(List<MarkerOptions> options) { for(MarkerOptions opts : options) { this.map.addMarker(opts); } } @Override protected void onPreExecute() {} @Override protected void onProgressUpdate(Void... values) {} } } 代替= operatorIN IN accept multiple values

您的查询会返回多个值。