SQL Server view not working after edit in Management Studio - sql-server

I had a view that worked, I edited it in SQL Server Mgmt Studio, it didn't work, I took out the changes and saved it down, it still doesn't work despite reverting it back to its original state.
I'm getting the error
Joined tables cannot be specified in a query containing outer join
operators. View or function 'adept_invoiced' contains joined tables.
I've googled around and come up with a possible join syntax problem due to the view being written in 2001 and me trying to edit it (for the first time) in Management Studio.
The view ran perfectly for years until I edited it this morning.
This is the From/Where section....
FROM dbo.SYS_COMP_ADDRESS
CROSS JOIN dbo.SL_ACCOUNTS
INNER JOIN dbo.SL_TRANSACTIONS
INNER JOIN dbo.ORD_HEADER
ON dbo.SL_TRANSACTIONS.ST_ORDER_NUMBER =
dbo.ORD_HEADER.OH_ORDER_NUMBER
INNER JOIN dbo.SL_PL_NL_DETAIL
LEFT OUTER JOIN dbo.STK_STOCK
ON dbo.SL_PL_NL_DETAIL.DET_STOCK_CODE =
dbo.STK_STOCK.STKCODE
INNER JOIN dbo.ORD_DETAIL
ON dbo.SL_PL_NL_DETAIL.DET_ORDER_LINK =
dbo.ORD_DETAIL.OD_PRIMARY
LEFT OUTER JOIN dbo.STK_STOCK3
ON dbo.SL_PL_NL_DETAIL.DET_STOCK_CODE =
dbo.STK_STOCK3.STKCODE3
LEFT OUTER JOIN dbo.STK_STOCK_2
ON dbo.SL_PL_NL_DETAIL.DET_STOCK_CODE =
dbo.STK_STOCK_2.STKCODE2
LEFT OUTER JOIN dbo.PRC_PRICE_RECS2
ON dbo.SL_PL_NL_DETAIL.DET_PRICE_CODE =
dbo.PRC_PRICE_RECS2.PRCODE2
ON dbo.SL_TRANSACTIONS.ST_HEADER_KEY =
dbo.SL_PL_NL_DETAIL.DET_HEADER_KEY
ON dbo.SL_ACCOUNTS.CUCODE = dbo.SL_TRANSACTIONS.ST_COPYCUST
INNER JOIN dbo.SL_ACCOUNTS2
ON dbo.SL_ACCOUNTS.CUCODE = dbo.SL_ACCOUNTS2.CUCODE2
WHERE ( dbo.SL_TRANSACTIONS.ST_TRANTYPE IN ( 'INV', 'CRN' ) )
AND ( dbo.SL_TRANSACTIONS.ST_BATCH_FLAG <> 1 )
AND ( dbo.SL_ACCOUNTS.CUCODE <> '1023134' )
AND ( dbo.SL_ACCOUNTS.CUCODE <> '1023265' )
AND ( dbo.SL_ACCOUNTS.CUSORT NOT LIKE '%other income%' )
AND ( dbo.SL_ACCOUNTS.CUSORT NOT LIKE '%supplier%' )
I'm not sure where it might be failing, or how to fix it!
Any help would be greatly appreciated.

