1. James Medick
  2. PowerBuilder
  3. Friday, 6 August 2021 19:43 PM UTC

Can PB handle a variable as a table name in embedded SQL?  Not sure how the TransObject could handle that.  Dynamically changing the table would cut down on some code in a global function.

Select column

Into :ls_column

From :table_name

Using SQLCA;

 

Thanks

Accepted Answer
Sivaprakash BKR Accepted Answer Pending Moderation
  1. Saturday, 7 August 2021 07:41 AM UTC
  2. PowerBuilder
  3. # Permalink

Other ways of achieving the result

1.  Create an NVO [ nc_dynamic_sql ]
2.  Insert a datastore [ ds_store ] in it.
3.  Write a function, to create datawindow from dynamic SQL [ of_result_from_sql [value string as_query, ref datastore as-result_ds, ref string as_msg, value transaction at_transaction]

************* Function of_result_from_sql () ****************
Parameter(s)
Sl.   PassBY         Argument Type            ArgumentName
1.    Value          String                   as_query
2.    reference      datastore                as_result_ds
3.    reference      string                   as_msg
4.    value          transaction              at_transaction
*************

String		ls_present_str
String		ls_syntax_str
Long		ll_return, ll_ret

ll_return = 0
ls_present_str = "style(type=tabular)"
ls_syntax_str  = at_transaction.SyntaxFromSQL(as_query, ls_present_str, as_msg)
If ls_syntax_str = "" Then
	Messagebox('Elifa', 'Syntax Error~r~n' + as_msg)
	Return -1	
End If

ds_store.Create(ls_syntax_str, as_msg)
ll_ret = ds_store.SetTransObject(at_transaction)
ds_store.Retrieve()
as_result_ds = ds_store

Return 1

*****************************

To call this function:

ls_sql =  " Select (a.field1) as field1 "
ls_sql += " From   table1 a "
ls_sql += " Join   table2 b "
ls_sql += " On     b.field2 = a.field2 "

io_sql.of_result_from_sql(ls_sql, ds_1, ls_msg, Sqlca)

If ds_1.RowCount() > 0 and Len(Trim(ls_msg)) = 0 Then 
	For i = 1 To ds_1.RowCount()
		// Process the result here
	Next
Else
	If Len(ls_msg) > 0 Then io_message.Messagebox('Error in Query', ls_msg)
End If

 

With the above function(s), one can read result from dynamically created queries.

HTH

Happiness Always
BKR Sivaprakash

 

Comment
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Friday, 6 August 2021 20:23 PM UTC
  2. PowerBuilder
  3. # 1

Hi James;

  Absolutely! You can use the SQLPREVIEW event to change the Table name. The DWO does not care as long as the number of columns & their data types are the same 

  Also since PB 2.0, that is what the dynamic SQL feature (format #4 I believe) is used for. HTH

Regards ... Chris

Comment
There are no comments made yet.
James Medick Accepted Answer Pending Moderation
  1. Saturday, 7 August 2021 20:52 PM UTC
  2. PowerBuilder
  3. # 2

Wow...thanks to everyone for the amazing answers, impressive.

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.