Error in FROM clause: near '{'. Unable to parse query text - sql

I'm trying to execute the following query for a Dataset:
SELECT
S.CUSIP, S.SecName,
ISNULL(RA.FinInstCode, '') AS RemarketingAgent,
S.IssueDate, S.MaturityDate, F2.FinInstName AS Trustee,
CASE WHEN SC1.ExpirationDate IS NULL
THEN '' ELSE ISNULL(F1.FinInstCode, '') END
AS CreditProvider,
CASE WHEN SC1.ExpirationDate IS NULL
THEN '' ELSE ISNULL(C1.CreditEnhancementCode, '') END
AS CreditType,
SC1.ExpirationDate AS CreditExpirationDate,
SC1.RefNum AS RefNumber
FROM
dbo.SecurityCreditEnhancementProvider SC1,
dbo.FinInst F1,
dbo.LUCreditEnhancement C1,
{ oj { oj dbo.Security S
LEFT OUTER JOIN
dbo.FinInst F2 ON S.TrusteeID = F2.FinInstID }
LEFT OUTER JOIN
dbo.FinInst RA ON S.RemarketingAgentID = RA.FinInstID }
WHERE
S.SecID = SC1.SecID
AND
SC1.CreditProviderID = F1.FinInstID
AND
SC1.CreditEnhancementID = C1.CreditEnhancementID
AND
C1.CreditEnhancementCode = 'LOC' OR
C1.CreditEnhancementCode = 'LIQUIDITY'
AND
(S.ActiveFlag = 'A')
AND
(S.SecTypeID NOT IN (4, 7))
ORDER BY CreditExpirationDate
When trying to run the query the error I get is " Error in FROM clause near:
'{' . Unable to parse query. "
Any help to fix this issue would be most appreciated.
Thanks,
EDIT 1: This is what is looks like after query designer changes the code:
FROM { oj { oj { oj { oj { oj dbo.SecurityCreditEnhancementProvider SC1 LEFT OUTER JOIN
dbo.FinInst F1 ON SC1.CreditProviderID = F1.FinInstID } LEFT OUTER JOIN
dbo.LUCreditEnhancement C1 ON SC1.CreditEnhancementID = C1.CreditEnhancementID } LEFT OUTER JOIN
dbo.Security S ON S.SecID = SC1.SecID } LEFT OUTER JOIN
dbo.FinInst F2 ON S.TrusteeID = F2.FinInstID } LEFT OUTER JOIN
dbo.FinInst RA ON S.RemarketingAgentID = RA.FinInstID }
Thanks all for there help.

Formatting code in a case like this might be helpful. you had incorrect syntax in your FROM clause. I also updated the query to use JOIN Syntax on all tables instead of using commas between your tables.
SELECT S.CUSIP
, S.SecName
, ISNULL(RA.FinInstCode, '') AS RemarketingAgent
, S.IssueDate
, S.MaturityDate
, F2.FinInstName AS Trustee
, CASE WHEN SC1.ExpirationDate IS NULL THEN '' ELSE ISNULL(F1.FinInstCode, '') END AS CreditProvider
, CASE WHEN SC1.ExpirationDate IS NULL THEN '' ELSE ISNULL(C1.CreditEnhancementCode, '') END AS CreditType
, SC1.ExpirationDate AS CreditExpirationDate
, SC1.RefNum AS RefNumber
FROM dbo.SecurityCreditEnhancementProvider SC1
LEFT JOIN dbo.FinInst F1
ON SC1.CreditProviderID = F1.FinInstID
LEFT JOIN dbo.LUCreditEnhancement C1
ON SC1.CreditEnhancementID = C1.CreditEnhancementID
LEFT JOIN dbo.Security S
ON S.SecID = SC1.SecID
LEFT OUTER JOIN dbo.FinInst F2
ON S.TrusteeID = F2.FinInstID
LEFT OUTER JOIN dbo.FinInst RA
ON S.RemarketingAgentID = RA.FinInstID
WHERE (C1.CreditEnhancementCode = 'LOC' OR C1.CreditEnhancementCode = 'LIQUIDITY' )
AND (S.ActiveFlag = 'A')
AND (S.SecTypeID NOT IN (4, 7))
ORDER BY CreditExpirationDate

Related

EclipseLink/TopLink full outer join instead left join

