incorrect syntax near the keyword 'AS' - sql-server-2005

Why do i receive a syntax error as such: Incorrect syntax near the keyword 'AS'
I am using microsoft visual studio 2005 and sql server 2005
string strSql =
"SELECT a.MCode, a.NameOfModule, a.Mod_Abbreviation, dt.ModuleCode,
dt.Course, dt.Stage, dt.ModuleGrpFrom, dt.ModuleGrpTo, dt.GrpName,
dt.GrpType, dt.StaffID, dt.AcadYear, dt.AcadSemester,
dt.TotalHour, dt.WeeklyLectHr, dt.WeeklyPractHr, dt.WeeklyTutHr,
dt.ModuleLeader, 0 AS TotalTeach, '' AS ModuleGroups, '' AS ML, 0 AS L, 0 AS P, 0 AS T, 1 AS NofGrp, '' AS TotalTeachUnit" +
"FROM (SELECT * FROM
(SELECT a.ModuleCode, a.Course, a.Stage, a.ModuleGrpFrom,
a.ModuleGrpTo, a.GrpName, a.GrpType, a.StaffID, b.AcadYear,
b.AcadSemester, b.TotalHour, b.WeeklyLectHr, b.WeeklyPractHr,
b.WeeklyTutHr, b.ModuleLeader
FROM ModuleStrGrp a
LEFT JOIN ModuleStr b ON a.AcadYear = b.AcadYear
AND a.AcadSemester = b.AcadSemester
AND a.Course = b.Course
AND a.ModuleCode = b.ModuleCode) AS MSG
INNER JOIN Parameter Para ON MSG.Course = Para.Paracode2) AS dt
LEFT JOIN Module a ON dt.ModuleCode = a.MCode" +
"WHERE dt.AcadYear LIKE AcadYear AND dt.AcadSemester LIKE AcadSemester
AND dt.Course LIKE Course AND dt.Stage LIKE Stage AND dt.StaffID LIKE Staff
AND dt.SpecGrp LIKE Specialisation" +
"ORDER BY dt.ModuleCode, dt.GrpType";

Missing spaces.
Your code is creating the string
"... , '' AS TotalTeachUnitFROM (SELECT * ..."
Same goes for the other lines, they need spaces between them.

I won't answer your question, but I'll give you a strategy:
copy the contents of the strSql string to the clipboard, then to a query in SQL Management Studio, and run it there.
Then it will actually tell you which "AS" is causing the problem. Most likely it's a missing space or appostraphe or comma.

make "FROM ... as " FROM ...
right now it is ... AS TotalTeachUnitFROM ...

You are missing spaces.
"...a ON dt.ModuleCode = a.MCode" +
"FROM (SELECT * FROM ..."
results in
"...a ON dt.ModuleCode = a.MCodeFROM (SELECT * FROM ..."
^
space missing

Rather use # instead of +
string strSql = #"SELECT a.MCode, a.NameOfModule, a.Mod_Abbreviation, dt.ModuleCode, dt.Course, dt.Stage, dt.ModuleGrpFrom, dt.ModuleGrpTo, dt.GrpName, dt.GrpType, dt.StaffID, dt.AcadYear, dt.AcadSemester, dt.TotalHour, dt.WeeklyLectHr, dt.WeeklyPractHr, dt.WeeklyTutHr, dt.ModuleLeader, 0 AS TotalTeach, '' AS ModuleGroups, '' AS ML, 0 AS L, 0 AS P, 0 AS T, 1 AS NofGrp, '' AS TotalTeachUnit
FROM (SELECT * FROM (SELECT a.ModuleCode, a.Course, a.Stage, a.ModuleGrpFrom, a.ModuleGrpTo, a.GrpName, a.GrpType, a.StaffID, b.AcadYear, b.AcadSemester, b.TotalHour, b.WeeklyLectHr, b.WeeklyPractHr, b.WeeklyTutHr, b.ModuleLeader FROM ModuleStrGrp a LEFT JOIN ModuleStr b ON a.AcadYear = b.AcadYear AND a.AcadSemester = b.AcadSemester AND a.Course = b.Course AND a.ModuleCode = b.ModuleCode) AS MSG INNER JOIN Parameter Para ON MSG.Course = Para.Paracode2) AS dt LEFT JOIN Module a ON dt.ModuleCode = a.MCode
WHERE dt.AcadYear LIKE AcadYear AND dt.AcadSemester LIKE AcadSemester AND dt.Course LIKE Course AND dt.Stage LIKE Stage AND dt.StaffID LIKE Staff AND dt.SpecGrp LIKE Specialisation
ORDER BY dt.ModuleCode, dt.GrpType";

