Can I use NULL in a IN close? - tsql

How do I add the empty string and null to the list of values in the parenthesis. I think for the empty is just adding '' but for the null, I'm not that sure.
UPDATE ProjectsActiveNotes
SET NoteTypeID = 1
,NoteType = 'USER - CLIENT'
WHERE NoteCategory = 'Client' AND
NoteSubCategory IN
(
''
,'Delivery'
,'Estimate'
,'Invoicing'
,'Placement'
)
GO
Thank for helping

You can't use NULL in IN but you can add it to the WHERE clause;
UPDATE ProjectsActiveNotes
SET NoteTypeID = 1
,NoteType = 'USER - CLIENT'
WHERE NoteCategory = 'Client' AND
(NoteSubCategory IN
(
''
,'Delivery'
,'Estimate'
,'Invoicing'
,'Placement'
) or NoteSubCategory is null)
GO

use this
UPDATE ProjectsActiveNotes
SET NoteTypeID = 1 ,
NoteType = 'USER - CLIENT'
WHERE NoteCategory = 'Client'
AND COALESCE(NoteSubCategory, '') IN ( '', 'Delivery', 'Estimate',
'Invoicing', 'Placement' )

Another way is to use ISNULL :
UPDATE ProjectsActiveNotes
SET NoteTypeID = 1
,NoteType = 'USER - CLIENT'
WHERE NoteCategory = 'Client' AND
ISNULL(NoteSubCategory,'') IN
(
''
,'Delivery'
,'Estimate'
,'Invoicing'
,'Placement'
)
GO

Related

How to filter records with a not null value in Moodle?

I am trying to select a record with the mimetype field with a non-null value,
$file = $DB->get_record('table', array('itemid' => $id_imagen, 'component' => 'user', 'mimetype' => 'is not null'));
Is there any way to get this using get_record?
I answer my own question in case this can help someone
No, it is no possible to use IS NOT NULL or others with get_record function. You can acomplish it using function get_record_sql like this:
$file = $DB->get_record_sql("SELECT * FROM table WHERE itemid = " . $id_imagen . " AND component = 'user' AND mimetype IS NOT NULL");

TYPO3 - MySQL to mm Query Syntax

I have an working MySQL Query now i need it to convert to Typo3 Syntax
SELECT
tt_news_tx_extendnews_subscriber_mm.uid_local,
fe_users.*
FROM fe_users
JOIN tt_news_tx_extendnews_subscriber_mm
ON tt_news_tx_extendnews_subscriber_mm.uid_foreign = fe_users.uid
WHERE tt_news_tx_extendnews_subscriber_mm.uid_local = 101
TYPO3
$res0 = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
'tt_news_tx_extendnews_subscriber_mm.uid_local,fe_users.*',
'fe_users',
'tt_news_tx_extendnews_subscriber_mm',
'tt_news_tx_extendnews_subscriber_mm.uid_foreign = fe_users.uid',
'tt_news_tx_extendnews_subscriber_mm.uid_local = 101',
'',
'',
''
);
the result is empty...anybody knows how this work with typo3?
Debug brings this: $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery;
SELECT tt_news_tx_extendnews_subscriber_mm.uid_local,fe_users.*
FROM fe_users,tt_news_tx_extendnews_subscriber_mm,tt_news_tx_extendnews_subscriber_mm.uid_foreign = fe_users.uid
WHERE fe_users.uid=tt_news_tx_extendnews_subscriber_mm.uid_local
AND tt_news_tx_extendnews_subscriber_mm.uid_foreign = fe_users.uid.uid=tt_news_tx_extendnews_subscriber_mm.uid_foreign tt_news_tx_extendnews_subscriber_mm.uid_local = 101
By exec_SELECT_mm_query the 4th parameter is the foreign key table name, not the reference. You need instead of:
tt_news_tx_extendnews_subscriber_mm.uid_foreign = fe_users.uid
only:
fe_users
See more details in TYPO3 api : exec_SELECT_mm_query.
I think, you can try the following:
$res0 = $GLOBALS['TYPO3_DB']->exec_SELECT_mm_query(
'tt_news_tx_extendnews_subscriber_mm.uid_local,fe_users.*',
'tt_news',
'tt_news_tx_extendnews_subscriber_mm',
'fe_users',
'tt_news_tx_extendnews_subscriber_mm.uid_local = 101',
'',
'',
''
);
Or you can use exactly the SQL what you have with the following little trick, because exe_SELECTquery only concats your string to SELECT..., FROM... (and so on...) parts. So because of this, you can use a JOIN in FROM part.
$res0 = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tt_news_tx_extendnews_subscriber_mm.uid_local,fe_users.*',
'fe_users
JOIN tt_news_tx_extendnews_subscriber_mm
ON tt_news_tx_extendnews_subscriber_mm.uid_foreign = fe_users.uid',
'tt_news_tx_extendnews_subscriber_mm.uid_local = 101',
'',
'',
'');