On model Card can have or not CardHolder ( 1:1 ), and I would like getting every cards filter by issuer linked to actived cardHolders plus cards without cardHolders, so I need a full outer join. Although the query below translate to left join returning just cards with cardHolders
final ExpressionBuilder builder = new ExpressionBuilder( Card.class );
Expression queryExp = builder.get( "cardIssuer" ).equal( cardIssuer );
queryExp = queryExp.and( builder.get( "cardStatus" ).get( "statusType" ).equal( "ACTIVATED" ) );
queryExp = queryExp.and( builder.getAllowingNull( "cardHolder" )isNull().or(
builder.get( "cardHolder" ).get( "status" ).get( "status" ).equal( "ACTIVE" ) ) );
Expression orderExpression = builder.get( "cardHolder" ).get( "surname" ).descending();
return getMultiple( queryExp, pageable , Card.class, orderExpression );
Translate query is
SELECT COUNT(t0.CARD_ID) FROM CARD t0 LEFT JOIN CARD_HOLDER t3
ON (t3.CARD_HOLDER_ID = t0.CARD_HOLDER_ID), CARD_HOLDER_STATUS t2, CARD_STATUS t1
WHERE (((((t0.CARD_ISSUER_ID = 10006) AND (t1.STATUS_TYPE = 'ACTIVATED')) AND (t2.STATUS = 'ACTIVE'))
AND (t0.CARD_ID IN ('52683','52692')))
AND ((t1.CARD_STATUS_ID = t0.CARD_STATUS_ID) AND (t2.STATUS_ID = t3.STATUS_ID)))
Due to JPA version the outer join is not properly done, so I found a way through native queries
#NamedNativeQueries( {
#NamedNativeQuery( name = Card.USER_DIRECTORY_BASE,
query = "select * from card c full outer join card_holder ch on c.card_holder_id = ch.card_holder_id "
+ "where c.CARD_ISSUER_ID = ?1 and c.card_status_id = 1 and ( ch.STATUS_ID = 1 or c.CARD_HOLDER_ID is null) "
+ "order by ch.FORENAME asc",
resultClass = Card.class ),
#NamedNativeQuery( name = Card.USER_DIRECTORY_BASE_COUNT,
query = "select count(*) from card c full outer join card_holder ch on c.card_holder_id = ch.card_holder_id "
+ "where c.CARD_ISSUER_ID = ?1 and c.card_status_id = 1 and ( ch.STATUS_ID = 1 or c.CARD_HOLDER_ID is null) "
+ "order by ch.FORENAME asc" )
} )
And getting results
Query query = em.createNamedQuery( Card.USER_DIRECTORY_BASE);
query.setParamenter(1,10000);
query.getResultList();

convert sql subquery with multiple joins to linq

I'm having trouble working in my subselect into LINQ. Here is the SQL followed by the LINQ. The SQL Query runs fine. The problem is incorporating the subquery. Any assistance would be appreciated:
SELECT DISTINCT
cic.CommitmentItemCategoryName + '( ' + cicType.CommitmentItemCategoryTypeName + ' )' AS displayCategory
, 'CategoryType_' + CAST(cic.CommitmentItemCategoryID AS VARCHAR(10)) AS displayCategoryID
, ISNULL(vwPAD.DollarsAllocated, 0) AS DisplayDollarsAllocated
,cic.CommitmentItemCategoryID
FROM
tblCommitmentItemCategory cic
LEFT OUTER JOIN
tblCommitmentItemCategoryType cicType ON cic.CommitmentItemCategoryTypeID = cicType.CommitmentItemCategoryTypeID
LEFT OUTER JOIN
tblAccountDirectToCommitmentItemCategory adToCIC ON adToCIC.CommitmentItemCategoryID IN (SELECT CommitmentItemCategoryID FROM tblCommitmentItemCategory)
LEFT OUTER JOIN
vw_ParentAccountDollarsAllocatedByCommitmentItemCategory vwPAD ON vwPAD.FiscalYear = 2015
AND cic.CommitmentItemCategoryID = vwPAD.CommitmentItemCategoryID
AND vwPAD.AccountDirectParentID = 19
WHERE
adToCIC.AccountDirectParentID = 19
ORDER BY
displayCategory
var queryInner = from cic in MyContext.tblCommitmentItemCategory
select new
{
cic.CommitmentItemCategoryID
};
var queryDollars = (from cic in MyContext.tblCommitmentItemCategory
join cicType in MyContext.tblCommitmentItemCategoryType
on cic.CommitmentItemCategoryTypeID equals cicType.CommitmentItemCategoryTypeID
into t2
from cicType in t2.DefaultIfEmpty()
join adToCIC in MyContext.tblAccountDirectToCommitmentItemCategory
//What goes here?
on ...
// on cic.CommitmentItemCategoryID equals adToCIC.CommitmentItemCategoryID
into t3
from adToCIC in t3.DefaultIfEmpty()
join vw in MyContext.vw_ParentAccountDollarsAllocatedByCommitmentItemCategory
on new { FiscalYear = currentFiscalYear, CommitmentItemCategory = cic.CommitmentItemCategoryID, ParentAccountID = currentParentAccountID }
equals new { FiscalYear = vw.FiscalYear, CommitmentItemCategory = vw.CommitmentItemCategoryID, ParentAccountID = vw.AccountDirectParentID }
into t
from vw in t.DefaultIfEmpty()
where adToCIC.AccountDirectParentID == currentParentAccountID
let displayCategory = cic.CommitmentItemCategoryName + " ( " + cicType.CommitmentItemCategoryTypeName + " )"
// Called CategoryType but it's actually the ID below
let displayCategoryTypeID = "CategoryType_" + cic.CommitmentItemCategoryID.ToString()
//let displayCategoryTypeID = "CategoryType!" + cic.CommitmentItemCategoryID + "_" + adToCIC.AccountDirectToCommitmentItemCategoryID.ToString()
let displayDollarsAllocated = vw.DollarsAllocated == null ? 0 : vw.DollarsAllocated
orderby cic.CommitmentItemCategoryName
select new
{
displayCategory,
displayCategoryTypeID,
cic.CommitmentItemCategoryID,
displayDollarsAllocated
}).Distinct();
Can you convert queryInner to a join? Then use that result in the larger query:
var queryInner =
from atcic in MyContext.tblAccountDirectToCommitmentItemCategory
join cic in MyContext.tblCommitmentItemCategory on atcic.CommitmentItemCategoryID equals cic.CommitmentItemCategoryID
select atcic;
And in the big query:
join adToCIC in queryInner into t3
from adToCIC in t3.DefaultIfEmpty()

