BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
novinosrin
Tourmaline | Level 20

SQL pass through vs libname ODBC with unexpected results? Requesting advise as to why, what and how to fix plz?

 


/*Pass through*/
proc sql noprint;
connect to odbc(dsn=MSPBDE_PROD);
create table work.COVID_FB as 
select * from connection to odbc
     (select 
      a.LN_NO,
	  a._RowStatus,
	  a._EffectiveDate,
	  a._ExpirationDate,
	  a.FP_CREAT_DT,
	  a.FP_STAT_CD,
	  a.FP_STAT_DT,
	  a.FP_PROMISE_QT,
	  a.FP_PLAN_TY,
	  a.FP_LST_PR_DU_DT

      from MSPODS.bde.FORBEARANCE_PLAN for system_time ALL as a
   WHERE a._RowStatus NOT IN ('PD') AND a.FP_PLAN_TY in('90','91','92','93'));
quit;

proc sql;
 select count(LN_NO) as c, count(distinct LN_NO) as c1
 from COVID_FB;
quit;

RESULTS:
c	           c1
134289	34866

Vs

/*Libname */
libname msp_ods odbc dsn=mspbde_prod qualifier=MSPODS schema=BDE;

data w;
 set msp_ods.FORBEARANCE_PLAN;
 WHERE _RowStatus NOT IN ('PD') AND FP_PLAN_TY in('90','91','92','93');
run;

proc sql;
 select count(LN_NO) as c
 from w;
quit;

RESULT:
c
34671

What's going on?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Never used SQL server but for Teradata temporal tables we just set-up views that included the equivalent option for accessing the historical records.  So you might have a table or view named MYTABLE that shows the current state and than a view named MYTABLE_HISTORY that shows all records.  Then you can reference the MYTABLE_HISTORY view through your libref and see all records.

View solution in original post

7 REPLIES 7
ballardw
Super User

How many records returned for Work.Covid_FB and  W?

 


      from MSPODS.bde.FORBEARANCE_PLAN for system_time ALL as a
   WHERE a._RowStatus NOT IN ('PD') AND a.FP_PLAN_TY in('90','91','92','93'));
quit;

I might guess that the pass through using "for system_time all" overrides a default behavior from the ODBC connection default.

novinosrin
Tourmaline | Level 20
passthrough results
c c1
134289 34866
   
   
   
libname results
c  
34671  
SASKiwi
PROC Star

Do a PROC COMPARE on the two extracted datasets to see if you are starting with the same data.

novinosrin
Tourmaline | Level 20

Many thanks 'Sir'*3 for quick responses. 

 

I'm afraid I do not know SQL server proprietary language yet and I inherited from somebody here. 

 

I googled this https://docs.microsoft.com/en-us/sql/relational-databases/tables/querying-data-in-a-system-versioned...

 

to see what it means -

"If you search for non-current row versions only, we recommend you query the history table directly as this will yield the best query performance. Use ALL when you need to query current and historical data without any restrictions."

 

If I am "comprehending correctly", all ln_no(loans) in forebearance table with its history is what I am after. I am sure this would make sense to you.  I presume from the above definition the ALL keyword gives me everything. However, how to replicate the same with LIBNAME? Or is it some sort of a restricted snaps that libname engine cannot fetch while pass through could?

 

For some reason, I am glad my mind told me check this. Had I just went with Libname results, I would had to face a huge escalation. Gosh, I am breathing still.

 

Tom
Super User Tom
Super User

Never used SQL server but for Teradata temporal tables we just set-up views that included the equivalent option for accessing the historical records.  So you might have a table or view named MYTABLE that shows the current state and than a view named MYTABLE_HISTORY that shows all records.  Then you can reference the MYTABLE_HISTORY view through your libref and see all records.

novinosrin
Tourmaline | Level 20

Sir @Tom  BRILLIANT BRILLIANT BRILLIANT.!!!!!!!!!!!!! YES I AM SHOUTING. AGAIN- BRILLIANT!!!!!!!!!!!!

 

It's just unfair for Einsteins to be in SAS community. I I just can't thank you enough.

 

Okay, one follow from my team, Can you please give us a moment to enlighten to thoroughly understand the mechanics as what SQL pass through fetches in full while  the libname couldn't. I will just send this link to my team mates and share the knowledge once I could have the privilege of yet another response please. 

 

KUDOS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1282 views
  • 2 likes
  • 5 in conversation