如果你对SQLC还不熟悉,你要注意的第一事情是散布在代码中以EXEC SQL开始的非C语言语句,这些是特殊的指令,SQLC预处理器知道是静态语句(static statements),这个预处理器将这些语句转换为正确的C代码,他们引用DBMS库函数以便在数据库引擎上执行命令。因为他们是硬编码(hard-coding)到结果目表代码,因此他们是静态语句。
SQLC功能强大的部分是特殊声明的C变量,它能直接被嵌入到SQL语句以接受结果,或把变量传递给运行时刻(runtime)询问。这样的变量被看作主机变量(host variables),在上面例子中在fetch_rows函数重使用主机变量将查询结果从查询传递给标准C变量。
EXEC SQL FETCH RELATIVE 1 C1
INTO :emp_id, :name, :sex, :birthdate; |
emp_id、name、sex和birthdate是基本C指针,包含查询返回值。这种情况下,查询准备从以前用下列语句设置的光标中获取下一行值。
EXEC SQL DECLARE C1 CURSOR FOR
SELECT emp_id, emp_fname || ' ' || emp_lname, sex, birth_date
FROM "dba".employee; |
表达式emp_fname || ' ' || emp_lname连接两个字符类型的列emp_fname和emp_lname,变成一个字符串值,从查询返回值。
为了使这个例子工作,拷贝例子源码到一个文件listing1.sqc中,并且运行SQLC预处理器:
为了使这个例子工作,拷贝例子源码到一个文件listing1.sqc中,并且运行SQLC预处理器:
它输出一个C文件listing1.sqc,你必须编译它,特别小心地指定相关的库和包括文件目录:
$ cc -Wall -I$SYBASE/include/ -L$SYBASE/lib/ -ldblib6 -ldbtools6 -o listing1 listing1.c |
产生一个可执行文件listing1。在运行它之前,保证有一台引擎运行范例数据库:
它的输出应该是这样的:
$ ./listing1
(102, Fran Whitney, F, 1958-06-05)
(105, Matthew Cobb, M, 1960-12-04)
(129, Philip Chin, M, 1966-10-30)
(148, Julie Jordan, F, 1951-12-13)
(160, Robert Breault, M, 1947-05-13)
(184, Melissa Espinoza, F, 1939-12-14)
(191, Jeannette Bertrand, F, 1964-12-21)
(195, Marc Dill, M, 1963-07-19)
(207, Jane Francis, F, 1954-09-12)
(243, Natasha Shishov, F, 1949-04-22)
(247, Kurt Driscoll, M, 1955-03-05)
(249, Rodrigo Guevara, M, 1956-11-23)
(266, Ram Gowda, M, 1947-10-18)
(278, Terry Melkisetian, F, 1966-05-17)
(299, Rollin Overbey, M, 1964-03-15)
(316, Lynn Pastor, F, 1962-07-14)
(318, John Crow, M, 1962-04-24)
(390, Jo Ann Davidson, F, 1957-02-17)
(409, Bruce Weaver, M, 1946-04-05)
(445, Kim Lull, M, 1955-01-19)
(453, Andrew Rabkin, M, 1957-08-10)
(467, James Klobucher, M, 1952-11-09)
(479, Linda Siperstein, F, 1967-09-21)
(501, David Scott, M, 1947-03-01)
(529, Dorothy Sullivan, F, 1950-04-19)
(582, Peter Samuels, M, 1968-02-28)
(586, James Coleman, M, 1966-03-04)
(591, Irene Barletta, F, 1957-01-30)
(604, Albert Wang, M, 1958-12-25)
(641, Thomas Powell, M, 1951-10-31)
(667, Mary Garcia, F, 1963-01-23)
(690, Kathleen Poitras, F, 1965-09-29)
(703, Jose Martinez, M, 1953-07-22)
(750, Jane Braun, F, 1939-08-09)
(757, Denis Higgins, F, 1968-05-12)
(839, Dean Marshall, M, 1966-05-21)
(856, Samuel Singer, M, 1959-04-07)
(862, John Sheffield, M, 1955-09-25)
(868, Felicia Kuo, F, 1968-07-24)
(879, Kristen Coe, F, 1965-11-11)
(888, Doug Charlton, M, 1966-01-23)
(902, Moira Kelly, F, 1950-08-16)
(913, Ken Martel, M, 1943-04-23)
(921, Charles Crowley, M, 1960-09-11)
(930, Ann Taylor, F, 1962-06-06)
(949, Pamela Savarino, F, 1955-07-28)
(958, Thomas Sisson, M, 1969-10-02)
(992, Joyce Butterfield, F, 1960-04-15)
(1013, Joseph Barker, M, 1969-02-14)
(1021, Paul Sterling, M, 1950-02-27)
(1039, Shih Lin Chao, M, 1969-12-12)
(1062, Barbara Blaikie, F, 1953-11-14)
(1090, Susan Smith, F, 1950-11-30)
(1101, Mark Preston, M, 1966-09-14)
(1142, Alison Clark, F, 1957-05-04)
(1157, Hing Soo, M, 1970-03-07)
(1162, Kevin Goggin, M, 1952-04-18)
(1191, Matthew Bucceri, M, 1944-11-19)
(1250, Emilio Diaz, M, 1936-01-02)
(1293, Mary Anne Shea, F, 1955-03-13)
(1336, Janet Bigelow, F, 1950-07-21)
(1390, Jennifer Litton, F, 1948-04-05)
(1446, Caroline Yeung, F, 1971-06-21)
(1483, John Letiecq, M, 1939-04-27)
(1507, Ruth Wetherby, F, 1959-07-19)
(1570, Anthony Rebeiro, M, 1963-04-12)
(1576, Scott Evans, M, 1960-11-15)
(1596, Catherine Pickett, F, 1959-11-18)
(1607, Mark Morris, M, 1941-01-08)
(1615, Sheila Romero, F, 1972-09-12)
(1643, Elizabeth Lambert, F, 1968-09-12)
(1658, Michael Lynch, M, 1973-01-18)
(1684, Janet Hildebrand, F, 1955-10-31)
(1740, Robert Nielsen, M, 1965-06-19)
(1751, Alex Ahmed, M, 1963-12-12)
$ | |