I have written a simple stored procedure with some output parameters, but when I execute it from my C# code, I get an error
Expects Parameters
It is expecting me provide values for the OUTPUT parameters ...
Please help me figure out the error ...
My stored procedure:
ALTER PROCEDURE [dbo].[Check_EntryTrade_value]
#Account_ID bigint,
#Date datetime2(7),
#tradeby varchar(20),
#Symbol varchar(20),
#B_S varchar(20),
#RateE float ,
#Usd_Marg bigint OUTPUT,
#Amount bigint OUTPUT,
#Rate float OUTPUT,
#Entry_Order_id bigint OUTPUT
AS
BEGIN
SELECT
#Rate = [Rate],
#Entry_Order_id = [EntryOrder_ID],
#Amount = [Amount],
#Usd_Marg = [Usd_Mrg]
FROM
[dbo].[entrytable]
WHERE
[B_S] = #B_S
AND [Rate] = #RateE
AND [Account_ID] = #Account_ID
AND [Symbol] = #Symbol;
IF (#Rate IS NOT NULL)
INSERT INTO neworder (Account_ID, Symbol, B_S, Rate, Amount, Date, Usd_Marg, tradeby)
VALUES(#Account_ID, #Symbol, #B_S, #Rate, #Amount, #Date, #Usd_Marg, #tradeby);
DELETE FROM entrytable WHERE EntryOrder_ID = #Entry_Order_id;
END
And this is my C# Code:
SqlConnection conn = new SqlConnection(Properties.Settings.Default.MyCon);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
//SQL INJECTION '"+ +"'
cmd.CommandText = "Check_EntryTrade_value";
SqlParameter pID1 = new SqlParameter("#Account_ID", SqlDbType.BigInt, 10);
pID1.Value = 1;
SqlParameter pID2 = new SqlParameter("#Symbol", SqlDbType.VarChar, 20);
pID2.Value = "EUR/USD";
SqlParameter pID3 = new SqlParameter("#B_S", SqlDbType.VarChar, 20);
pID3.Value = "Buy";
SqlParameter pID4 = new SqlParameter("#RateE", SqlDbType.Float, 10);
pID4.Value = 1.24763;
//SqlParameter pID6 = new SqlParameter("#Amount", SqlDbType.BigInt, 10);
//pID6.Value = 1;
SqlParameter pID5 = new SqlParameter("#Date", SqlDbType.DateTime2, 7);
pID5.Value = DateTime.Now.ToShortDateString();
SqlParameter pID6 = new SqlParameter("#tradeby", SqlDbType.VarChar, 20);
pID6.Value = "User";
cmd.Parameters.Add(pID1);
cmd.Parameters.Add(pID2);
cmd.Parameters.Add(pID3);
cmd.Parameters.Add(pID4);
cmd.Parameters.Add(pID5);
cmd.Parameters.Add(pID6);
cmd.ExecuteNonQuery();
Running my C# code returns this error:
Procedure or Function 'Check_EntryTrade_value' expects parameter '#Usd_Marg', which was not supplied.
I Found it :) .. Thanks Every One for Help .. i love StackOverFlow :P
Here is what i did :
//STEP-1 CONNECTION
SqlConnection conn = new SqlConnection(Properties.Settings.Default.MyCon);
//STEP-2 COmmand
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
//SQL INJECTION '"+ +"'
cmd.CommandText = "Check_EntryTrade_value";
SqlParameter pID1 = new SqlParameter("#Account_ID", SqlDbType.BigInt, 10);
pID1.Value = 1;
SqlParameter pID2 = new SqlParameter("#Symbol", SqlDbType.VarChar, 20);
pID2.Value = "EUR/USD";
SqlParameter pID3 = new SqlParameter("#B_S", SqlDbType.VarChar, 20);
pID3.Value = "Buy";
SqlParameter pID4 = new SqlParameter("#RateE", SqlDbType.Float, 10);
pID4.Value = 1.24763;
//SqlParameter pID6 = new SqlParameter("#Amount", SqlDbType.BigInt, 10);
//pID6.Value = 1;
SqlParameter pID5 = new SqlParameter("#Date", SqlDbType.DateTime2, 7);
pID5.Value = DateTime.Now.ToShortDateString();
SqlParameter pID6 = new SqlParameter("#tradeby", SqlDbType.VarChar, 20);
pID6.Value = "User";
SqlParameter pID7 = new SqlParameter("#Usd_Marg", SqlDbType.BigInt, 10);
pID7.Direction = ParameterDirection.Output;
// pID6.Value = "User";
SqlParameter pID8 = new SqlParameter("#Amount", SqlDbType.BigInt, 20);
pID8.Direction = ParameterDirection.Output;
// pID6.Value = "User";
SqlParameter pID9 = new SqlParameter("#Rate", SqlDbType.BigInt, 20);
pID9.Direction = ParameterDirection.Output;
// pID6.Value = "User";
SqlParameter pID10 = new SqlParameter("#Entry_Order_id", SqlDbType.BigInt, 20);
pID10.Direction = ParameterDirection.Output;
// pID6.Value = "User";
cmd.Parameters.Add(pID1);
cmd.Parameters.Add(pID2);
cmd.Parameters.Add(pID3);
cmd.Parameters.Add(pID4);
cmd.Parameters.Add(pID5);
cmd.Parameters.Add(pID6);
cmd.Parameters.Add(pID7);
cmd.Parameters.Add(pID8);
cmd.Parameters.Add(pID9);
cmd.Parameters.Add(pID10);
Try to remove
#Usd_Marg bigint OUTPUT,
#Amount bigint OUTPUT,
#Rate float OUTPUT,
#Entry_Order_id bigint OUTPUT
from stored procedure definition .
Modify your query :
SELECT [Rate], [EntryOrder_ID],[Amount],[Usd_Mrg]
FROM [dbo].[entrytable]
WHERE [B_S] = #B_S AND [Rate] = #RateE AND [Account_ID] = #Account_ID AND [Symbol] = #Symbol;
On the C# side you will receive a data set so you need to change last line of c# code :
cmd.ExecuteScalar();
Related
Need Help! I am new in executing function in a stored procedure package, I need to execute this package using oracle C# windows application..
CREATE OR REPLACE PACKAGE TSCBE1005_PKG IS
FUNCTION TSCBE1005_001_FNC( P_BLD_LVL_CD IN VARCHAR2,
P_IN_DATE IN VARCHAR2,
P_IN_PC IN VARCHAR2,
P_BLD_SPEC_NO IN OUT VARCHAR2,
P_BLD_SPEC_NO_SEQ IN OUT NUMBER,
P_ART_NO IN OUT VARCHAR2`,
P_CURE_KIBN IN OUT VARCHAR2,
P_CURE_LR_KBN IN OUT VARCHAR2,
P_HINMEI IN OUT VARCHAR2,
P_TIRE_SIZE IN OUT VARCHAR2,
P_E4_MARK IN OUT NUMBER,
P_SECTOR_NO IN OUT VARCHAR2,
P_UPLATE_NO IN OUT VARCHAR2,
P_LPLATE_NO IN OUT VARCHAR2,
P_BLADER_NO IN OUT VARCHAR2,
P_MAX_TCLC_SINGLE IN OUT NUMBER,
P_TCLC_UNIT_SINGLE IN OUT VARCHAR2,
P_MAX_IP_SINGLE IN OUT NUMBER,
P_IP_UNIT_SINGLE IN OUT VARCHAR2,
P_PR IN OUT NUMBER,
P_MSG IN OUT VARCHAR2
) RETURN NUMBER;
PROCEDURE TSCBE1005_002_PRC( P_RET_DATA IN NUMBER,
P_INFO1 IN VARCHAR2,
P_INFO2 IN VARCHAR2,
P_IN_PC IN VARCHAR2
) ;
END TSCBE1005_PKG;
When I'm running it to Oracle, using script below, it's actually successful.
variable O_BLD_SPEC_NO VARCHAR2(7)
variable O_BLD_SPEC_NO_SEQ NUMBER
variable O_ART_NO VARCHAR2(5)
variable O_CURE_KIBN VARCHAR2(4)
variable O_CURE_LR_KBN VARCHAR2(1)
variable O_HINMEI VARCHAR2(50)
variable O_TIRE_SIZE VARCHAR2(20)
variable O_E4_MARK NUMBER
variable O_SECTOR_NO VARCHAR2(6)
variable O_UPLATE_NO VARCHAR2(6)
variable O_LPLATE_NO VARCHAR2(6)
variable O_BLADER_NO VARCHAR2(7)
variable O_MAX_TCLC_SINGLE NUMBER
variable O_TCLC_UNIT_SINGLE VARCHAR2(3)
variable O_MAX_IO_SINGLE NUMBER
variable O_IO_UNIT_SINGLE VARCHAR2(3)
variable O_PR NUMBER
variable O_MSG VARCHAR2(20)
variable ret varchar2(100)
execute :ret := TSCBE1005_PKG.TSCBE1005_001_FNC('R123456789','20170124120644','YRC07400',:O_BLD_SPEC_NO,:O_BLD_SPEC_NO_SEQ,:O_ART_NO,:O_CURE_KIBN,:O_CURE_LR_KBN,:O_HINMEI,:O_TIRE_SIZE,:O_E4_MARK,:O_SECTOR_NO,:O_UPLATE_NO,:O_LPLATE_NO,:O_BLADER_NO,:O_MAX_TCLC_SINGLE,:O_TCLC_UNIT_SINGLE,:O_MAX_IO_SINGLE,:O_IO_UNIT_SINGLE,:O_PR,:O_MSG)
;
print O_BLD_SPEC_NO
print O_BLD_SPEC_NO_SEQ
print O_ART_NO
print O_CURE_KIBN
print O_CURE_LR_KBN
print O_HINMEI
print O_TIRE_SIZE
print O_E4_MARK
print O_SECTOR_NO
print O_UPLATE_NO
print O_LPLATE_NO
print O_BLADER_NO
print O_MAX_TCLC_SINGLE
print O_TCLC_UNIT_SINGLE
print O_MAX_IO_SINGLE
print O_IO_UNIT_SINGLE
print O_PR
print O_MSG
print ret
I tried to execute this in C# oracle using below code,
using (OracleConnection objConn = new OracleConnection("User id=skraze; Password=skraze; Data source=database"))
{
objConn.Open();
OracleCommand objCmd = new OracleCommand();
objCmd.Connection = objConn;
objCmd.CommandText = "TSCBE1005_PKG.TSCBE1005_001_FNC";
objCmd.CommandType = CommandType.StoredProcedure;
OracleParameter P_BLD_LVL_CD = new OracleParameter();
P_BLD_LVL_CD.ParameterName = ":P_BLD_LVL_CD";
P_BLD_LVL_CD.OracleType = OracleType.VarChar;
P_BLD_LVL_CD.Direction = ParameterDirection.Input;
P_BLD_LVL_CD.Value = "R123456789";
objCmd.Parameters.Add(P_BLD_LVL_CD);
OracleParameter P_IN_DATE = new OracleParameter();
P_IN_DATE.ParameterName = ":P_IN_DATE";
P_IN_DATE.OracleType = OracleType.VarChar;
P_IN_DATE.Direction = ParameterDirection.Input;
P_IN_DATE.Value = "20170124120644";
objCmd.Parameters.Add(P_IN_DATE);
OracleParameter P_IN_PC = new OracleParameter();
P_IN_PC.ParameterName = ":P_IN_PC";
P_IN_PC.OracleType = OracleType.VarChar;
P_IN_PC.Direction = ParameterDirection.Input;
P_IN_PC.Value = "YRC07400";
objCmd.Parameters.Add(P_IN_PC);
OracleParameter P_BLD_SPEC_NO = new OracleParameter();
P_BLD_SPEC_NO.ParameterName = ":P_BLD_SPEC_NO";
P_BLD_SPEC_NO.OracleType = OracleType.VarChar;
P_BLD_SPEC_NO.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_BLD_SPEC_NO);
OracleParameter P_BLD_SPEC_NO_SEQ = new OracleParameter();
P_BLD_SPEC_NO_SEQ.ParameterName = ":P_BLD_SPEC_NO_SEQ";
P_BLD_SPEC_NO_SEQ.OracleType = OracleType.Number;
P_BLD_SPEC_NO_SEQ.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_BLD_SPEC_NO_SEQ);
OracleParameter P_ART_NO = new OracleParameter();
P_ART_NO.ParameterName = ":P_ART_NO";
P_ART_NO.OracleType = OracleType.VarChar;
P_ART_NO.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_ART_NO);
OracleParameter P_CURE_KIBN = new OracleParameter();
P_CURE_KIBN.ParameterName = ":P_CURE_KIBN";
P_CURE_KIBN.OracleType = OracleType.VarChar;
P_CURE_KIBN.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_CURE_KIBN);
OracleParameter P_CURE_LR_KBN = new OracleParameter();
P_CURE_LR_KBN.ParameterName = ":P_CURE_LR_KBN";
P_CURE_LR_KBN.OracleType = OracleType.VarChar;
P_CURE_LR_KBN.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_CURE_LR_KBN);
OracleParameter P_HINMEI = new OracleParameter();
P_HINMEI.ParameterName = ":P_HINMEI";
P_HINMEI.OracleType = OracleType.VarChar;
P_HINMEI.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_HINMEI);
OracleParameter P_TIRE_SIZE = new OracleParameter();
P_TIRE_SIZE.ParameterName = ":P_TIRE_SIZE";
P_TIRE_SIZE.OracleType = OracleType.VarChar;
P_TIRE_SIZE.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_TIRE_SIZE);
OracleParameter P_E4_MARK = new OracleParameter();
P_E4_MARK.ParameterName = ":P_E4_MARK";
P_E4_MARK.OracleType = OracleType.Number;
P_E4_MARK.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_E4_MARK);
OracleParameter P_SECTOR_NO = new OracleParameter();
P_SECTOR_NO.ParameterName = ":P_SECTOR_NO";
P_SECTOR_NO.OracleType = OracleType.VarChar;
P_SECTOR_NO.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_SECTOR_NO);
OracleParameter P_UPLATE_NO = new OracleParameter();
P_UPLATE_NO.ParameterName = ":P_UPLATE_NO";
P_UPLATE_NO.OracleType = OracleType.VarChar;
P_UPLATE_NO.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_UPLATE_NO);
OracleParameter P_LPLATE_NO = new OracleParameter();
P_LPLATE_NO.ParameterName = ":P_LPLATE_NO";
P_LPLATE_NO.OracleType = OracleType.VarChar;
P_LPLATE_NO.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_LPLATE_NO);
OracleParameter P_BLADER_NO = new OracleParameter();
P_BLADER_NO.ParameterName = ":P_BLADER_NO";
P_BLADER_NO.OracleType = OracleType.VarChar;
P_BLADER_NO.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_BLADER_NO);
OracleParameter P_MAX_TCLC_SINGLE = new OracleParameter();
P_MAX_TCLC_SINGLE.ParameterName = ":P_MAX_TCLC_SINGLE";
P_MAX_TCLC_SINGLE.OracleType = OracleType.Number;
P_MAX_TCLC_SINGLE.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_MAX_TCLC_SINGLE);
OracleParameter P_TCLC_UNIT_SINGLE = new OracleParameter();
P_TCLC_UNIT_SINGLE.ParameterName = ":P_TCLC_UNIT_SINGLE";
P_TCLC_UNIT_SINGLE.OracleType = OracleType.VarChar;
P_TCLC_UNIT_SINGLE.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_TCLC_UNIT_SINGLE);
OracleParameter P_MAX_IO_SINGLE = new OracleParameter();
P_MAX_IO_SINGLE.ParameterName = ":P_MAX_IO_SINGLE";
P_MAX_IO_SINGLE.OracleType = OracleType.Number;
P_MAX_IO_SINGLE.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_MAX_IO_SINGLE);
OracleParameter P_IO_UNIT_SINGLE = new OracleParameter();
P_IO_UNIT_SINGLE.ParameterName= ":P_IO_UNIT_SINGLE";
P_IO_UNIT_SINGLE.OracleType = OracleType.VarChar;
P_IO_UNIT_SINGLE.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_IO_UNIT_SINGLE);
OracleParameter P_PR = new OracleParameter();
P_PR.ParameterName = ":P_PR";
P_PR.OracleType = OracleType.Number;
P_PR.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_PR);
OracleParameter W_BLD_SPEC_NO = new OracleParameter();
W_BLD_SPEC_NO.OracleType = OracleType.VarChar;
W_BLD_SPEC_NO.Size = 7;
W_BLD_SPEC_NO.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_BLD_SPEC_NO);
OracleParameter W_BLD_SPEC_NO_SEQ = new OracleParameter();
W_BLD_SPEC_NO_SEQ.OracleType = OracleType.Number;
W_BLD_SPEC_NO_SEQ.Size = 1;
W_BLD_SPEC_NO_SEQ.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_BLD_SPEC_NO_SEQ);
OracleParameter W_ART_NO = new OracleParameter();
W_ART_NO.OracleType = OracleType.VarChar;
W_ART_NO.Size = 5;
W_ART_NO.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_ART_NO);
OracleParameter W_CURE_KIBN = new OracleParameter();
W_CURE_KIBN.OracleType = OracleType.VarChar;
W_CURE_KIBN.Size = 4;
W_CURE_KIBN.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_CURE_KIBN);
OracleParameter W_CURE_LR_KBN = new OracleParameter();
W_CURE_LR_KBN.OracleType = OracleType.VarChar;
W_CURE_LR_KBN.Size = 1;
W_CURE_LR_KBN.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_CURE_LR_KBN);
OracleParameter W_HINMEI = new OracleParameter();
W_HINMEI.OracleType = OracleType.VarChar;
W_HINMEI.Size = 50;
W_HINMEI.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_HINMEI);
OracleParameter W_TIRE_SIZE = new OracleParameter();
W_TIRE_SIZE.OracleType = OracleType.VarChar;
W_TIRE_SIZE.Size = 20;
W_TIRE_SIZE.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_TIRE_SIZE);
OracleParameter W_E4_MARK = new OracleParameter();
W_E4_MARK.OracleType = OracleType.Number;
W_E4_MARK.Size = 1;
W_E4_MARK.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_E4_MARK);
OracleParameter W_SECTOR_NO = new OracleParameter();
W_SECTOR_NO.OracleType = OracleType.VarChar;
W_SECTOR_NO.Size = 6;
W_SECTOR_NO.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_SECTOR_NO);
OracleParameter W_UPLATE_NO = new OracleParameter();
W_UPLATE_NO.OracleType = OracleType.VarChar;
W_UPLATE_NO.Size = 6;
W_UPLATE_NO.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_UPLATE_NO);
OracleParameter W_LPLATE_NO = new OracleParameter();
W_LPLATE_NO.OracleType = OracleType.VarChar;
W_LPLATE_NO.Size = 6;
W_LPLATE_NO.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_LPLATE_NO);
OracleParameter W_BLADER_NO = new OracleParameter();
W_BLADER_NO.OracleType = OracleType.VarChar;
W_BLADER_NO.Size = 7;
W_BLADER_NO.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_BLADER_NO);
OracleParameter W_MAX_TCLC_SINGLE = new OracleParameter();
W_MAX_TCLC_SINGLE.OracleType = OracleType.Number;
W_MAX_TCLC_SINGLE.Size = 1;
W_MAX_TCLC_SINGLE.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_MAX_TCLC_SINGLE);
OracleParameter W_TCLC_UNIT_SINGLE = new OracleParameter();
W_TCLC_UNIT_SINGLE.OracleType = OracleType.VarChar;
W_TCLC_UNIT_SINGLE.Size = 3;
W_TCLC_UNIT_SINGLE.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_TCLC_UNIT_SINGLE);
OracleParameter W_MAX_IO_SINGLE = new OracleParameter();
W_MAX_IO_SINGLE.OracleType = OracleType.Number;
W_MAX_IO_SINGLE.Size = 1;
W_MAX_IO_SINGLE.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_MAX_IO_SINGLE);
OracleParameter W_IO_UNIT_SINGLE = new OracleParameter();
W_IO_UNIT_SINGLE.OracleType = OracleType.VarChar;
W_IO_UNIT_SINGLE.Size = 3;
W_IO_UNIT_SINGLE.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_IO_UNIT_SINGLE);
OracleParameter W_PR = new OracleParameter();
W_PR.OracleType = OracleType.Number;
W_PR.Size = 1;
W_PR.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_PR);
objCmd.ExecuteNonQuery();
objConn.Close();
}
But I'm still getting error
{"ORA-06550: line 1, column 34:\nPLS-00103: Encountered the symbol \"=\" when expecting one of the following:\n\n . ( * # % & = - + ; < / > at in is mod not rem\n <an exponent (**)> <> or != or ~= >= <= <> and or like\n between || indicator\n"}
Please, I hope anyone could help me :(
This looks wrong
P_BLD_LVL_CD.ParameterName = ":P_BLD_LVL_CD";
^
The colon is used for identifying placeholders in a SQL string. Actual parameter name should be
P_BLD_LVL_CD.ParameterName = "P_BLD_LVL_CD";
Also ParameterDirection.ReturnValue is used for the return value only. So there should only be one such parameter per function call. You appear to be using it for all the IN OUT parameters. That is wrong.
For IN OUT parameters you need to use ParameterDirection.InputOutput.
Incidentally a function with IN OUT parameters is poor PL/SQL practice, but I guess there's not much you can do about that.
So, TSCBE1005_001_FNC is a function which takes three IN (read only) parameters and eighteen IN OUT (read/write) parameters. Therefore your call needs:
one parameter with ParameterDirection.ReturnValue
three parameters with ParameterDirection.Input
eighteen parameters with ParameterDirection.InputOutput
I never used objCmd.CommandType = CommandType.StoredProcedure;
I always use this syntax:
objCmd.CommandText = "BEGIN :res := TSCBE1005_PKG.TSCBE1005_001_FNC(:P_BLD_LVL_CD, :P_IN_DATE, ...); END;";
objCmd.CommandType = CommandType.Text;
Then your parameter declaration is wrong.
P_SECTOR_NO.Direction = ParameterDirection.Input;
ParameterDirection has to match signature of your function, i.e. ReturnValue or Input or Output (not used in your example) or InputOutput
The type is also wrong:
P_SECTOR_NO.OracleType = OracleType.VarChar;
You must use OracleDbType, see OracleDbType Enumeration. For mapping between OracleDbType and .NET type see C#: Oracle Data Type Equivalence with OracleDbType
btw, you can put this all in one command, e.g.
cmd.Parameters.Add("P_BLD_LVL_CD", OracleDbType.Varchar2, ParameterDirection.Input).Value = "R035570412";
Be aware in case of ParameterDirection.Output you have to set DbType separately. Also in case of OracleDbType.Varchar2 you must specify the size and an initial value:
cmd.Parameters.Add("parName", OracleDbType.Varchar2, 240, null, ParameterDirection.Output);
cmd.Parameters("parName").DbType = DbType.String;
Good afternoon, I'm running a procedure and returning a cursor, so going the ORA-01652 error: unable to extend temp segment by 128 in tablespace TEMP
C# , ASP.NET
OracleCommand cmd = connection.CreateCommand();
OracleDataReader dr;
DataTable dt = new DataTable();
try
{
OpenConn();
cmd.CommandText = "UNITIZA_RELATORIOS.getComparaZila";
cmd.CommandType = CommandType.StoredProcedure;
OracleParameter par1 = new OracleParameter("vElemServico", OracleType.VarChar);
par1.Value = elemServico;
par1.Direction = ParameterDirection.Input;
OracleParameter par2 = new OracleParameter("vdataInicio", OracleType.VarChar);
par2.Value = dataInicio;
par2.Direction = ParameterDirection.Input;
OracleParameter par3 = new OracleParameter("vdataFim", OracleType.VarChar);
par3.Value = dataFim;
par3.Direction = ParameterDirection.Input;
OracleParameter par4 = new OracleParameter("vOrdem", OracleType.VarChar);
par4.Value = ordem;
par4.Direction = ParameterDirection.Input;
OracleParameter par5 = new OracleParameter("vCodAneel", OracleType.VarChar);
par5.Value = codigoAneel;
par5.Direction = ParameterDirection.Input;
OracleParameter par6 = new OracleParameter("nCodigoError", OracleType.Number);
par6.Size = 1;
par6.Direction = ParameterDirection.Output;
OracleParameter par7 = new OracleParameter("return_value", OracleType.Cursor);
par7.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(par1);
cmd.Parameters.Add(par2);
cmd.Parameters.Add(par3);
cmd.Parameters.Add(par4);
cmd.Parameters.Add(par5);
cmd.Parameters.Add(par6);
cmd.Parameters.Add(par7);
dr = cmd.ExecuteReader();
dt.Load(dr);
return dt;
Note: The error only when acontence consultation over 1800 lines
This indicates your process is chewing up all the temporary tablespace...perhaps inefficient joins or query syntax. Have a look at the explain plan and see why it needs all that space. It maybe easier to optimize the query than the other solution:
From here
Create a new datafile by running the following command:
alter tablespace TABLE_SPACE_NAME add datafile 'D:\oracle\Oradata\TEMP04.dbf' size 2000M autoextend on;
I am trying to get data returned from my stored procedure. This is what I have tried and absolutely nothing is coming back.
Stored Proc:
Declare #Submit_ID as int
Set #Submit_ID = (Select (Max(Sub_ID) + 1) from Sub_Details);
INSERT INTO.....
Return #Submit_ID
C# (2.0)
SqlConnection conn = null;
int subid;
try
{
conn = new SqlConnection(conn_string);
SqlCommand cmd = new SqlCommand("INSERT_INTO_MYDB", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("#FirstName", SqlDbType.NVarChar, 255);
p1.Direction = ParameterDirection.Input;
p1.Value = txtFirstName.Text;
cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("#LastName", SqlDbType.NVarChar, 255);
p2.Direction = ParameterDirection.Input;
p2.Value = txtLastName.Text;
cmd.Parameters.Add(p2);
SqlParameter pSub = new SqlParameter("#Sub_ID", SqlDbType.Int);
pSub.Direction = ParameterDirection.ReturnValue;
conn.Open();
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(pSub);
}
You're not adding the return parameter to the command:
SqlParameter pSub = new SqlParameter("#Sub_ID", SqlDbType.Int);
pSub.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(pSub); // <------------- add to command
conn.Open();
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(cmd.Parameters["pSub"].Value);
First add parameter to cmd and use .Value to its value
cmd.Parameters.Add(pSub); // add parameters to command
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(pSub.Value); // Use .Value
I'm struggling with this thing for the past hour and I'm sure I'm missing something small, I have a stored procedure in SQL Server 2008 and C# code that I want to return the output parameters of my stored procedure.
SQL :
Alter Procedure dbo.GetAssessment
#UserID int,
#AssessmentName varchar(255),
#Score varchar(100) output,
#Completed varchar(10) output,
#DisplayName nvarchar(128) output,
#Result varchar(2500) output
as
begin
select #Score = A.Score, #Completed = A.Completed, #DisplayName = U.Displayname, #Result = A.Result
from Assessment A
inner join Users U
on U.UserId = A.UserID
where U.UserID = #UserId
and AssessmentName = #AssessmentName
end
GO
C#
String SScore, SName, SResult, SComp;
lblAsse.Text = Request.QueryString["AID"];
InsertAssessment(lblAsse.Text, "No", 2, "N/A", "N/A");
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString))
{
SqlParameter outScore = new SqlParameter("#Score", SqlDbType.VarChar,100){ Direction = ParameterDirection.Output };
SqlParameter outComp = new SqlParameter("#Completed", SqlDbType.VarChar,10){ Direction = ParameterDirection.Output };
SqlParameter outName = new SqlParameter("#DisplayName", SqlDbType.NVarChar, 128) { Direction = ParameterDirection.Output };
SqlParameter outResult = new SqlParameter("#Result", SqlDbType.VarChar,2500){ Direction = ParameterDirection.Output };
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "GetAssessment";
cmd.Parameters.AddWithValue("#AssessmentName", lblAsse.Text);
cmd.Parameters.AddWithValue("#UserId", 2);
cmd.Parameters.Add(outScore);
cmd.Parameters.Add(outComp);
cmd.Parameters.Add(outName);
cmd.Parameters.Add(outResult);
cmd.ExecuteScalar();
SScore = outScore.ToString();
SName = outName.ToString();
SResult = outResult.ToString();
SComp = outComp.ToString();
conn.Close();
lblAsse.Text = SScore;`
Output :
#Score
What can possibly be wrong with me or my code. Please help!
You just need to read out the actual values from your output parameters:
SScore = outScore.Value;
The .ToString() doesn't return the value - it returns the name of the parameter instead...
See the MSDN documentation on SqlParameter for more details.
just need to do this.
Before getting the output parameters you must close the Data reader as
reader.Close();
and then you get output parameters as
SScore = outScore.Value.Tostring();
for more help consult this http://msdn.microsoft.com/en-us/library/ms971497
>Try this its working fine for the multiple output parameter:
using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["conStringEndicia"].ConnectionString)){
using (var sqlCmd = new SqlCommand("endicia.credentialLookup", sqlConnection))
{
sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("#accountNumber", accountNumber);
SqlParameter outLogin = new SqlParameter("#login", SqlDbType.NVarChar, 100) { Direction = ParameterDirection.Output };
sqlCmd.Parameters.Add(outLogin);
SqlParameter outPassword = new SqlParameter("#password", SqlDbType.NVarChar, 100) { Direction = ParameterDirection.Output };
sqlCmd.Parameters.Add(outPassword);
sqlConnection.Open();
sqlCmd.ExecuteNonQuery();
string login, password;
login = outLogin.Value.ToString();
password = outPassword.Value.ToString();
}
}
I have a register form and I want to check that the user did not register before.
My code is in below. I think two problems exist: (1) the call method and (2) passing the parameter to the stored procedure. My symptom is that this causes an exception that says the input parameter not initialized.
create procedure fakeuser #username nvarchar(250),#codemeli nchar(10),#email nvarchar(50), #user nvarchar(250) output,#code nchar(10)output,#mail nvarchar(50)output
as
if exists(select username,email,codemeli from karbar where username=#username)
set #user=#username
else if exists(select username,email,codemeli from karbar where codemeli=#codemeli)
set #code=#codemeli
else if exists(select username,email,codemeli from karbar where email=#email)
set #mail= #email
Here is the c# code:
public static string confirm(string username, string email, string codemeli)
{
string constring = "data source=.;database=site;integrated security=true;";
SqlConnection connection = new SqlConnection(constring);
// Command - specify as StoredProcedure
SqlCommand command = new SqlCommand("fakeuser", connection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("#username", SqlDbType.NVarChar);
param.Direction = ParameterDirection.Input;
param.Value = username;
command.Parameters.Add(param);
SqlParameter param2 = new SqlParameter("#email", SqlDbType.NVarChar);
param2.Direction = ParameterDirection.Input;
param2.Value = username;
command.Parameters.Add(param2);
SqlParameter param3 = new SqlParameter("#codemeli", SqlDbType.NChar);
param3.Direction = ParameterDirection.Input;
param3.Value = username;
command.Parameters.Add(param3);
// Return value as parameter
SqlParameter returnuser = new SqlParameter("#user", SqlDbType.NVarChar);
returnuser.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnuser);
SqlParameter returncode = new SqlParameter("#code", SqlDbType.NChar);
returncode.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returncode);
SqlParameter returnmail = new SqlParameter("#mail", SqlDbType.NVarChar);
returnmail.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnmail);
// Execute the stored procedure
connection.Open();
command.ExecuteNonQuery();
connection.Close();
Thank you.
Set ParameterDirection=Output for #user,#code, and #mail and also specify the width/size/length of parameter.
SqlParameter returnuser = new SqlParameter("#user", SqlDbType.NVarChar,100);
returnuser.Direction = ParameterDirection.Output;
command.Parameters.Add(returnuser);