Try this:
FROM dbo.SYS_COMP_ADDRESS
CROSS JOIN dbo.SL_ACCOUNTS
INNER JOIN dbo.SL_TRANSACTIONS
INNER JOIN dbo.ORD_HEADER
ON dbo.SL_TRANSACTIONS.ST_ORDER_NUMBER =
dbo.ORD_HEADER.OH_ORDER_NUMBER
INNER JOIN dbo.SL_PL_NL_DETAIL
ON dbo.SL_TRANSACTIONS.ST_HEADER_KEY =
dbo.SL_PL_NL_DETAIL.DET_HEADER_KEY
LEFT OUTER JOIN dbo.STK_STOCK
ON dbo.SL_PL_NL_DETAIL.DET_STOCK_CODE =
dbo.STK_STOCK.STKCODE
INNER JOIN dbo.ORD_DETAIL
ON dbo.SL_PL_NL_DETAIL.DET_ORDER_LINK =
dbo.ORD_DETAIL.OD_PRIMARY
LEFT OUTER JOIN dbo.STK_STOCK3
ON dbo.SL_PL_NL_DETAIL.DET_STOCK_CODE =
dbo.STK_STOCK3.STKCODE3
LEFT OUTER JOIN dbo.STK_STOCK_2
ON dbo.SL_PL_NL_DETAIL.DET_STOCK_CODE =
dbo.STK_STOCK_2.STKCODE2
LEFT OUTER JOIN dbo.PRC_PRICE_RECS2
ON dbo.SL_PL_NL_DETAIL.DET_PRICE_CODE =
dbo.PRC_PRICE_RECS2.PRCODE2
ON dbo.SL_ACCOUNTS.CUCODE = dbo.SL_TRANSACTIONS.ST_COPYCUST
INNER JOIN dbo.SL_ACCOUNTS2
ON dbo.SL_ACCOUNTS.CUCODE = dbo.SL_ACCOUNTS2.CUCODE2
WHERE ( dbo.SL_TRANSACTIONS.ST_TRANTYPE IN ( 'INV', 'CRN' ) )
AND ( dbo.SL_TRANSACTIONS.ST_BATCH_FLAG <> 1 )
AND ( dbo.SL_ACCOUNTS.CUCODE <> '1023134' )
AND ( dbo.SL_ACCOUNTS.CUCODE <> '1023265' )
AND ( dbo.SL_ACCOUNTS.CUSORT NOT LIKE '%other income%' )
AND ( dbo.SL_ACCOUNTS.CUSORT NOT LIKE '%supplier%' )

Are you using the old style join syntax anywhere so search for *= and =* in your view definition

Related

SQLServer 2014 unable to achieve parallelism for query

I use SQLServer 2014 (120 compatibility mode) and I want a parallel execution plan for my query, but because of a few fields, it isn't. As long as field pi.Name AS ProcessName is commented I have parallelism but when it is active it isn't. The same problem is with other commented fields. Is the reason connected with an index which covers pi.ProcessGuid but not pi.Name or something more?
ExecutionPlan_Parallel
ExecutionPlan_Single
ExecutionPlan_Original
When I commented on those few fields ExecutionPlan starts to be parallel and from 30 minutes query runs only 11 seconds.
SELECT kg.Id,
ui.UserInfoGuid AS UserGuid,
ui.FullName AS UserName,
di.DeviceInfoGuid AS DeviceGuid,
di.ComputerFullName,
pi.ProcessGuid,
--pi.Name AS ProcessName,
kg.ProcessActivationGuid,
t.Caption,
whi.WebsiteHostGuid,
--whi.HostName,
wv.WebsiteGuid,
wv.[Url],
--cc.CategoryGuid,
--cc.[Name] AS CategoryName,
ca.ControlSequenceGuid,
ca.ControlActivationGuid/*
(SELECT ce.[Name] + '/' AS [text()]
FROM [raw].ControlElement AS ce
WHERE ce.ControlSequenceGuid = ca.ControlSequenceGuid
ORDER BY ce.SequenceNumber DESC
FOR xml path ('')) AS ControlTree*/
--,ac.ApplicationContextName
,kg.BeginDate AS KeystrokeBeginDate
,kg.EndDate AS KeystrokeEndDate
,kg.TotalKeyCount
,kg.KeyCount
,kg.FunctionalKeyCount
,kg.ClickCount
,kg.WheelCount
,kg.OtherKeyCount
,kg.TotalMilliseconds
,kg.ActiveMilliseconds
FROM raw.[Session] AS s
INNER JOIN raw.Monitoring AS m
ON ( m.SessionGuid = s.SessionGuid )
INNER JOIN stats.[KeystrokeGroupRcpView] AS kg
ON ( kg.MonitoringGuid = m.MonitoringGuid )
INNER JOIN dbo.UserInfo AS ui
ON ( ui.UserInfoGuid = kg.UserGuid )
INNER JOIN dbo.DeviceInfo AS di
ON ( di.DeviceInfoGuid = kg.DeviceGuid )
INNER JOIN dbo.ProcessInfo AS pi
ON ( pi.OrganizationGuid = di.OrganizationGuid
AND pi.ProcessGuid = kg.ProcessGuid )
INNER JOIN raw.Title AS t
ON ( t.TitleGuid = kg.TitleGuid )
LEFT OUTER JOIN [raw].ControlActivation AS ca
ON ca.ProcessActivationGuid = kg.ProcessActivationGuid
AND kg.BeginDate >= ca.BeginDate
AND kg.EndDate <= ca.EndDate
LEFT OUTER JOIN controls.ControlFocus AS cf
ON cf.ControlActivationGuid = ca.ControlActivationGuid
LEFT OUTER JOIN controls.ControlSequenceContext AS csc
ON csc.ControlSequenceGuid = ca.ControlSequenceGuid
LEFT OUTER JOIN controls.ApplicationContext AS ac
ON ac.ApplicationContextGuid = csc.ApplicationContextGuid
LEFT OUTER JOIN controls.ProcessControlCategory AS pcc
ON pcc.ProcessGuid = pi.ProcessGuid
AND pcc.ControlCategoryKeyGuid =
cf.ControlCategoryKeyGuid
LEFT OUTER JOIN controls.ControlCategory AS cc
ON cc.CategoryGuid = pcc.CategoryGuid
LEFT OUTER JOIN raw.WebsiteVisit AS wv
ON ( pi.IsWebBrowser = 1
AND wv.ProcessActivationGuid =
kg.ProcessActivationGuid
AND ( wv.BeginDate < kg.BeginDate
AND wv.EndDate >= kg.EndDate ) )
LEFT OUTER JOIN dbo.WebsiteHostInfo AS whi
ON ( whi.WebsiteHostGuid = wv.WebsiteHostGuid
AND whi.OrganizationGuid = di.OrganizationGuid )
WHERE kg.BeginDate > '2020-07-01' AND kg.BeginDate < '2020-07-02' and kg.UserGuid in ('A170565A-2D30-4911-5B9C-8525E2A2772B','4BE982BD-ADFC-6201-31C2-60A0BEF0D7F6','AE296576-87C6-EC2F-5A6F-E24ACE14456E')

