Line data Source code
1 : #include "common.h"
2 :
3 : /* Test for store procedure and params */
4 : /* Test from Tom Rogers */
5 :
6 : /* SP definition */
7 :
8 : static const char sp_define[] = "CREATE PROCEDURE spTestProc\n"
9 : " @InParam int,\n"
10 : " @OutParam int OUTPUT,\n"
11 : " @OutString varchar(20) OUTPUT\n"
12 : "AS\n"
13 : " SELECT @OutParam = @InParam\n"
14 : " SELECT @OutString = 'This is cool!'\n"
15 : " IF @InParam < 10\n" " RETURN (0)\n" " ELSE\n" " RETURN (1)\n";
16 :
17 : #define SP_TEXT "{?=call spTestProc(?,?,?)}"
18 : #define OUTSTRING_LEN 20
19 :
20 : static int
21 20 : Test(int bind_before)
22 : {
23 20 : SQLSMALLINT ReturnCode = 0;
24 20 : SQLSMALLINT InParam = 5;
25 20 : SQLSMALLINT OutParam = 1;
26 : char OutString[OUTSTRING_LEN];
27 20 : SQLLEN cbReturnCode = 0, cbInParam = 0, cbOutParam = 0;
28 20 : SQLLEN cbOutString = SQL_NTS;
29 :
30 20 : odbc_connect();
31 :
32 : /* drop proc */
33 20 : odbc_command("IF OBJECT_ID('spTestProc') IS NOT NULL DROP PROC spTestProc");
34 :
35 : /* create proc */
36 20 : odbc_command(sp_define);
37 :
38 20 : if (!bind_before)
39 10 : CHKPrepare(T(SP_TEXT), strlen(SP_TEXT), "S");
40 :
41 20 : CHKBindParameter(1, SQL_PARAM_OUTPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &ReturnCode, 0, &cbReturnCode, "S");
42 20 : CHKBindParameter(2, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &InParam, 0, &cbInParam, "S");
43 20 : CHKBindParameter(3, SQL_PARAM_OUTPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &OutParam, 0, &cbOutParam, "S");
44 :
45 : OutString[0] = '\0';
46 20 : strcpy(OutString, "Test"); /* Comment this line and we get an error! Why? */
47 20 : CHKBindParameter(4, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_VARCHAR, OUTSTRING_LEN, 0, OutString,
48 : OUTSTRING_LEN, &cbOutString, "S");
49 :
50 20 : if (bind_before)
51 10 : CHKPrepare(T(SP_TEXT), strlen(SP_TEXT), "S");
52 :
53 20 : CHKExecute("S");
54 :
55 20 : odbc_command("DROP PROC spTestProc");
56 :
57 20 : printf("Output:\n");
58 20 : printf(" Return Code = %d\n", (int) ReturnCode);
59 20 : printf(" InParam = %d\n", (int) InParam);
60 20 : printf(" OutParam = %d\n", (int) OutParam);
61 20 : printf(" OutString = %s\n", OutString);
62 :
63 20 : if (InParam != OutParam) {
64 0 : fprintf(stderr, "InParam != OutParam\n");
65 0 : return 1;
66 : }
67 :
68 20 : if (strcmp(OutString, "This is cool!") != 0) {
69 0 : fprintf(stderr, "Bad string returned\n");
70 0 : return 1;
71 : }
72 :
73 20 : odbc_disconnect();
74 20 : ODBC_FREE();
75 20 : return 0;
76 : }
77 :
78 : int
79 10 : main(void)
80 : {
81 10 : if (Test(0))
82 : return 1;
83 10 : if (Test(1))
84 : return 1;
85 :
86 10 : printf("Done successfully!\n");
87 10 : return 0;
88 : }
|