Identity and Credential in different tables. How to login user?

I'm using Zend Framework.
I save users information in two tables.
I have one table for his basic information and password, and in the other table I save his e-mails.
He can login with any e-mail.
My question is how should I extend Zend_Auth_Adapter_DbTable so that I can allow this?
I prefer not to use table views.
[edit]
I found a solution. What worked for me:
class My_Auth_Adapter_DbTable extends Zend_Auth_Adapter_DbTable
{
protected function _authenticateCreateSelect()
{
// build credential expression
if (empty($this->_credentialTreatment) || (strpos($this->_credentialTreatment, '?') === false)) {
$this->_credentialTreatment = '?';
}
$credentialExpression = new Zend_Db_Expr(
'(CASE WHEN ' .
$this->_zendDb->quoteInto(
$this->_zendDb->quoteIdentifier($this->_credentialColumn, true)
. ' = ' . $this->_credentialTreatment, $this->_credential
)
. ' THEN 1 ELSE 0 END) AS '
. $this->_zendDb->quoteIdentifier(
$this->_zendDb->foldCase('zend_auth_credential_match')
)
);
// get select
//$dbSelect = clone $this->getDbSelect();
$mdl = new My_Model_Db_Table_Users();
$dbSelect = $mdl->select();
$dbSelect = $dbSelect->setIntegrityCheck(false);
$dbSelect = $dbSelect->from(array('u' => $this->_tableName), array('*', $credentialExpression));
$dbSelect = $dbSelect->joinInner(array('ue' => 'users_emails'), 'ue.id_user = u.user_id', array('user_email'));
$dbSelect = $dbSelect->where('ue.' . $this->_zendDb->quoteIdentifier($this->_identityColumn, true) . ' = ?', $this->_identity);
return $dbSelect;
}
}
I explained what did it for me in the question.
But, to repeat, easiest for me was to change Zend_Auth_Adapter_DbTable::_authenticateCreateSelect().
There are a method named Zend_Auth_Adapter_DbTable::getDbSelect returns Zend_Db_Select object.
Call it and then you can join those two tables.
Hope this help.
Regards,
Ahmed B.
Here's an alternate method.
Extend the Zend_Auth_Adapter_DbTable class.
class My_Auth_Adapter_DbTable extends Zend_Auth_Adapter_DbTable {
public function setDbSelect($select) {
$this->_dbSelect = $select;
return $this;
}
}
Create instance of your new adapter
$authAdapter = new My_Auth_Adapter_DbTable(
$this->dbTable->getAdapter()
, 'Users'
, 'Users.username'
, 'Users.password'
);
In your Application_Model_DbTable_Users class, create a method that returns the select object with the joined tables.
public function getSelectAuth() {
$select = $this
->select()
->setIntegrityCheck(false)
->from(array('SystemPeopleJoined' => $this->_name)
, array(
'id'
, 'person_id'
, 'system_role_id'
, 'created_on'
, 'expires_on'
)
)
->joinInner(
'People'
, 'People.id = SystemPeopleJoined.person_id'
, array(
'first_name' => 'first_name'
, 'last_name' => 'last_name'
, 'name' => "CONCAT_WS(' ', `People`.`first_name`, `People`.`last_name`)"
)
return $select;
}
Set the select object in your adapter
$select = $this->dbTable->getSelectAuth();
$authAdapter
->setDbSelect($select)
->setIdentity($params['username'])
->setCredential($params['password'])
->setCredentialTreatment("SHA1(?)")
;

can`t resolve default schema in zend_db_select

why zend_db_select does not pick up schema from config file? and how can i fix it?
config:
resources.database.adapter = "Oracle"
resources.database.params.dbname = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = x.x.x.x)(PORT = 1521)) (CONNECT_DATA = (SID = xx)))"
resources.database.params.username = ''
resources.database.params.password = ''
resources.database.params.trace =
resources.database.params.schema = YYY
select:
$select->from(
array('pm' => 'tab_1'),
array(
'pm_id',
'status',
'pm_status',
'pm_tabno',
'pm_cardno',
'pm_start_dt',
'pm_endtk_dt',
'pm_signature',
'pm_servrec_no',
'pm_isdirector'
)
)
->joinLeft(
array('pa' => 'tab_2'),
'pm.pm_id = pa.pa_pm',
array(
'PRL_NAME',
'PRF_NAME',
'PRS_NAME'
)
)
->joinLeft(
array('ddpst' => 'tab_3'),
'pm.status = ddpst.dic_value',
'dic_name'
)
->joinLeft(
array('pst' => 'tab_4'),
'pm.pm_status = pst.dic_value',
'dic_sname'
)
->where(
'pa.status = 1'
);
result:
SELECT z2.*
FROM (
SELECT z1.*, ROWNUM AS "zend_db_rownum"
FROM (
SELECT pm.pm_id, pm.status, pm.pm_status, pm.pm_tabno, pm.pm_cardno, pm.pm_start_dt, pm.pm_endtk_dt, pm.pm_signature, pm.pm_servrec_no, pm.pm_isdirector, pa.PRL_NAME, pa.PRF_NAME, pa.PRS_NAME, ddpst.dic_name, pst.dic_sname FROM tab_1 pm
LEFT JOIN tab_2 pa ON pm.pm_id = pa.pa_pm
LEFT JOIN tab_3 ddpst ON pm.status = ddpst.dic_value
LEFT JOIN tab_4 pst ON pm.pm_status = pst.dic_value WHERE (pa.status = 1) ORDER BY PRL_NAME ASC
) z1
) z2
WHERE z2."zend_db_rownum" BETWEEN 1 AND 50
Schema doesn't appear to be a valid parameter accepted by Zend_Application_Resrouce_Db or Zend_Db_Adapter_Oracle. You can set the schema from your DbTable classes if you are using Zend_Db_Table_Abstract to define your DbTables.
See Example #4.
rsolved by overriding Zend_Db_Select (_join() for adding default schema) + overriding Zend_Db_Adapter (for returning own Select)

SQL::Abstract::Limit failing at OR logic

I'm trying to create an OR logic query using Class::DBI/Class::DBI::AbstractSearch. My code looks something like this:
my $results = Example::CDBI::Quote->search_where(
{ field_1 => {'like', $search_string},
field_2 => {'like', $search_string}},
{logic => 'or'}
);
According to the documentation this should work. It says that the information is passed to SQL::Abstract::Limit, which shows as taking the logic parameter. Unfortunately, MySQL shows the following in the query log (edited for brevity, and assuming a search of "123"):
SELECT * FROM quote WHERE ((field_1 LIKE '123' AND field_2 LIKE '123' ))
I have trying changing 'or' to 'OR' (silly, but worth a shot) which did not work. I also tried hunting down the logic in SQL::Abstract::Limit, but this operator is being passed to SQL::Abstract instead.
How do I get SQL::Abstract::Limit to accept OR logic from Class::DBI?
How Class::DBI calls SQL::Abstract::Limit
I was able to determine how SQL::Abstract::Limit is being constructed. I put values in instead of the variable names so it is easier to read.
my $sql = SQL::Abstract::Limit->new({'logic' => 'OR'});
my($phrase, #bind) = $sql->where(
{'field_1'=>{'like' => '123'},'field_2'=>{'like'=>'123'}},
undef, undef, undef);
You can apply OR locally like this:
use SQL::Abstract;
my $sql = SQL::Abstract->new;
my ($stmt, #bind) = $sql->where(
{ -or => [ { field_1 => { 'like', 'John' }},
{ field_2 => { 'like', 'John' }},
],
}, []);
gives in $stmt:
WHERE ( ( field_1 LIKE ? OR field_2 LIKE ? ) )
The logic property can be set in SQL::Abstract constructor, but I don't have idea how to propagate from Class::DBI.
Edit: I don't know if it is bug or feature, but it the operators changed by logic clause seems apply only when you define with arrayrefs. With hashrefs, you get always AND:
my $sql_and = SQL::Abstract::Limit->new(logic => 'AND');
my $sql_or = SQL::Abstract::Limit->new(logic => 'OR');
say $sql_and->where(['field_1'=>{'like' => '123'},'field_2'=>{'like'=>'123'}]);
# WHERE ( ( field_1 LIKE ? AND field_2 LIKE ? ) )
say $sql_or->where (['field_1'=>{'like' => '123'},'field_2'=>{'like'=>'123'}]);
# WHERE ( ( field_1 LIKE ? OR field_2 LIKE ? ) )
Or, to work with Class::DBI:
my $results = Example::CDBI::Quote->search_where(
[ field_1 => {'like', $search_string},
field_2 => {'like', $search_string}],
{logic => 'or'}
);