DRUPAL db_or adding AND between multiple variables

I have inherited an application that is using the drupal filtering system to find results. The user can select to search the 'campus_location' column but when they enter multiple variables, it adds an AND between the condition.
Here is the printed version of the query generated by drupal:
SELECT * FROM champs c INNER JOIN users u ON u.uid = c.uid LEFT OUTER JOIN champs_rsvp r
ON r.uid = c.uid LEFT OUTER JOIN champs_checklists cl
ON cl.id = c.checklist_id LEFT OUTER JOIN node en
ON en.uid = u.uid AND en.type = 'getting_started_event'
LEFT OUTER JOIN node sn ON sn.uid = u.uid
AND sn.type = 'success_story'
LEFT OUTER JOIN champs_action_steps cas
ON cas.uid = u.uid
WHERE ( (c.campus_location LIKE 'Flint' ESCAPE '\\') )
**AND**( (c.campus_location LIKE 'Ann Arbor' ESCAPE '\\') )
I need the last AND to be an OR so it looks like this:
SELECT * FROM champs c INNER JOIN users u ON u.uid = c.uid LEFT OUTER JOIN champs_rsvp r
ON r.uid = c.uid LEFT OUTER JOIN champs_checklists cl
ON cl.id = c.checklist_id LEFT OUTER JOIN node en
ON en.uid = u.uid AND en.type = 'getting_started_event'
LEFT OUTER JOIN node sn ON sn.uid = u.uid
AND sn.type = 'success_story'
LEFT OUTER JOIN champs_action_steps cas
ON cas.uid = u.uid
WHERE ( (c.campus_location LIKE 'Flint' ESCAPE '\\') )
OR( (c.campus_location LIKE 'Ann Arbor' ESCAPE '\\') )
Which produces the correct results.
Here is the code that I inherited where it looks over the words in the search box. I just can't seem to figure out where it is adding the variables where it is putting the AND.
$words = explode(",", $_SESSION['form_state']['values']['filter']);
foreach($words as $word)
{
$or = db_or();
foreach($cols as $col)
{
$table = 'c';
if(in_array($col, array('completed_date', 'score')))
$table = 'cl';
elseif(in_array($col, array('name', 'mail')))
$table = 'u';
elseif($col == 'date')
$table = 'r';
elseif(in_array($col, array('announce', 'num_success_stories', 'num_action_steps'))) //can't put these in WHERE clause. Need to go in HAVING.
continue;
echo $word;
$or = $or->condition($table . '.' . $col, champs_make_like_sql($word), 'LIKE');
}
$result = $query->condition($or);
}

INNER JOIN LEFT JOIN in LINQ to SQL

