我有一个包含表名的表(列名是表名-1000行)。我想遍历此表,构造一个select语句来访问每个表中的数据。我可以毫无问题地遍历表,但是找不到将内容(表的实际名称)插入到select语句中的方法。
...
hold_tablename = a_rec.tablename;
query1 := 'SELECT * FROM hold_tablename';
-- Postgres thinks 'hold_tablename' is the name of the table rather than
-- looking into the content of that field which should be aaa_customer_tbl.
<<innerloop>>
FOR b_rec IN EXECUTE query1
loop
select * from b_rec;
end loop innerloop;
答案 0 :(得分:2)
将标识符插值到字符串中的最干净的方法是将format
与Option Explicit
Sub DivideSomeStuff()
Const NUMBER_TO_CHANGE As Long = 7 ' Change this as needed
Dim remainder As Long
Dim pair As Range
For Each pair In Range("B30, F30, J30")
If Right(pair, Len(CStr(NUMBER_TO_CHANGE))) = NUMBER_TO_CHANGE Then
If pair.Offset(0, 2) <= 12 Then
remainder = 0
Else
remainder = pair.Offset(0, 2) Mod 12
End If
Dim findFifteen As Double
findFifteen = (pair.Offset(0, 2) - remainder) / 10
Dim accumulator As Range
For Each accumulator In Range("A36, D36, G36, J36, M36, A40, D40, G40, J40, M40")
If accumulator.Offset(-1, 0) = Val(Left(pair, InStr(pair, "-") - 1)) Then
accumulator.Value = accumulator.Value + remainder
End If
accumulator.Value = accumulator.Value + findFifteen
Next accumulator
End If
Next pair
End Sub
指定符一起使用:
%I
在区分大小写或包含空格或其他有问题的字符的情况下,它将适当地引用query1 := format('SELECT * FROM %I', hold_tablename);
。
或者,您可以使用hold_tablename
和字符串串联:
quote_ident
但是query1 := 'SELECT * FROM ' || quote_ident(hold_tablename);
的噪音较小。