I have the following stored procedure:
ALTER procedure [dbo].[spx_kasir_verifikator_GetData_web]
#id_verifikator int
as
begin
SELECT * FROM tb_kasir_set_verifikator
WHERE tb_kasir_set_verifikator.id_verifikator = id_verifikator;
end
Controller:
public function show($id_verifikator)
{
$setverifikator = DB::select("exec spx_kasir_verifikator_GetData_web ?",[$id_verifikator]);
dd($setverifikator);
}
And I'm trying to call this procedure in Laravel 8, I need to display just one id or by id_verifikator but it's always showed all data. How I solve this?
It's returning everything as you are using select * which tells the database that you want the entire row. If you wish to only get a specific column then you need to change that to select id_verifikator.
A bit off-topic but I would suggest that you just run this query in Laravel instead of having a procedure, especially as this is such a basic query. The below links can help you get started.
https://laravel.com/docs/8.x/queries
https://laravel.com/docs/8.x/eloquent
Related
I'm writing a web API and trying to get sequence from the oracle database and create a method with help of this
var p = new Oracle.ManagedDataAccess.Client.OracleParameter("#result", Oracle.ManagedDataAccess.Client.OracleDbType.Int32);
p.Direction = System.Data.ParameterDirection.Output;
Context.Database.ExecuteSqlRaw("set #result = next value for Social_Media_Clip_Seq", p);
var nextVal = (int)p.Value;
but facing error ORA-00922: missing or invalid option
Can anyone help me to find what is missing or is any other way to find out the solution
Thanks in advance
If your API needs to find nextval only to insert new records into database, maybe you can configure the model to use the sequence directly for generating the new Ids:
protected override void OnModelCreating(ModelBuilder modelBuilder) {
...
modelBuilder.HasSequence<long>("SEQUENCE_DB_NAME");
modelBuilder.Entity<C#TYPENAME>(entity =>
{
entity.Property(e => e.Id)
.HasColumnName("ID")
.ValueGeneratedOnAdd()
.UseHiLo("SEQUENCE_DB_NAME");
});
Source:
https://learn.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-implementation-entity-framework-core#the-hilo-algorithm-in-ef-core
However, my environment is EF Core 5.0.13, Oracle EntityFrameworkCore 5.21.5,
Oracle database version is 19.3.
Apart from Oracle, I don't know anything about other tools you use.
However: link you posted is related to MS SQL Server.
In Oracle, a sequence next value is returned by sequence_name.nextval; something like this:
SQL> select seq_test.nextval from dual;
NEXTVAL
----------
4
SQL>
I wouldn't know how to implement it into code you posted.
I'm writing an installation script in php, this script has to ask the user some data and then store them in a XML file and on a database.
Here is the procedure:
1) Insert data;
2) Save configuration data (such as database name,user,password,host,port) in a XML file
3) Using those data to install a database (I have an empty dump I use to create it)
4) Insert in the "admin" table the first user data (taken from the form in point 1)
Now, everything works fine until point 4.
When I try to access the database MySql always returns my an error #1146 saying that the table doesn't not exists.
This is my custom JSON log:
{
"Status":"KO",
"Reason":"SELECT * FROM bbscp_admin_user; || Table 'adb.bbscp_admin_user' doesn't exist",
"Errno":1146
}
Obviously I checked both with phpMyAdmin and mysql (from cli) and I can say that both DB and Table DO exists.
Here is the part where I create the DB: [this works fine]
$Install = Install::getInstance();
$sqlscript = str_replace("__DBNAME__",
$config_info['dbname'],
file_get_contents('../config/bbscp-base-db.sql')
);
$connection = mysqli_connect($config_info['dbhost'],
$config_info['dbuser'],
$config_info['dbpwd']
) or die("Unable to connect! Error: ".mysql_error());
if(mysqli_query($connection,'CREATE DATABASE '.$config_info['dbname'].';')){
echo 'Database successfully createad';
mysqli_close($connection);
}else{
die('ERROR CREATING DATABASE!');
}
Here the part where I populate the database from the dump (it only add the tables)
$connection = mysqli_connect($config_info['dbhost'],
$config_info['dbuser'],
$config_info['dbpwd'],
$config_info['dbname']
) or die("Unable to connect! Error: ".mysql_error());
if(mysqli_multi_query($connection, $sqlscript)){
echo 'Database successfully imported';
mysqli_close($connection);
}else{
die('ERROR IMPORTING DATABASE');
}
Now the decisive part, I open a connection with the database (using a library that I've developed in the past years and has always worked fine)
$DB = new Database(Install::getInstance());
$Q = new Query();
$DB->connect();
$query = $Q->addNewUser($config_info['nickname'],
$config_info['password'],
$config_info['firstname'],
$config_info['lastname'],
$config_info['email'],
0); // this just returns the query string I need
// the query in this case is:
// INSERT INTO bbscp_admin_user
// (nickname,password,firstname,lastname,mail,confirmed)
// VALUES (the ones above);"
$res=$DB->startQuery($query); // THIS IS WHERE THE ERROR IS RETURNED
// this function only wraps the mysqli_query and returns a custom JSON
I've tried a lot of solution.. The tables DO exists but every query I try to invoke at this point doesn't work because apparently "table doesn't exist" (even using mysqli function instead of mines).
Thanks in advance and sorry for the long question! :-)
EDIT 1
During this days I've changed a lot of code, I tried using different implementation using functions, objects, redirects but none of them seems to work.
The thing is always the same:
It creates the database
Returns the error code when trying to add the user
If I refresh the page it correctly adds the user
All.
We have an Oracle package that returns a ref cursor:
CREATE OR REPLACE PACKAGE BODY sandbox AS
FUNCTION my_function (text VARCHAR2) RETURN result_cv IS result result_cv;
BEGIN
OPEN result FOR SELECT MLS_SID FROM MLS;
RETURN result;
END;
END sandbox;
I am calling the function with the following scala code:
lazy val database = Database.forDataSource(DB.getDataSource())
database withSession {
val x = sql"select sandbox.my_function($text) from DUAL".as[(Int)]
x foreach (x => println(x))
Ok(String.valueOf(x.first))
}
The code fails with the following error:
[SQLException: Invalid column type: getInt not implemented for class oracle.jdbc.driver.T4CResultSetAccessor]
The SQL statement works when I just use the select statement that is in the function (SELECT MLS_SID FROM MLS;), but when I open it as a ref cursor and return the ref cursor it fails. I looked at the T4CResultSetAccessor and it only has one method getBytes().
Can anyone offer suggestions on how to make this work using the Oracle function call and ref cursors? Thanks in advance.
-patrick
Frome Typesafe:
Slick doesn’t support OUT parameters at the moment (which you would
need to properly return a ref cursor). If the cursor is all you need
to return from your stored proc, I suggest to use the method
recommended in the SO post: Lift the result set to the top level with
TABLE(). Does this work for your use case?
So our solution was to do this:
SELECT * FROM TABLE(my_function('text'))
We got an error, but that was Lucas' and Typesafe's suggestion
I am migrating a VC++/SQL server app to using Oracle. The database access is implemented using ADO classes, and I can't find a way to go through the cursor that is returned by Oracle.
The sproc is something like:
create or replace PROCEDURE GetSettings
(
cv_1 OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN cv_1 FOR
SELECT KEY ,
VALUE
FROM Settings;
END;
The code is something like:
_CommandPtr pCommand;
_ParameterPtr pParam1;
HRESULT hr = pCommand.CreateInstance (__uuidof (Command));
if (FAILED (hr))
return;
pCommand->ActiveConnection = m_pConn;
pCommand->CommandText = "GetSettings";
pCommand->CommandType = adCmdStoredProc;
_RecordsetPtr pRecordset;
hr = pRecordset.CreateInstance (__uuidof (Recordset));
if (FAILED (hr))
return;
pRecordset = pCommand->Execute(NULL,NULL,adCmdStoredProc);
(in fact it is using the ADO classes from http://www.codeproject.com/Articles/1075/A-set-of-ADO-classes-version-2-20#TheSample02 )
The returned pRecordset is in a closed state and you cannot do anything with it. I imagine I should pass some parameter for the cursor, but how do you create/use/access the returned cursor using these ADO functions? There is no cursor parameter type that I can see
I am completely stuck and would greatly appreciate some help
Thanks
Finally found out how to do it, you need specify special parameters in the connection string to tell it to return result set:
Provider=ORAOLEDB.ORACLE;User ID=xxx;Password=xxx;Data Source=tns_name;OLEDB.Net=True;PLSQLRSet=True;
I have a login system for my webapp that works well using the Zend auth adapter but the problem is I want the email to be case insensitive when a user logs in. I am using Oracle as the back end DB and normally I would user the LOWER(EMAIL)=LOWER(:email) method. I tried to pass that Oracle function in the setIdentityColumn() but I get the error:
The supplied parameters to Zend_Auth_Adapter_DbTable failed to produce
a valid sql statement, please check table and column names for
validity.
protected function _getAuthAdapter()
{
//$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$db = Zend_Registry::get('db');
$authAdapter = new Zend_Auth_Adapter_DbTable($db);
$authAdapter->setTableName('USER_TABLE')
->setIdentityColumn('LOWER(EMAIL)') //Tried to pass LOWER()
->setCredentialColumn('ENCODED_PW')
->setCredentialColumn('PASSWORD');
return $authAdapter;
}
The error is coming from the function _authenticateCreateSelect() in the Zend_Auth_Adapter_DbTable class. The problem is this part of the script:
$dbSelect->from($this->_tableName, array('*', $credentialExpression))
->where($this->_zendDb->quoteIdentifier($this->_identityColumn, true) . ' = ?', $this->_identity);
The quoteIdentifier() method is like PHP quote() and is turning a query like this:
select * from LOWER(:email)
into this:
select * from "LOWER(:email)"
Anyone see a way around this?
Kind Regards
Nathan
Try something like this:
$authAdapter->setTableName('USER_TABLE')
->setIdentityColumn(new Zend_Db_Expr('LOWER(USERID)'))
->setCredentialColumn('PASSWORD');
The problem is that if you pass 'LOWER(USERID)' as a simple string, Zend will put quotes around it, causing it to create an invalid query. Using Zend_Db_Expr will stop Zend doing this.