SQL Syntax error (only when run as scheduled job) - sql-server

I have just created a scheduled job to run daily, but when the job runs via the scheduler I get the error "Incorrect syntax near 'Item'. [SQLSTATE 42000] (Error 102)".
This only happens when the job is run via the scheduler, and works fine when run directly.
DELETE FROM [NAVQueriesDB].[dbo].[New Items on Order] GO
SET ANSI_WARNINGS OFF
GO
INSERT [NAVQueriesDB].[dbo].[New Items on Order] (
[Updated Date],
[Location Code],
[Order No_],
[Item No_],
[Item Description],
[Variant Code],
[Description 2],
[Outstanding Qty_],
[Order Date],
[Vendor_No],
[Vendor_Name])
SELECT
GETDATE() AS "Updated Date",
[Purch Line].[Location Code],
[Purch Line].[Document No_],
[Item].[No_],
[Item].[Description],
ISNULL([Purch Line].[Variant Code],'-') AS "Variant Code",
ISNULL([Variant].[Description 2],'-') AS "Description 2",
CAST(SUM([Purch Line].[Outstanding Quantity]) as decimal(18,0)) AS "Outstanding Qty.",
[Purch Line].[Order Date],
[Purch Line].[Buy-from Vendor No_],
[Vendor].[Name]
FROM [Wings$Item] AS "Item"
LEFT JOIN [Wings$Purchase Line] AS "Purch Line"
ON [Item].[No_] = [Purch Line].[No_]
LEFT JOIN [Wings$Item Variant] AS "Variant"
ON [Item].[No_] = [Variant].[Item No_] AND [Purch Line].[Variant Code] = [Variant].[Code]
LEFT JOIN [Wings$Vendor] AS "Vendor"
ON [Purch Line].[Buy-from Vendor No_] = [Vendor].[No_]
LEFT JOIN [Wings$Item Ledger Entry] AS "Item Ledger"
ON [Item].[No_] = [Item Ledger].[Item No_]
WHERE
[Item].[No_] > '5618%'
AND [Item].[No_] <> 'Z999999'
AND [Item Ledger].[Item No_] IS NULL
GROUP BY
[Purch Line].[Location Code],
[Purch Line].[Document No_],
[Item].[No_],
[Item].[Description],
[Purch Line].[Variant Code],
[Variant].[Description 2],
[Purch Line].[Outstanding Quantity],
[Purch Line].[Order Date],
[Purch Line].[Buy-from Vendor No_],
[Vendor].[Name]
ORDER BY
[Location Code],
[Item].[No_],
[Item].[Description],
[Purch Line].[Variant Code]

It's a bad idea (IMO) to use double quotes for your identifiers. The standard is [] if it has to be quoted (which is also why I avoid spaces in my names so that I don't have to quote them).
SQL Agent defaults to QUOTED_IDENTIFIER being set to OFF. If you change the double quotes to brackets then this should work. Alternatively (but not recommended), you could include the first line of your script to be SET QUOTED_IDENTIFIER ON.
For example, GETDATE() AS "Updated Date"... GETDATE() AS [Updated Date] is better. GETDATE() AS updated_date is best.

Related

SSRS Highlight Duplicate Values in Column Across Entire Report