Related

Coalesce with a conditional expression

The following query works when executed in SequelPro:
SELECT * FROM friend_computer LEFT JOIN computer m ON computer_id = m.id LEFT JOIN computer_status ms ON m.id = ms.computer_id JOIN friend_account pa ON friend_account_id = pa.id WHERE friend_account_id = 1 ORDER BY coalesce(ms.last_connect_time < ms.last_disconnect_time, -1) asc;
But in Hibernate, I keep getting a syntax error for the below:
String sql2 = "SELECT pm FROM FriendComputer pm LEFT JOIN FETCH pm.computer m JOIN FETCH pm.friendAccount pa " +
"WHERE pm.friendAccount.id = :friendId AND pm.computer = m ORDER BY coalesce((m.computerStatus.lastDisconnectTime < m.computerStatus.lastConnectTime),-1 ) " + sortOrder;
org.hibernate.query.Query q = session().createQuery(sql2).setParameter("friendId", friendId);
The syntax error is due to the coalesce function. I can't find any online resources about how to use an expression inside coalesce.
What are you trying to do? An explicit case would at least provide clarity:
order by (case when ms.last_connect_time < ms.last_disconnect_time then 2
when ms.last_connect_time >= ms.last_disconnect_time 1
else 0
end)

difference in two tables Syntax error in SQL FROM Clause