How to convert INNER JOIN and LEFT JOIN in the following SQL query to LINQ to SQL? Thanks!
SELECT transactions.postdate,
transactions.clientkey AS TransClientKey,
transactions.type AS TransType,
clients.clientno,
Isnull(clients.nostmt, 0) AS CliNoStmt,
Isnull(aging.nostmt, 0) AS AgeNoStmt,
pmtchecks.*
FROM ((pmtchecks
INNER JOIN transactions
ON pmtchecks.transkey = transactions.transkey)
INNER JOIN clients
ON transactions.clientkey = clients.clientkey)
LEFT JOIN aging
ON ( transactions.clientkey = aging.clientkey )
AND ( pmtchecks.debtorkey = aging.debtorkey )
WHERE ( pmtchecks.debtorkey = 36927 )
AND ( transactions.status = 0 )
AND ( transactions.postdate <= '31-May-2012' )
AND ( ( transactions.postdate >= '01-May-2012' )
OR ( clients.clientno = 'UNKNOWN' ) )
ORDER BY pmtchecks.checkdate,
pmtchecks.checkno
Hi this is kind of dummy code i cnt say its exactly right but the idea will be exactly same to get the result
var anonymousType= (from pm in pmtchecks
join tr in transactions
on pm.transkey equals tr.transkey //INNERJOIN
join cl in clients
on tr.clientKey equals cl.clientKey
join ag in aging
on pm.debtorkey equals ag.debtorKey into ljoin //Left Join
from lj in ljoin.DefaultOrEmpty()
where pm.debortkey==36927 && tr.status==0 && tr.postdate<="31-May-2012" && tr.postdate>="01-May-2012" //u will have to change this to date format first
Select new {PostDate=tr.postdate, TransClientKey=tr.clientkey,TransType=tr.type,ClientNo=cl.clientno,CliNoStmt=cl.nomst ?? 0,AgeNoStmt=ag.nomst ??0,Pmtchecks=pm } //Anonymous type from this you can extract the values and fill to your custom type
).OrderBy(o=>o.Pmtchecks.checkdate).OrderBy(o=>o.Pmtchecks.checkno).ToList();
Hope this will help.
LINQ Query Samples
EDITED
var pmtchecks = from p in urcontext.pmtchecks
join t in urcontext.transactions on p.transkey equals t.transkey
join a in urcontext.aging on t.clientkey equals a.clientkey into details
from d in details.Where( a => ( a.debtorkey == p.debtorkey)).DefaultIfEmpty()
where (p.debtorkey == 36927 && t.status == 0 && t.postdate <= '31-May-2012'
&& (t.postdate >= '01-May-2012' || c.clientno == 'UNKNOWN' ))
orderby p.checkdate, p.checkno
select new
{
t.postdate,
t.clientkey,
// TransClientKey = t.clientkey, //only works if TransClientKey is property
t.type ,
//TransTypet = t.type ,//property
c.clientno,
c.nostmt,
//CliNoStmt = c.nostmt ?? 0,//property
a.nostmt//,
//AgeNoStmt = nostmt ?? 0,//property
//p. ... //follow above for p columns
};

What will be the LINQ query for the below SQL Query

SELECT t_PersonalInformation.personalInformation_Name, t_PersonalInformation.personalInformation_PresentAddress,
t_Applicant.applicant_TotalExperience,
t_Experience.experience_CompanyName,
t_Experience.experience_Responsibilities,
t_Training.training_TitleDetails
FROM t_Applicant LEFT OUTER JOIN
t_PersonalInformation ON t_Applicant.applicant_user_ID = t_PersonalInformation.personalInformation_applicant_ID
LEFT OUTER JOIN
t_Experience ON t_Applicant.applicant_user_ID = t_Experience.experience_applicant_ID
LEFT OUTER JOIN
t_Training ON t_Applicant.applicant_user_ID = t_Training.training_applicant_ID
WHERE (t_Applicant.applicant_user_ID = 'hasib789')
I am working with C# with vs2008 for a asp.net application
It is depend on what mapping do you have.
For example it can be:
var result =
from a in DataContext.Applicant
join pi in DataContext.PersonalInformation on a.applicant_user_ID equals pi.personalInformation_applicant_ID
join e in DataContext.Experience on a.applicant_user_ID equals e.experience_applicant_ID
join t in DataContext.Training on a.applicant_user_ID equals t.training_applicant_ID
where a.applicant_user_ID == 'hasib789'
select new { personalInformation_Name = pi.personalInformation_Name, personalInformation_PresentAddress = pi.personalInformation_PresentAddress, applicant_TotalExperience = a.applicant_TotalExperience, experience_CompanyName = e.experience_CompanyName, experience_Responsibilities = e.experience_Responsibilities, training_TitleDetails = t.training_TitleDetails }