I have an SSRS report that looks at products used in a bill of materials. It lists the items involved, the bill of materials (BOM) they belong to and other various information. I would like to highlight ANY duplicates in the item column without having to sort by item.
Example data:
Level
Item No.
BOM No.
Quantity Per
1
001468
017998
4
1
001850
017998
6
1
017663
017998
2
1
017792
017998
2
2
001468
017663
3
So in the above data, the item # "001468" should get highlighted even though the information in the other columns is different per row.
I am aware of the "Previous" function but I don't want to re-order the item number column so I don't think using that is an option
The report sorts by level so that it first lists all the items in the top level (level 1) followed by the lower levels (2, 3, 4 etc...)
There can be multiple duplicates (not just 2), so any solution will have to factor that in
Would like the duplicate(s) too be highlighted in Bold Red
If more information is needed please let me know.
Edit: More Details added below 08/05/2022
Here is the code I am working with which begins with a recursive CTE:
WITH CTERBOM AS
(
SELECT PBL.[Production BOM No_], PBL.[Version Code], PBL.[No_], PBL.[Quantity per], 1 AS LVL
FROM [DataBaseName$Production BOM Line] PBL
LEFT JOIN [DataBaseName$Production BOM Header Extra] PBHEX ON PBHEX.[No_] = PBL.[Production BOM No_]
WHERE PBL.[Production BOM No_] = '008722' AND PBHEX.[Active Version No_] = PBL.[Version Code]
UNION ALL
SELECT PBL2.[Production BOM No_], PBL2.[Version Code], PBL2.[No_], PBL2.[Quantity per], LVL + 1
FROM [DataBaseName$Production BOM Line] PBL2
INNER JOIN CTERBOM ON CTERBOM.[No_] = PBL2.[Production BOM No_]
)
SELECT
CTERBOM.[LVL] AS [Level],
CTERBOM.[No_] AS [Item No.],
CTERBOM.[Production BOM No_] AS [Production BOM No.],
CTERBOM.[Quantity per] AS [Qty. Per]
FROM CTERBOM
LEFT JOIN [DataBaseName$Production BOM Header Extra] PBHEX ON PBHEX.[No_] = CTERBOM.[Production BOM No_]
GROUP BY
CTERBOM.[LVL],
CTERBOM.[Production BOM No_],
CTERBOM.[No_],
CTERBOM.[Quantity per],
ORDER BY
CTERBOM.[LVL],
CTERBOM.[Production BOM No_],
CTERBOM.[No_]
One way to do this would be to do add a new column to the query, as duplicates = count(*) over (partition by [Item No.]) (insert your actual column name).
Then use that to conditionally highlight the Item No field in the report if the value of the duplicates column is > 1. You would do this by going to the Item No field in the report > properties > fill > fill color > press the expression button and enter something like:
=IIf(Fields!duplicates.Value > 1,"Yellow","Default")
Add this column to your final select:
-- APPROACH 1:
SELECT
CTERBOM.[LVL] AS [Level],
CTERBOM.[No_] AS [Item No.],
count(*) over (partition by CTERBOM.[No_]) as duplicates,
/* ... */
/* APPROACH 2:
if you can't use approach 1 because of other code not currently
shown in the question, you can wrap the entire select in
another select, which will always work:
*/
WITH CTERBOM AS
(
SELECT PBL.[Production BOM No_], PBL.[Version Code], PBL.[No_], PBL.[Quantity per], 1 AS LVL
FROM [DataBaseName$Production BOM Line] PBL
LEFT JOIN [DataBaseName$Production BOM Header Extra] PBHEX ON PBHEX.[No_] = PBL.[Production BOM No_]
WHERE PBL.[Production BOM No_] = '008722' AND PBHEX.[Active Version No_] = PBL.[Version Code]
UNION ALL
SELECT PBL2.[Production BOM No_], PBL2.[Version Code], PBL2.[No_], PBL2.[Quantity per], LVL + 1
FROM [DataBaseName$Production BOM Line] PBL2
INNER JOIN CTERBOM ON CTERBOM.[No_] = PBL2.[Production BOM No_]
)
select level,
[Item No.],
count(*) over (partition by [Item No.]) as duplicates,
[Production BOM No.],
[Qty. Per]
from (
SELECT
CTERBOM.[LVL] AS [Level],
CTERBOM.[No_] AS [Item No.],
CTERBOM.[Production BOM No_] AS [Production BOM No.],
CTERBOM.[Quantity per] AS [Qty. Per]
/* rest of existing query ... */
) t;

I want to convert inner join Query in SubQuery

I am using inner join in query but I want Subquery instead of join
select [Document No_] as DocumentNo,
ledger.[Posting Date] as Date,
[Sales (LCY)] as Amount,
header.[Customer Reference] as PONo
from [Uneek Clothing Company Ltd$Cust_ Ledger Entry] ledger
inner join [Uneek Clothing Company Ltd$Sales Invoice Header] header on ledger.[Entry No_]=header.[Cust_ Ledger Entry No_]
where [Customer No_] = 'DRC01'
and [Document Type]=2
and [Sales (LCY)]!=0
and ledger.[Posting Date] between '01/01/2019' and '12/31/2019'
order by [Sales (LCY)] asc offset 0 ROWS FETCH NEXT 20 ROWS ONLY
[Entry No_] is foreign key in [Uneek Clothing Company Ltd$Sales Invoice Header] with name [Cust_ Ledger Entry No_]
if you really want to drop the join and use a subquery instead,
it will look something like this.
Since you did not use an table alias for all columns, I had to assume that no other column of table in the subquery is used anywhere else. If that is not the case then this will not work
It will also only work if the subquery returns no more then 1 row, otherwise a subquery cannot be used at all
select [Document No_] as DocumentNo,
ledger.[Posting Date] as Date,
[Sales (LCY)] as Amount,
( select header.[Customer Reference]
from [Uneek Clothing Company Ltd$Sales Invoice Header] header
where ledger.[Entry No_] = header.[Cust_ Ledger Entry No_]
) as PONo
from [Uneek Clothing Company Ltd$Cust_ Ledger Entry] ledger
where [Customer No_] = 'DRC01'
and [Document Type]=2
and [Sales (LCY)]!=0
and ledger.[Posting Date] between '01/01/2019' and '12/31/2019'
order by [Sales (LCY)] asc
offset 0 ROWS FETCH NEXT 20 ROWS ONLY

