Question

I have a local table in SAS that I am trying to create as a temporary table table on a remote DB2 server. Is there anyway to do this other than build an insert statement elsewhere and stream it?

libname temp db2 uid=blagh pwd=blagh dsn=blagh connection=global schema=Session;

Proc SQL;
  Connect to db2 (user=blagh pw=blagh dsn=blagh connection=global);

  Execute (
    Declare Global Temporary Table Session.Test
      ( foo char(10))
      On Commit Preserve Rows
      Not Logged
    ) by db2;

  Execute (Commit) by db2;

  Insert Into Session.Test
    Select Distinct A.foo From Work.fooSource A;

I have tried several variations on these theme, each resulting in errors. The above code produces.

ERROR: Column foo could not be found in the table/view identified with the correlation name A.
ERROR: Unresolved reference to table/correlation name A.

Removing the alias gives me.

ERROR: INSERT statement does not permit correlation with the table being inserted into.
Was it helpful?

Solution

A pass-through statement like below should work.

proc sql;
  connect to db2 (user=blagh pw=blagh dsn=blagh connection=global);
  execute (create view
              sasdemo.tableA as
              select VarA, 
                     VarB,
                     VarC
              from sasdemo.orders)
  by db2;
  execute 
     (grant select on
      sasdemo.tableA to testuser)
      by db2;
  disconnect from db2;
quit;

The code below is what I routinely use to upload to DB2

rsubmit YourServer;
libname temp db2 uid=blagh pwd=blagh dsn=blagh connection=global schema=Session;

   data temp.Uploaded_table(bulkload = yes bl_method = cliload);
      set work.SAS_Local_table;
   run;

endrsubmit;
libname temp remote server=YourServer;

More options for DB2 are available from SAS support... http://support.sas.com/documentation/onlinedoc/91pdf/sasdoc_913/access_dbspc_9420.pdf

OTHER TIPS

I don't know db2 so I don't know for sure this works, but the 'normal' way to do this is with PROC COPY (although the data step also should work). I would guess in your code above that db2 doesn't allow inserts that way (I think that's fairly common to not be supported in SQL flavors).

libname temp db2 uid=blagh pwd=blagh dsn=blagh connection=global schema=Session;

proc copy in=work out=temp;
select work.foosource;
run;

If you need the name to be different (in PROC COPY it won't be), you can do a simple data step.

data temp.yourname;
set work.foosource;
run;

You shouldn't need to do the inserts in SQL. If you want to first declare it in db2 (in a connect to... session) you probably can do that and still do either of these options (though again this varies some based on the RDBMS, so test this).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top