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.
Related
I am working in SQL Server 2016 and trying to update the element value in my XML column for numerous records and getting a syntax error but can't figure out where the problem is. I've googled and tried a number of fixes but to no avail. Any help/direction would be appreciated. Thanks. Here are the details:
Here is my XML:
<IW>
<form name="INSTALLMENT LATE BILLING" version="2" blkflg="0">
<MonthlyPayment>260.34</MonthlyPayment>
<BillingFee>7.91</BillingFee>
<LateFee>13.00</LateFee>
<BillDate>12/23/2021</BillDate>
<DueDate>12/23/2021</DueDate>
<LateDate>12/24/2021</LateDate>
<CancelDate>01/04/2022</CancelDate>
</form>
</IW>
Here is my SQL code:
UPDATE dt
SET dt.DocumentData.modify('replace value of (/IW/form/CancelDate/text())[1] with "01/09/2022"')
, dt.CancelDate = '2022-01-09'
--SELECT dt.CancelDate AS DocTransCancellationDate
-- , p.CancelDate AS PolicyCancellationDate
-- , p.PolicyNo
-- , dt.DocumentData
-- , dt.*
FROM dbo.AUT_DocumentTransaction dt
JOIN dbo.AUT_Policy p ON dt.PolicySysID = p.PolicySysID
WHERE dt.DocumentTransactionSysID IN (45601342
,45601349
,45601354
,45601356
,45601357
,45601359
,45601360
,45601363
,45601364
,45601365
,45601366)
Here is the error I'm getting:
Msg 102, Level 15, State 1, Line 377
Incorrect syntax near 'modify'.
I want to use the sum function in the ELSE part of the CASE Statement. I need to give the SUM function conditions but I'm unable to do so.
I tried to use the GROUP BY Function within the sum function but it doesn't work.
CASE WHEN SectionCrossList = NULL THEN InstructorCredits ELSE InstructorCredits/Sum(Instructorcredits) GROUP BY (AcademicYear,SectionCrossList) END NumCreditSplit
Msg 156, Level 15, State 1, Line 220 Incorrect syntax near the keyword 'GROUP'.
Msg 102, Level 15, State 1, Line 221 Incorrect syntax near 'NumCreditSplit'.
You should use CTE or window function sum
SELECT AcademicYear,SectionCrossList,
CASE
WHEN SectionCrossList = NULL
THEN
InstructorCredits
ELSE
InstructorCredits/Sum(Instructorcredits)
end NumCreditSplit
FROM <yourTable>
group by AcademicYear,SectionCrossList
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;
These are the questions:
Write a script that will give you six columns of output.
a) the customer first name
b) the customer last name
c) the order number
d) the item name
e) the discount
f) a column with the following words for the discount----either NONE, LESS THAN TWENTY, LESS THAN FORTY, or MORE THAN FORTY
I have done this:
SELECT
Customers.FirstName,
Customers.LastName,
Orders.OrderID,
Products.ProductName,
OrderItems.DiscountAmount,
IF(OrderItems.DiscountAmount = 0, 'NONE', IF(OrderItems.DiscountAmount <= 20, 'LESS THAN TWENTY', IF(OrderItems.DiscountAmount <= 40, 'LESS THAN FORTY', 'MORE THAN FORTY'))) AS discount
FROM
Customers
INNER JOIN
Orders ON (Customers.ShippingAddressID = Orders.ShipAddressID
AND Customers.BillingAddressID = Orders.BillingAddressID)
INNER JOIN
OrderItems ON (Orders.OrderID = OrderItems.OrderID)
INNER JOIN
Products ON (OrderItems.ProductID = Products.ProductID)
It gives me an error on my if statement? Can you see what I'm doing wrong?
This is the database I'm using:
Error messages:
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Your database diagram and errors suggest you are using Microsoft SQL Server, not MySQL. The IF command/expression/keyword is not standardized and implemented differently in different platforms.
Therefore you should not use MySQL proprietary IF-syntax but the MSSQL proprietary IIF-syntax. IF is a control-of-flow statement in MSSQL.
This is SQL Server. There is no if statement. Use case:
(CASE WHEN OrderItems.DiscountAmount = 0 THEN 'NONE'
WHEN OrderItems.DiscountAmount <= 20 THEN 'LESS THAN TWENTY'
WHEN OrderItems.DiscountAmount <= 40 THEN 'LESS THAN FORTY'
ELSE 'MORE THAN FORTY'
END) AS discount
Use "case..when" statement here. Usage link here: case statement
Case
when OrderItems.DiscountAmount = 0 then 'NONE'
when OrderItems.DiscountAmount <= 20 then 'LESS THAN TWENTY'
when OrderItems.DiscountAmount <= 40 then 'LESS THAN FORTY'
else 'MORE THAN FORTY'
End AS discount
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