Scheduled datetime conversion error (SQL)

When trying to convert a datetime field to get rid of NULL's I am getting the error that the datetime cannot be converted.
This query is populating another database where the column datatype is date.
This job runs perfectly fine when not scheduled, but results in an error during the scheduled insert job.
Any ideas?
, (SELECT
CASE
WHEN MIN([Discount Ledger].[Posting Date]) IS NOT NULL
THEN CONVERT(VARCHAR(50), MIN([Discount Ledger].[Posting Date]), 121)
ELSE '-'
END
FROM
[Wings$Discount Ledger Entry] AS "Discount Ledger"
LEFT JOIN
[Wings$Periodic Discount] AS "MixMatchHeader" ON [Discount Ledger].[Offer No_] = [MixMatchHeader].[No_]
LEFT JOIN
[Wings$Periodic Discount Line] AS "MixMatch" ON [MixMatchHeader].[No_] = [MixMatch].[Offer No_]
LEFT JOIN
[Wings$Item_Special Group Link] AS "Special Group" ON [MixMatch].[No_] = [Special Group].[Special Group Code]
LEFT JOIN
[Wings$Store Price Group] AS "Store Price Group" ON [MixMatch].[Price Group] = [Store Price Group].[Price Group Code]
WHERE
[MixMatch].[No_] = [Item Ledger].[Item No_]
AND [MixMatch].[Variant Code] = [Item Ledger].[Variant Code]
AND [Store Price Group].[Store] = [Item Ledger].[Location Code]) AS "Discount Start Date"
You can do this using ISNULL. Since you are looking for the MIN date, I will use GETDATE() as the argument/replacement value for NULL. Unless your data has future dates, then GETDATE() should function as the MAX value. Unless you want the CASE statement to evaluate the NULL values as the MIN. In that case, you can use an empty string '' or '1900-01-01' instead of GETDATE().
, (SELECT
CASE
WHEN
MIN([Discount Ledger].[Posting Date]) IS NOT NULL
THEN CONVERT(varchar(50),MIN(ISNULL([Discount Ledger].[Posting Date],GETDATE())),121)
ELSE '-'
END
FROM [Wings$Discount Ledger Entry] AS "Discount Ledger"
LEFT JOIN [Wings$Periodic Discount] AS "MixMatchHeader" ON [Discount Ledger].[Offer No_] = [MixMatchHeader].[No_]
LEFT JOIN [Wings$Periodic Discount Line] AS "MixMatch" ON [MixMatchHeader].[No_] = [MixMatch].[Offer No_]
LEFT JOIN [Wings$Item_Special Group Link] AS "Special Group" ON [MixMatch].[No_] = [Special Group].[Special Group Code]
LEFT JOIN [Wings$Store Price Group] AS "Store Price Group" ON [MixMatch].[Price Group] = [Store Price Group].[Price Group Code]
WHERE [MixMatch].[No_] = [Item Ledger].[Item No_] AND [MixMatch].[Variant Code] = [Item Ledger].[Variant Code] AND [Store Price Group].[Store] = [Item Ledger].[Location Code])
AS "Discount Start Date"

SQL Server Incorrect Syntax error

