| Author |
Topic |
|
vishu.av
Starting Member
26 Posts |
Posted - 2007-04-26 : 09:51:07
|
| Hi All,I have a Stored procedure as below..create procedure sp_sample @name varchar(12)asbeginset @name = (select name from mytable where id = 1)select @nameendhow can i return the varchar value from the above Stored procedure?I want to capture it in SQLFetch ODBC call.Thanks in advance!!vishuBangalore |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-26 : 09:54:13
|
[code]create procedure sp_sample@name varchar(12) outputasbeginset @name = (select @name = name from mytable where id = 1)select @nameend[/code] KH |
 |
|
|
vishu.av
Starting Member
26 Posts |
Posted - 2007-04-26 : 10:02:09
|
| actually i have my query like this..CREATE PROCEDURE SP_REC ( @LOCATION VARCHAR(20),@TEMPLOCATION VARCHAR(20) OUTPUT)ASSET @TEMPLOCATION = ( SELECT LOC_RELATEDLOCATION FROM LOC_LOCATION WHERE LOC_LOCATIONID = @LOCATION)IF @TEMPLOCATION != 'NULL'BEGINSELECT @TEMPLOCATIONEXEC SP_REC @TEMPLOCATIONENDNOW I WANT TO RETURN @TEMPLOCATION AND CAPTURE IT IN SQLFetch() odbc call..using....retcode = SQLPrepare((hStmt),(SQLCHAR*)"{CALL SP_REC(?,?)}",SQL_NTS);retcode = SQLBindParameter(hStmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 20, 0, strUserNameID, 0, &Num2IndOrLen1); retcode = SQLBindParameter(hStmt, 2, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_VARCHAR, 20, 0, strOutLoc, 0, &Num2IndOrLen2);retcode = SQLExecute (hStmt); if (SQL_SUCCESS==retcode) { SQLBindCol (hStmt, 1, SQL_C_CHAR,strLocation ,20, &cbModel); retcode=SQLFetch(hStmt); while((SQL_SUCCESS==retcode)||(SQL_SUCCESS_WITH_INFO==retcode)) { retcode=0; printf((const char*)strOutLoc); retcode=SQLFetch(hStmt); } }The stuff is not working.. everything is fine till ...retcode = SQLExecute (hStmt); after that it fails..vishuBangalore |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-26 : 16:50:38
|
| [code]CREATE PROCEDURE SP_REC ( @LOCATION VARCHAR(20), @TEMPLOCATION VARCHAR(20) OUTPUT)ASSET NOCOUNT ONSELECT @TEMPLOCATION = LOC_RELATEDLOCATIONFROM LOC_LOCATIONWHERE LOC_LOCATIONID = @LOCATIONIF @TEMPLOCATION IS NOT NULLBEGIN EXEC SP_REC @TEMPLOCATION -- HERE THE CODE FAILS! YOU ARE DOING A RECURSIVE CALL! WHERE IS THE SECOND PARAMETER? SELECT @TEMPLOCATIONEND[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
vishu.av
Starting Member
26 Posts |
Posted - 2007-04-27 : 03:38:57
|
| Thanks for the response. I have modified my query accordingly..ALTER PROCEDURE SP_REC ( @LOCATION VARCHAR(20),@TEMPLOCATION VARCHAR(20) OUTPUT)ASDECLARE @TEMPLOCATION1 VARCHAR(100)SET @TEMPLOCATION = ( SELECT LOC_RELATEDLOCATION FROM LOC_LOCATION WHERE LOC_LOCATIONID = @LOCATION)IF @TEMPLOCATION != 'NULL'BEGINSELECT @TEMPLOCATIONEXEC SP_REC @TEMPLOCATION,@TEMPLOCATION1ENDwhat wrong in the beow piece of code? Why i dont get 'strOutLoc' populated?retcode = SQLPrepare((hStmt),(SQLCHAR*)"{CALL SP_RECTEST(?,?)}",SQL_NTS); //retcode = SQLBindParameter(hStmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 20, 0, // strUserNameID, 0, &Num2IndOrLen1); long Num = 101; retcode = SQLBindParameter(hStmt,1, SQL_PARAM_INPUT, SQL_C_ULONG,SQL_NUMERIC,10, 0, &Num, 0, &Num2IndOrLen1); retcode = SQLBindParameter(hStmt, 2, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_VARCHAR, 100, 0, strOutLoc, 0, &Num2IndOrLen2); if (retcode != SQL_SUCCESS) return 1; // if (retcode != SQL_SUCCESS) {printf("Error2");} // Execute the SQL statement handle retcode = SQLExecute (hStmt); //SQLRETURN SC= SQLError(hEnv,hDBC,hStmt,(SQLCHAR*)Sqlstate,(SQLINTEGER*)NativeError,(SQLCHAR*)MessageText,BufferLength,&TextLength); //retcode=NativeError; if (SQL_SUCCESS==retcode) { //SQLBindCol (hStmt, 1, SQL_C_CHAR,strLocation ,100, &cbModel); printf((const char*)strOutLoc); retcode=SQLFetch(hStmt); while((SQL_SUCCESS==retcode)||(SQL_SUCCESS_WITH_INFO==retcode)) { retcode=0; printf((const char*)strOutLoc); retcode=SQLFetch(hStmt); } }Thanks in advance..vishuBangalore |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-27 : 04:29:12
|
| First of all, run the stored procedure in QA or SSMS to see that all is ok BEFORE you add another piece of layer that can disturb the expected outcome.Peter LarssonHelsingborg, Sweden |
 |
|
|
vishu.av
Starting Member
26 Posts |
Posted - 2007-04-27 : 04:41:45
|
| The Stored Procedure fetchs me the desired result. vishuBangalore |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-27 : 04:53:06
|
| Great!Next question. Why are you using ODBC API instead of OLEDB?Peter LarssonHelsingborg, Sweden |
 |
|
|
vishu.av
Starting Member
26 Posts |
Posted - 2007-04-27 : 05:10:51
|
| I am working on a porting project and i need to code using ODBC APIs :-))I am all successful with insert/ select statements using ODBC API but got struck here using Stored Procedure. Please let me know if there is anything wrong in above piece of c-codeThanksvishuBangalore |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-27 : 05:54:36
|
| What is the error you get?Peter LarssonHelsingborg, Sweden |
 |