TSQL Inner select using outer join

I have a query that is working for the most part until I had to add the inner select for "Trainers".
As you can see in the code below, I am trying to get all of the trainers for each of the segment ID's.
I am getting an error on the first inner selects where clause WHERE trn.segmentID = tes.teSegmentID saying that tes.teSegmentID is not defined.
Is there another way to approach this query in order to get the trainers like I am trying to accomplish?
SELECT *,
(SELECT e2.[FirstName] AS trainerFirst,
e2.[LastName] AS trainerLast
FROM BS_Training_Trainers AS trn
LEFT OUTER JOIN
employeeTable AS e2
ON trn.trainerEmpID = e2.EmpID
WHERE trn.segmentID = tes.teSegmentID
FOR XML PATH ('trainer'), TYPE, ELEMENTS, ROOT ('trainers'))
FROM dbo.BS_TrainingEvents AS a
WHERE a.trainingEventID IN (SELECT tes.trainingEventID
FROM dbo.BS_TrainingEvent_Segments AS tes
INNER JOIN
dbo.BS_TrainingEvent_SegmentDetails AS tesd
ON tesd.segmentID = tes.teSegmentID
INNER JOIN
dbo.BS_LocaleCodes AS locale
ON locale.localeID = tesd.localeID
WHERE locale.location = 'Baltimore');
It seems like you're taking the scenic route towards this:
SELECT a.*,
X.[FirstName],
X.[LastName]
FROM dbo.BS_TrainingEvents AS a
LEFT OUTER JOIN (SELECT e2.[FirstName], e2.[LastName], locale.location FROM dbo.BS_TrainingEvent_Segments AS tes
INNER JOIN dbo.BS_Training_Trainers AS trn ON trn.segmentID = tes.teSegmentID
INNER JOIN dbo.BS_TrainingEvent_SegmentDetails AS tesd ON tesd.segmentID = tes.teSegmentID
INNER JOIN dbo.BS_LocaleCodes AS locale ON locale.localeID = tesd.localeID
LEFT OUTER JOIN employeeTable AS e2 ON trn.trainerEmpID = e2.EmpID) AS X ON a.trainingEventID = X.trainingEventID
WHERE X.location = 'Baltimore';
Not sure if I got all those joins right, it was hard to decode from all the nesting you have going on.
If I have guessed table relationships from their names correctly, the only way to solve this is to reference the same filtering condition twice: first, in the XML generation part, and second in the outer level of the query:
with cte as (
select distinct tes.trainingEventID, tes.teSegmentID
from dbo.BS_TrainingEvent_Segments AS tes
INNER JOIN dbo.BS_TrainingEvent_SegmentDetails AS tesd ON tesd.segmentID = tes.teSegmentID
INNER JOIN dbo.BS_LocaleCodes AS locale ON locale.localeID = tesd.localeID
WHERE locale.location = 'Baltimore'
)
SELECT a.*, (
SELECT e2.[FirstName] AS trainerFirst, e2.[LastName] AS trainerLast
FROM BS_Training_Trainers AS trn
LEFT OUTER JOIN employeeTable AS e2 ON trn.trainerEmpID = e2.EmpID
inner join cte c on trn.segmentID = c.teSegmentID
FOR XML PATH ('trainer'), TYPE, ELEMENTS, ROOT ('trainers')
)
FROM dbo.BS_TrainingEvents AS a
where exists (select 0 from cte c where c.testrainingEventID = a.trainingEventID);
It's difficult to tell whether this is completely correct, of course, but I hope you get the idea.
Oh yes, and if you would have an event with multiple Baltimore segments, you will never be able to tell which trainer takes which one. But you can always add more data into XML to resolve this.