Thank for your help and reading, I have the below query and I don't understand why this error message occurs:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'Price'
I am able to query this data column (Wholesale Price) in a standalone query. I'm using SQL Server 2005 Management Studio .
SELECT
ILE.[Location Code],
ILE.SUM(Quantity) AS "Transfer Qty",
PP.SUM(Wholesale Price) AS "Transfer Cost (HK)"
FROM
[DB].[dbo].[Company$Item Ledger Entry] ILE
INNER JOIN
[DB].[dbo].[Company$Purchase Price] PP ON ILE.[Item No_] = PP.[Item No_]
AND ILE.[Variant Code] = PP.[Variant Code]
WHERE
ILE.[Entry Type] = '4'
AND ILE.[Location Code] NOT LIKE '%STAFF%'
AND ILE.[Location Code] NOT LIKE 'WHSPACKAGE'
AND ILE.[Location Code] NOT LIKE 'WHS'
GROUP BY
ILE.[Location Code], ILE.[Quantity], PP.[Wholesale Price]
ORDER BY
[Location Code]
Thanks!
Regards,
Patrick
In SQL, you have to escape names that contain special characters -- and spaces are special characters. Because reading and writing code that has lots of square braces is cumbersome, the general advice is to avoid using such names.
In your case, you are missing the square braces:
SELECT ILE.[Location Code], ILE.Sum(Quantity) as "Transfer Qty",
Sum(PP.[Wholesale Price]) as "Transfer Cost (HK)"
FROM [DB].[dbo].[Company$Item Ledger Entry] ILE INNER JOIN
[DB].[dbo].[Company$Purchase Price] PP
ON ILE.[Item No_] = PP.[Item No_] AND
ILE.[Variant Code] = PP.[Variant Code]
Where ILE.[Entry Type] = '4' AND
ILE.[Location Code] NOT LIKE '%STAFF%' AND
ILE.[Location Code] NOT LIKE 'WHSPACKAGE' AND
ILE.[Location Code] NOT LIKE 'WHS'
Group by ILE.[Location Code], ILE.[Quantity]
Order by [Location Code];
In addition:
PP.SUM() doesn't make sense. The table alias goes with the column name.
Putting the wholesale price in the GROUP BY doesn't make sense. You want to aggregate the value, so it usually wouldn't go there.
Check this select statement for your error
SELECT
ILE.[Location Code],
ILE.SUM(Quantity) AS "Transfer Qty",
PP.SUM([Wholesale Price]) AS "Transfer Cost (HK)"--Error line
Thank you so much! Gordon!
Both SUM statement should be wording this format,
Sum(ILE.[Quantity] and Sum(PP.[Wholesale Price])
SELECT ILE.[Location Code], Sum(ILE.[Quantity]) as "Transfer Qty",
Sum(PP.[Wholesale Price]) as "Transfer Cost (HK)"
FROM [Dummy-28-Oct-2016].[dbo].[TEST ENV$Item Ledger Entry] ILE
INNER JOIN [Dummy-28-Oct-2016].[dbo].[TEST ENV$Purchase Price] PP
ON ILE.[Item No_] = PP.[Item No_]
AND ILE.[Variant Code] = PP.[Variant Code]
Where ILE.[Entry Type] = '4'
AND ILE.[Location Code] NOT LIKE '%STAFF%'
AND ILE.[Location Code] NOT LIKE 'WHSPACKAGE'
AND ILE.[Location Code] NOT LIKE 'WHS'
Group by ILE.[Location Code], ILE.[Quantity]
Order by [Location Code];

Did not receive expected results when using LEFT JOIN?

I have the following SQL statement, and it returns only 2 records even though I have 300 employees. Does anyone see what I could be doing wrong?
SELECT Employees.[Employee ID],
Employees.Employee,
Employees.[First Name],
Employees.[Middle Name],
Employees.[Last Name],
Employees.Position,
[Work].[Work ID]
FROM Employees LEFT JOIN
[Work] ON Employees.[Employee ID] = [Work].[Employee ID]
WHERE [Work].[Work Date] = '06-13-2012'
When using outer join filter on outer table must be applied in ON clause, otherwise you effectively get inner join:
SELECT
Employees.[Employee ID],
Employees.Employee,
Employees.[First Name],
Employees.[Middle Name],
Employees.[Last Name],
Employees.Position,
[Work].[Work ID]
FROM Employees
LEFT JOIN [Work]
ON Employees.[Employee ID] = [Work].[Employee ID]
AND [Work].[Work Date] = '06-13-2012'
If you want all employees, perhaps you are looking for something like:
SELECT Employees.[Employee ID], ...
FROM Employees LEFT JOIN
[Work] ON Employees.[Employee ID] = [Work].[Employee ID]
AND [Work].[Work Date] = '20120613'
Also note that you should use an unambiguous date format, e.g. yyyyMMdd
Have you checked the date in the Where clause?
Try converting into a datetime or check if you don't have hours/minutes/seconds defined in the [Work].[Work Date] column data.
Try removing the join to see where is the problem:
SELECT [Work].[Employee ID], [Work].[Work ID]
FROM [Work]
WHERE [Work].[Work Date] = '06-13-2012'
How many records you have like this?
Do all the Employee ID's match the ID's in Employees table?
If you need all the employees than you should do an LEFT OUTER JOIN:
SELECT Employees.[Employee ID],
Employees.Employee,
Employees.[First Name],
Employees.[Middle Name],
Employees.[Last Name],
Employees.Position,
[Work].[Work ID]
FROM Employees LEFT OUTER JOIN
[Work] ON Employees.[Employee ID] = [Work].[Employee ID]
AND [Work].[Work Date] = '06-13-2012'
Is this what you want?
Also try to check out [Work Date] column- if it also have time part (DateTime data type), then you should take that in account:
SELECT
Employees.[Employee ID],
Employees.Employee,
Employees.[First Name],
Employees.[Middle Name],
Employees.[Last Name],
Employees.Position,
[Work].[Work ID]
FROM Employees
LEFT JOIN [Work]
ON Employees.[Employee ID] = [Work].[Employee ID]
WHERE Convert(Char(10), [Work].[Work Date], 120) = '2012-06-13' -- can find better way

Resources