Need help with SQL join to a function - sql-server

I am trying to enter the following to an UPDATE trigger of QuoteItem:
UPDATE QuoteItem
SET List = StyleItem.List, CostFactor = StyleItem.CostFactor, Cost = NULL
FROM dbo.fn_GetQuoteItemListPrice(QuoteItem.ItemId, QuoteItem.RoomId)
AS StyleItem
CROSS JOIN (QuoteItem JOIN INSERTED ON
QuoteItem.QuoteItemId = INSERTED.QuoteItemId)
WHERE (INSERTED.List IS NULL) AND (INSERTED.ItemId IS NOT NULL)
I get the follwoing error:
Msg 4104, Level 16, State 1, Procedure QuoteItem_UPDATE, Line 6
The multi-part identifier "QuoteItem.ItemId" could not be bound.
Msg 4104, Level 16, State 1, Procedure QuoteItem_UPDATE, Line 6
The multi-part identifier "QuoteItem.RoomId" could not be bound.

UPDATE QuoteItem
SET List = StyleItem.List, CostFactor = StyleItem.CostFactor, Cost = NULL
FROM dbo.fn_GetQuoteItemListPrice(xx.ItemId, xx.RoomId)
AS StyleItem
CROSS JOIN (QuoteItem JOIN INSERTED ON
QuoteItem.QuoteItemId = INSERTED.QuoteItemId) xx
WHERE (INSERTED.List IS NULL) AND (INSERTED.ItemId IS NOT NULL)
Try the change I made above, assigning a table alias xx and referencing that instead..

Answer found here by Naomi Nosonovsky:
UPDATE QuoteItem
SET List = StyleItem.List, CostFactor = StyleItem.CostFactor, Cost = NULL
FROM QuoteItem JOIN INSERTED AS INSERTED ON QuoteItem.QuoteItemId =
INSERTED.QuoteItemId
CROSS APPLY dbo.fn_GetQuoteItemListPrice(QuoteItem.ItemId, QuoteItem.RoomId)
AS StyleItem
WHERE (INSERTED.List IS NULL) AND (INSERTED.ItemId IS NOT NULL)

Related

Update code shown here is not updating the items

What is wrong with my code?
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near 'UP'.
SQL server says missing something
UPDATE
(
SELECT
T.Br, U.Br AS Br1 ,
T.Dis, U.Dis AS Disc1 ,
T.DeletedDate , U.DeletedDate AS DeletedDate1 ,
T.INSERT_TS, U.INSERT_TS AS INSERT_TS1
FROM dbo.Matrix T
JOIN tlt_svs.Matrix_Update U
ON T.Key= U.Key)UP
SET
UP.Br = UP.Br1 ,
UP.Dis = UP.Dis1 ,
UP.DeletedDate = UP.DeletedDate1 ,
UP.INSERT_TS = UP.INSERT_TS1
Maybe you want an UPDATE with a join, i.e. update the values of matrix with the corresponding values of matrix_update?
UPDATE m
SET m.br = mu.br,
m.dis = mu.dis,
m.deleteddate = mu.deleteddate,
m.insert_ts = mu.insert_ts
FROM dbo.matrix m
INNER JOIN tlt_svs.matrix_update mu
ON mu.key = m.key;

SQL Stored Procedure using results of main query in a subquery