sql query assistance

I have a sql that is as under:
SELECT ib.branch_no,
ib.on_hand,
p.weightedav,
p.item_code,
FROM physical p
INNER JOIN
item_branch as ib on p.item_code = ib.item_code
WHERE ib.on_hand <> 0
This SQL returns only those branch_no that have on_hand <> 0.
I am trying to get all the branch_nos irrespective of the on_hand field, but while still using the where on_hand clause.
Taking the on_hand clause away solves my problem, but gives me large amount of un-needed rows with 0's.
I am using SQL SERVER 2008 R2.
Thanks in advance for any guidance. Please apologize if I am missing any information.
------------------------------------------ENTIRE SQL QUERY (Updated)---------------------------------------------
select
ib.branch_no,
p.weighted_av,
p.item_code,
p.x_value,
p.y_value,
ib.on_hand,
p.on_hand as PhysicalOH,
ip.price,
i.item_code as StyleCode,
i.description,
i.cat1,
i.cat2,
i.cat3,
i.cat4,
np.is_style_yn,
si.supplier_code ,
ysv.sort as YSort
from physical as p
left outer JOIN
item_branch as ib on p.item_code = ib.item_code -- and ib.on_hand <> 0
INNER JOIN
item_price as ip on p.item_code = ip.item_code and ip.price_type = 'P1'
INNER JOIN
style_values as sv on p.style_code = sv.style_code and p.x_value = sv.value
INNER JOIN
style_values as ysv on p.style_code = ysv.style_code and p.y_value = ysv.value and ysv.axis = 'Y'
INNER JOIN
ITEM as i on p.style_code = i.item_code
INNER JOIN
NON_PHYSICAL as np ON i.item_code = np.item_code and np.is_style_yn = 1
INNER JOIN
supplier_item as si ON i.item_code = si.item_code and si.pref_supp_no = 1
where --ib.on_hand <> 0 and
sv.axis = 'X' and
i.item_code in
(SELECT ITEM.item_code
FROM ITEM
INNER JOIN
NON_PHYSICAL ON ITEM.item_code = NON_PHYSICAL.item_code
LEFT JOIN
supplier_item ON Item.item_code = supplier_item.item_code and pref_supp_no = 1
WHERE NON_PHYSICAL.is_style_yn = 1 and ITEM.cat1 = 'Verge Sportswear Ltd' )
order by
si.supplier_code,
i.cat4,
i.cat3,
i.cat2,
i.cat1,
sv.sort
Try this:
SELECT ib.branch_no,
ib.on_hand,
p.weightedav,
p.item_code,
FROM physical p
INNER JOIN
item_branch as ib on (p.item_code = ib.item_code AND ib.on_hand <> 0)