|
|
vishu.av
Starting Member
26 Posts |
Posted - 2007-04-27 : 06:17:40
|
| Hi Peter,There is no error..Infact i have 4 rows of data when i execute my Stored procedure on SSMS.But i get retcode as 100 ( SQL_NO_DATA) and out of the loop.I have to get those values through ODBC call.vishuBangalore |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-27 : 06:19:51
|
| Yes, you get this "error message" because there are no more records to fetch after the fourth one, hence "no more data".Peter LarssonHelsingborg, Sweden |
 |
|
|
vishu.av
Starting Member
26 Posts |
Posted - 2007-04-27 : 06:23:42
|
| Ok. but My string doesn't contain any value returned from the SP.vishuBangalore |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-27 : 06:27:18
|
| Where is the string strOutLoc declared?Peter LarssonHelsingborg, Sweden |
 |
|
|
vishu.av
Starting Member
26 Posts |
Posted - 2007-04-27 : 06:32:35
|
| It is declared at the begining of the program as SQLUCHAR strOutLoc[100]={'\0'};vishuBangalore |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-27 : 06:39:19
|
| Ok. C/C++ is not my ballpark. Maybe you should post your question in a proper forum.It is NOT a database question anymore.Peter LarssonHelsingborg, Sweden |
 |
|
|
vishu.av
Starting Member
26 Posts |
Posted - 2007-04-27 : 07:36:23
|
| THANX ALL.IT WAS OF GREAT HELPvishuBangalore |
 |
|
|
|