I've got a stored procedure made for my Crystal Reports. It does not work yet and I haven't got an idea on how to fix this.. This is my query:
use [fmsAir];
WITH Main AS (
SELECT [File].[FileNumber], [CostRevenue].[COSTAMOUNT], [CostRevenue].[RevenueAmount], [File].[FileDate], [CostRevenue].[REVENUERATE], [CostRevenue].[CostRate], [CostRevenue].[REVENUECURRENCY],
[File].[COMPANY], [CostRevenue].[CRNUMBER], [CostRevenue].[IINVOICENUMBER], [File].[RELATIONCODE], [CostRevenue].[COSTRELATIONCODE], [CostRevenue].[REVENUEDESCRIPTION]
FROM
[fmsAir].[dbo].[File]
INNER JOIN
[fmsAir].[dbo].[CostRevenue] ON [File].[FILENUMBER] = [CostRevenue].FILENUMBER
WHERE
[File].[FILEDATE] BETWEEN '2016-08-01 00:00:00' AND '2018-01-20 00:00:00' AND
[File].[FILENUMBER] > 500
),
Relation AS (
SELECT [Relation].[Companyname], [Relation].[RELATIONCODE]
FROM [fms].[dbo].[Relation]
WHERE [Relation].[Relationcode] = Main.[File].[RELATIONCODE]
),
OutgoingInvoiceLine AS (
SELECT [OutgoingInvoiceLine].[AMOUNT], [OutgoingInvoiceLine].[RATE], [OutgoingInvoiceLine].[Currency], [OutgoingInvoiceLine].[CRNUMBER]
FROM [fmsAir].[dbo].[OutgoingInvoiceLine]
WHERE [OutgoingInvoiceLine].[CRNUMBER] = Main.[CostRevenue].[CRNUMBER]
),
IncomingInvoiceLine AS (
SELECT [IncomingInvoiceLine].[AMOUNT], [IncomingInvoiceLine].[RATE], [IncomingInvoiceLine].[CURRENCY], [IncomingInvoiceLine].[CRNUMBER]
FROM [fmsAir].[dbo].[IncomingInvoiceLine]
WHERE [IncomingInvoiceLine].[CRNUMBER] = Main.[CostRevenue].[CRNUMBER]
),
RelationCost AS (
SELECT [Relation].[CompanyNAME], [Relation].[RelationCode]
FROM [fms].[dbo].[Relation]
WHERE Relation.RELATIONCODE = Main.[CostRevenue].[COSTRELATIONCODE]
)
SELECT main.FILENUMBER, main.COSTAMOUNT from Main
Now the part where i use WHERE and then the Main.. it does not work because that is not the way you can use it, but I do not know a way on how to do it the right way either. Can anybody help me make this work?
These are the errors:
Msg 4104, Level 16, State 1, Line 16
The multi-part identifier "Main.File.RELATIONCODE" could not be bound.
Msg 4104, Level 16, State 1, Line 21 The multi-part identifier
"Main.CostRevenue.CRNUMBER" could not be bound. Msg 4104, Level 16,
State 1, Line 26 The multi-part identifier "Main.CostRevenue.CRNUMBER"
could not be bound. Msg 4104, Level 16, State 1, Line 31 The
multi-part identifier "Main.CostRevenue.COSTRELATIONCODE" could not be
bound.
Thank you in advance!
Try to Create the column name in the cte expression and use that column name in the below mentioned cte.
I hope this will help
Why your are using 4 cte if you are only fetching the result from only first cte table.

Ambiguous column when update with remote server in SQL server?

I have 2 SQL servers .
dstest\mssql2008 <--I'm currently at this instance ( server)
dstrn <-- another server
Both servers has the same table called :
EServices_Pages_Content
Goal : I need to update data on dstest from dstrn
On the current server (dstest) I have :
I can(!) access dstrn from dstest :
SELECT * FROM dstrn.weberp.dbo.EServices_Pages_Content WHERE pageid=80
OKay.
So where is the problem ?
As I told , I need to update data on dstest(current db) from a far server (dstrn) so I do :
UPDATE EServices_Pages_Content
SET [Content] = a.Content
FROM [dstrn].weberp.dbo.EServices_Pages_Content a
WHERE PageID = a.pageID
AND MasterEntityID = a.masterEntityid
AND LanguageID = a.LanguageID
AND PageID = 80
But I get an error :
Msg 209, Level 16, State 1, Line 4
Ambiguous column name 'PageID'.
Msg 209, Level 16, State 1, Line 5
Ambiguous column name 'MasterEntityID'.
Msg 209, Level 16, State 1, Line 6
Ambiguous column name 'LanguageID'.
Msg 209, Level 16, State 1, Line 7
Ambiguous column name 'PageID'.
I don't understand , I did use aliases , why does it tell me Ambiguous columns? How can I fix it ?
Edit : I found a way to make it work( see my answer) - but still can't understand why I need full table prefix in the where clause. - and can't use aliases to prevent Ambiguity
Try this:
UPDATE Dest
SET [Content] = a.Content
FROM EServices_Pages_Content Dest
INNER JOIN [dstrn].weberp.dbo.EServices_Pages_Content a
ON Dest.PageID = a.pageID
AND Dest.MasterEntityID = a.masterEntityid
AND Dest.LanguageID = a.LanguageID
WHERE a.PageID = 80
Explanation:
Using an alias on just one of the tables is not enough.
Since both of the tables have the same names and the same column names,
you need to specify an alias to the destination table as well, and use a multi-part name for it's columns.
Also, you are using an implicit join. Since explicit joins more readable, I would recommend to never use implicit joins.
I have created 2 test tables ( on the same server) :
Table testA
Table testB
Now , for simplicity - let's say I want to update tableA from tableB but only only for pageId=2 :
So first I tried :
UPDATE testA a
SET a.[Content] = b.Content
FROM testB b
WHERE a.PageID = b.pageID
AND a.PageID = 2
But I got an error :
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'a'.
So then I tried :
UPDATE testA
SET [Content] = b.Content
FROM testB b
WHERE testA.PageID = b.pageID -- notice prefix
AND testA.PageID = 2 -- notice prefix
And Then :
(1 row(s) affected)
And now you can see it's ok :
You need some join here:
UPDATE d
SET [Content] = a.Content
FROM EServices_Pages_Content d
JOIN [dstrn].weberp.dbo.EServices_Pages_Content s ON d.PageID = s.pageID
AND d.MasterEntityID = s.masterEntityid
AND d.LanguageID = s.LanguageID
AND d.PageID = 80

