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