Convert right outer join to Linq query in EF

I want to convert below SQL query to linq query but I confused:
SELECT *
FROM dbo.Vahed
INNER JOIN dbo.VahedMahsol ON dbo.Vahed.VahedId = dbo.VahedMahsol.VahedId
RIGHT OUTER JOIN dbo.Mahsol ON dbo.VahedMahsol.MahsolId = dbo.Mahsol.MahsolId
I wrote this code:
vaheds =
(
from i in db.spGetVahedByWhatWhere(what, wherestr, int.Parse(whattype), new Guid(shahrid))
join gr in db.GorohSenfis on i.GorohSenfiId equals gr.GorohSenfiID
join ct in db.Contacts on i.VahedId equals ct.VahedId
join vm in db.VahedMahsols on i.VahedId equals vm.VahedId
select i
)
.ToList();
but i don't know how to convert
RIGHT OUTER JOIN dbo.Mahsol ON dbo.VahedMahsol.MahsolId = dbo.Mahsol.MahsolId
to linq query.
First of all you can rewrite your SQL query to use a LEFT OUTER JOIN instead of a RIGHT OUTER JOIN (because this is easier to convert to LINQ):
SELECT *
FROM dbo.Mahsol
LEFT OUTER JOIN dbo.VahedMahsol ON dbo.Mahsol.MahsolId = dbo.VahedMahsol.MahsolId
INNER JOIN dbo.Vahed ON dbo.VahedMahsol.VahedId = dbo.Vahed.VahedId
Now you can convert the query above in LINQ like this:
vaheds = (
from m in db.Mahsols
join vm1 in db.VahedMahsols on m.MahsolId equals v.MahsolId into vmgroup
from vm in vmgroup.DefaultIfEmpty()
join i in db.spGetVahedByWhatWhere(what, wherestr, int.Parse(whattype), new Guid(shahrid))
on vm.VahedId equals i.VahedId
join gr in db.GorohSenfis on i.GorohSenfiId equals gr.GorohSenfiID
join ct in db.Contacts on i.VahedId equals ct.VahedId
select i
).ToList();
I have no way of testing this, but I hope it works.

JOIN codition in SQL Server

After applying join condition on two tables I want records which is maximum among records of left table
My query
SELECT a1.*,
t.*,
( a1.trnratefrom - t.trnratefrom )AS minrate,
( a1.trnrateto - t.trnrateto ) AS maxrate
FROM (SELECT a.srno,
trndate,
b.trnsrno,
Upper(Rtrim(Ltrim(b.trnstate))) AS trnstate,
Upper(Rtrim(Ltrim(b.trnarea))) AS trnarea,
Upper(Rtrim(Ltrim(b.trnquality))) AS trnquality,
Upper(Rtrim(Ltrim(b.trnlength))) AS trnlength,
Upper(Rtrim(Ltrim(b.trnunit))) AS trnunit,
b.trnratefrom,
b.trnrateto,
a.remark,
entdate
FROM mstprodrates a
INNER JOIN trnprodrates b
ON a.srno = b.srno)a1
INNER JOIN (SELECT c.srno,
trndate,
d.trnsrno,
Upper(Rtrim(Ltrim(d.trnstate))) AS trnstate,
Upper(Rtrim(Ltrim(d.trnarea))) AS trnarea,
Upper(Rtrim(Ltrim(d.trnquality))) AS trnquality,
Upper(Rtrim(Ltrim(d.trnlength))) AS trnlength,
Upper(Rtrim(Ltrim(d.trnunit))) AS trnunit,
d.trnratefrom,
d.trnrateto,
c.remark,
entdate
FROM mstprodrates c
INNER JOIN trnprodrates d
ON c.srno = d.srno) AS t
ON a1.trnstate = t.trnstate
AND a1.trnquality = t.trnquality
AND a1.trnunit = t.trnunit
AND a1.trnlength = t.trnlength
AND a1.trnarea = t.trnarea
AND a1.remark = t.remark
WHERE t.srno = (SELECT MAX(srno)
FROM a1
WHERE srno < a1.srno)
If you mean to say,
you want Records exist in Left table but not in right then use LEFT OUTER JOIN.

Resources