type of column conflicts with the type of other columns specified in the UNPIVOT list

SELECT
RMS_Code, Property, Value
FROM
(SELECT
RMS_Code,
ITEM_CODE= CONVERT(VARCHAR(320),ITEM_CODE),
ITEM_DESC= CONVERT(VARCHAR(320),ITEM_DESC),
[SECOND_DESCRIPTION] = CONVERT(VARCHAR(320), [SECOND_DESCRIPTION]),
[IBC] = CONVERT(VARCHAR(320), [IBC]),
[ITEM_GROUP] = CONVERT(VARCHAR(320), [ITEM_GROUP]),
[Main_Item] = CONVERT(VARCHAR(320),[Main_Item]),
AGGREGATE_CODE = CONVERT(VARCHAR(320),AGGREGATE_CODE),
ITEM_TYPE = convert(varchar(320),ITEM_TYPE)
FROM
[dbo].[RMS_Master_11]) AS t
UNPIVOT
(
Value FOR Property IN ( ITEM_CODE,ITEM_DESC,
[SECOND_DESCRIPTION],
[IBC],ITEM_GROUP, Main_Item,AGGREGATE_CODE,ITEM_TYPE
)
) AS up;
The error message:
Msg 8167, Level 16, State 1, Line 22
The type of column "ITEM_TYPE" conflicts with the type of other columns specified in the UNPIVOT list.

MSSQL select query with IF () statement

I've MYSQL Query, its working fine
The QUERY Is:
SELECT * , IF(totexec >= totexecrun1, totexec-totexecrun1,0) AS rewrk,
SUM(tcu) tcunit ,
IF(totexec=0, ((SUM(tcu)/totexec)*100),0) AS proflevel
FROM mntest_schedule a
LEFT JOIN mnrelease_details b
ON b.tester=a.tester
AND a.project=b.project
LEFT JOIN tc_details c
ON b.tc_id=c.tc_name
AND a.project=c.project
WHERE a.rel_name='automanual_assign'
AND a.project='JupiterQA'
GROUP BY a.tester;
I tried to execute the same Query in MSSQL but its throwing error.
ERROR IS:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
Am I did anything wrong with this query?
SELECT * ,
CASE WHEN totexec >= totexecrun1 THEN totexec - totexecrun1
ELSE 0
END AS rewrk ,
SUM(tcu) tcunit ,
CASE WHEN totexec = 0 THEN ( SUM(tcu) / totexec ) * 100
ELSE 0
END AS proflevel
FROM mntest_schedule a
LEFT JOIN mnrelease_details b ON b.tester = a.tester
AND a.project = b.project
LEFT JOIN tc_details c ON b.tc_id = c.tc_name
AND a.project = c.project
WHERE a.rel_name = 'automanual_assign'
AND a.project = 'JupiterQA'
GROUP BY a.tester;
Use CASE instead of IF.
Refer this(Understanding Case Expression in SQL Server with Example) to learn CASE in SQL SERVER.
SELECT * ,
CASE WHEN (totexec >= totexecrun1)
THEN totexec-totexecrun1
ELSE 0 END AS rewrk,
SUM(tcu) tcunit ,
CASE WHEN (totexec=0)
THEN ((SUM(tcu)/totexec)*100)
ELSE 0 END AS proflevel
FROM mntest_schedule a LEFT JOIN mnrelease_details b
ON b.tester=a.tester AND a.project=b.project
LEFT JOIN tc_details c ON b.tc_id=c.tc_name AND a.project=c.project
WHERE a.rel_name='automanual_assign' AND a.project='JupiterQA'
GROUP BY a.tester;

Resources