I am trying to compare two tables with different columns.
I tried below code but it is giving syntax error
Syntax error:
"Exception thrown by code stage: Syntax error in FROM clause"
SELECT [Sheet1].[ID], [Sheet2].[ID_EXT] from [Sheet1], [Sheet2]
A As (SELECT [Sheet1].[ID], ([Sheet1].[Email] + ';' + [Sheet2].[Long Email]) as email from [Sheet1] inner join [Sheet2]
on [Sheet1].[ID] = FORMAT([Sheet2].[ID_EXT],'00000000000')
WHERE [Sheet2].[Type] = 3 AND UCase [Sheet1].[Email] <> UCase [Sheet2].[Long Email])
B As (SELECT [Sheet1].[ID], ([Sheet1].[Batchcode] + ';' + str([Sheet2].[Code])) as Code from [Sheet1] inner join [Sheet2] on [Sheet1].[ID] = FORMAT([Sheet2].[ID_EXT],'00000000000') WHERE [Sheet2].[Type]= 3 AND [Sheet1].[Batchcode]<>FORMAT([Sheet2].[Code],'0000))
SELECT [A].[ID], [A].[Email], [B].[Batchcode] from [A] Full outer join [A] ON [A].[ID]=[B].[ID_EXT]
Instead of A as (select...) you need , (select ... ) as A.

SQL query fails, no idea why

So I have just upgraded to MySQL 8 to test if it would be a viable option, but I have stumbled upon quiet the riddle.
It throws an error at 'table_product_features AS pf' but I can't seem to figure out why as in the documentation it is done exactly the same way: https://dev.mysql.com/doc/refman/8.0/en/join.html
Error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'groups ON pf.parent_id = groups.feature_id LEFT JOIN table_product_features_des' at line 1 (1064)
I also checked the bug tracker but that doesnt seem to have anything related to my issue.
What exactly am I doing wrong here?
P.S. I put it in a code snippet so that it would remain formatted.
SELECT pf.feature_id,
pf.company_id,
pf.feature_type,
pf.parent_id,
pf.display_on_product,
pf.display_on_catalog,
pf.display_on_header,
table_product_features_descriptions.description,
table_product_features_descriptions.lang_code,
table_product_features_descriptions.prefix,
table_product_features_descriptions.suffix,
pf.categories_path,
table_product_features_descriptions.full_description,
pf.status,
pf.comparison,
pf.position,
groups.position AS group_position,
table_product_features_values.value,
table_product_features_values.variant_id,
table_product_features_values.value_int
FROM table_product_features AS pf
LEFT JOIN table_product_features AS groups
ON pf.parent_id = groups.feature_id
LEFT JOIN table_product_features_descriptions
ON table_product_features_descriptions.feature_id = pf.feature_id
AND table_product_features_descriptions.lang_code = 'en'
INNER JOIN table_product_features_values
ON table_product_features_values.feature_id = pf.feature_id
AND table_product_features_values.product_id = 230
AND table_product_features_values.lang_code = 'en'
INNER JOIN table_ult_objects_sharing
ON ( table_ult_objects_sharing.share_object_id = pf.feature_id
AND table_ult_objects_sharing.share_company_id = 1
AND table_ult_objects_sharing.share_object_type =
'product_features' )
WHERE 1
AND pf.feature_type != 'G'
AND pf.status IN ( 'A' )
AND pf.display_on_product = 'Y'
AND ( pf.categories_path = ''
OR Isnull(pf.categories_path)
OR Find_in_set(203, pf.categories_path)
OR Find_in_set(215, pf.categories_path)
OR Find_in_set(216, pf.categories_path) )
GROUP BY pf.feature_id
ORDER BY group_position,
pf.position,
table_product_features_descriptions.description
If your not using aggregate functions I don't see why you need the GROUP BY.
Also groups is a reserved word, change it to tgroups or something else.
SELECT pf.feature_id,
pf.company_id,
pf.feature_type,
pf.parent_id,
pf.display_on_product,
pf.display_on_catalog,
pf.display_on_header,
tdesc.description,
tdesc.lang_code,
tdesc.prefix,
tdesc.suffix,
pf.categories_path,
tdesc.full_description,
pf.status,
pf.comparison,
pf.position,
tgroups.position group_position,
tvals.value,
tvals.variant_id,
tvals.value_int
FROM table_product_features pf
LEFT JOIN table_product_features tgroups ON pf.parent_id = tgroups.feature_id
LEFT JOIN table_product_features_descriptions tdesc ON tdesc.feature_id = pf.feature_id AND tdesc.lang_code = 'en'
INNER JOIN table_product_features_values tvals ON tvals.feature_id = pf.feature_id AND tvals.product_id = 230 AND tvals.lang_code = 'en'
INNER JOIN table_ult_objects_sharing tshar ON (tshar.share_object_id = pf.feature_id AND tshar.share_company_id = 1 AND tshar.share_object_type = 'product_features')
WHERE 1
AND pf.feature_type != 'G'
AND pf.status IN ('A')
AND pf.display_on_product = 'Y'
AND (pf.categories_path = '' OR Isnull(pf.categories_path) OR Find_in_set(203, pf.categories_path) OR Find_in_set(215, pf.categories_path) OR Find_in_set(216, pf.categories_path))
ORDER BY group_position, pf.position, tdesc.description

Teradata 3706, expected something between DISTINCT and LEFT keyword

I am here trying to use UPDATE query with JOIN and SELECT, But I am facing 3706 error.
here is the error line -
UPDATE A0619IL3549_D_RPT.RPT_IS_ELIGIBILITY A2
FROM
(SELECT A.REP_CD, A.GEO_CD,A.MONTH_ID, B.IC_PRD AS IC_PAYOUT_FLAG,
COALESCE(CASE
WHEN UPPER(B.IC_PRD) = 'SEMESTERLY' THEN A.CURR_SEMESTER_FLAG
WHEN UPPER(B.IC_PRD) = 'QUARTERLY' THEN A. CURR_QUARTER_FLAG
ELSE '0'
END,'0') IC_PAYOUT_FLAG
FROM
A0619IL3549_D_RPT.RPT_IS_ELIGIBILITY A,
(
SELECT DISTINCT LEFT(B. REP_CD, POSITION('-' IN B.REP_CD)-1) AS REP_CD,C.GEO_CD,IC_PRD FROM
A0619IL3549_D00_IC_MAIN.ICDM_GEO_REP_PROD A, A0619IL3549_D00_IC_MAIN.ICDM_DIM_REP B,A0619IL3549_D00_IC_MAIN.ICDM_DIM_GEO_HIER C
WHERE
A.REP_SK=B.REP_SK
AND A.GEO_SK=C.GEO_SK
AND A.RUN_ID = (SELECT MAX(RUN_ID) FROM A0619IL3549_D00_IC_MAIN.ICDM_GEO_REP_PROD)
) B
WHERE
A.REP_CD = B.REP_CD
AND
A.GEO_CD = B.GEO_CD)A1
SET A2.IC_PAYOUT_FALG = A1.IC_PAYOUT_FLAG
WHERE
A1.REP_CD = A2.REP_CD
AND
A1.MONTH_ID = A2.MONTH_ID
AND
A.GEO_CD = A2.GEO_CD
LEFT is not a valid Teradata function before TD15.10, it's ODBC SQL, which is automatically translated, but only for SELECT.
Use valid syntax instead:
SUBSTRING(B. REP_CD FROM 1 FOR POSITION('-' IN B.REP_CD)-1)

Converting an Access Query to SQL Server

I am trying to convert this query to a SQL Server view
SELECT
IIF([tblStockMovementsDate] ! [LocationType] = 3, [tblShops] ! [Type], [tblLocationType] ! [LocationTypeDesc]) AS [LocationTypeDesc],
tblStockMovementsDate.[Location],
qryAllLocations.[WarehouseName],
SUM (([tblStockMovementsdate] ! [MovementQtyHangers] + ([tblStockMovementsdate] ! [MovementQtyBoxes] * Iif(Isnull([qryGarmentsPerBox] ! [GarmentsPerBox]), 1, [qryGarmentsPerBox] ! [GarmentsPerBox]))) * [qryUnitPrice] ! [UnitPrice]) AS [TotalValue],
IIF ([tblShops] ! [Type] = 'Concession', 4, [tblStockMovementsDate] ! [LocationType]) AS [LocationType]
FROM
(((((tblStockMovementsDate
INNER JOIN
tblLocationType ON tblStockMovementsDate.[LocationType] = tblLocationType.[LocationType])
INNER JOIN
qryAllLocations ON tblStockMovementsDate.[Location] = qryAllLocations.[WarehouseRef])
LEFT JOIN
qryGarmentsPerBox ON (tblStockMovementsDate.[Location] = qryGarmentsPerBox.[WarehouseRef])
AND (tblStockMovementsDate.[StockCode] = qryGarmentsPerBox.[StockCode]))
LEFT JOIN
qryUnitPrice ON tblStockMovementsDate.[StockCode] = qryUnitPrice.[StockCode])
LEFT JOIN
tblShops ON tblStockMovementsDate.[Location] = tblShops.[ShopRef])
INNER JOIN
tblStock ON tblStockMovementsDate.[StockCode] = tblStock.[StockCode]
WHERE
((IIF ([tblShops].[Clearance] = 0, [tblStock].[DeadCode] = 0, [tblStock].[RemoveFromClearance] = 0) ) <> False)
GROUP BY
IIF ([tblStockMovementsDate] ! [LocationType] = 3, [tblShops] ! [Type], [tblLocationType] ! [LocationTypeDesc]),
tblStockMovementsDate.[Location],
qryAllLocations.[WarehouseName],
IIF ([tblShops] ! [Type] = 'Concession', 4, [tblStockMovementsDate] ! [LocationType])
ORDER BY
IIF ([tblShops] ! [Type] = 'Concession', 4, [tblStockMovementsDate] ! [LocationType])
but it won't produce an output, instead I keep getting this error:
Error in list of function arguments: '=' not recognized.
Error in list of function arguments: 'IS' not recognized.
Unable to parse query text.
Any help would be appreciated.
This is a shot in the dark. I did my best to decipher the code you posted originally.
I would recommend getting rid of those nested views. They are going to cause you serious anguish at some point. You will likely have to grab the queries from those views and move that to this query.
Notice how much cleaner this is when you use aliases instead of the Access default of slapping the table in front of every column. I also removed dozens of parenthesis groups that did nothing but make this hard to see. I also converted all your IIF expressions to case expressions since I don't know what version of sql you are using.
SELECT
case when smd.LocationType = 3
then tblShops.Type
else lt.LocationTypeDesc
end AS LocTypeDesc
, smd.Location
, al.WarehouseName
, Sum((smd.MovementQtyHangers + (smd.MovementQtyBoxes * Isnull(g.GarmentsPerBox, 1))) * qryUnitPrice.UnitPrice) AS TotalValue
, case when sh.Type = 'Concession'
then 4
else smd.LocationType
end AS LocType
FROM tblStockMovementsDate smd
INNER JOIN tblLocationType lt ON smd.LocationType = ll.LocationType
INNER JOIN qryAllLocations al ON smd.Location = al.WarehouseRef
LEFT JOIN qryGarmentsPerBox g ON smd.Location = g.WarehouseRef
AND smd.StockCode = g.StockCode
LEFT JOIN qryUnitPrice up ON smd.StockCode = up.StockCode
LEFT JOIN tblShops sh ON smd.Location = sh.ShopRef
INNER JOIN tblStock st ON smd.StockCode = st.StockCode
WHERE tblShops.Clearance <> 0
OR tblStock.DeadCode <> 0
OR tblStock.RemoveFromClearance <> 0
GROUP BY case when smd.LocationType = 3 then tblShops.Type else lt.LocationTypeDesc end
, smd.Location
, al.WarehouseName
, case when sh.Type = 'Concession' then 4 else smd.LocationType end
order by LocType