Incompatible if statement when migrating from MySql to SQL Server - sql-server-2008

I have this query
select if(flow_peymankar_type.title = '' ,
flows.title, flow_peymankar_type.title) as title,
flows.minRow,
[flow_peymankar_type].id,
[flows].[file],
[flows].isMandetory
from flow_peymankar_type
inner join flows
on flows.id = flow_peymankar_type.flowsId
where [peymankarTypeId]=11
order by flow_peymankar_type.code desc
and it's working fine in mysql.
But now I have to migrate to sql server 2008,
but this query is a problem for me and I don't know how to change it for working in sql server. plz help

Unfortunately, IIF is only available in sql server 2012 and later. Use CASE instead:
SELECT CASE
WHEN flow_peymankar_type.title = '' THEN flows.title
ELSE flow_peymankar_type.title
END AS title
....

Related

Equivalent of SQL Server's CONTAINSTABLE in MySQL

I am converting a Query from SQL Server to MySQL but I have a problem converting the sentence contains table , because it uses rank .
I´ve looked for a similar property but I didn´t find anything , here is my Query at SqlServer
SELECT KEY_TBL.RANK
FROM CATS
INNER JOIN containsTABLE (CATS,(COLOR,CITY),'ORANGE',1000) AS KEY_TBL ON RUP.ID = KEY_TBL.[KEY]
ORDER BY RANK

MySql Query acts differently on my local version (8.0.21) and my staging (5.7.12)

I have an issue running a query on MySql using a Goapplication.
My local version works fine using mysql 8.0.21 but the same query on my staging version 5.7.12 on aurora fails
SELECT COUNT(*) AS cnt
FROM (
SELECT ? AS item_id, ? AS ar
)
AS A
JOIN item ON A.item_id = item.id
AND A.ar + item.existing_qty > item.qty;
Running this code in data grip with replacements works fine on both local and staging
Running this code but replacing the question marks with ints works fine
The error I get is:
Error 1054: Unknown column 'A.ar' in 'field list
I am thinking there is some driver / version issue
Does it work if you phrase the query like this?
select count(*)
from item
where item = ? and existing_qty + ? < qty
This is the same logic as your original query, and the parameters are passed in the same order.

Select statement failing SQL Server

I have a select statement running in a jsp against SQL Server (previously using MySql without issues).
the TOP 1 was added because otherwise SQL Server moans about order by clauses (but only when displaying a result in a jsp, not when running the query inside SQL Server Management Studio).
This query runs fine in SQL Server Management Studio
SELECT TOP 1
alerts.id,
alerts.ts,
asset_firstname,
asset_lastname,
assetid,
alerttype.name,
node.zonename,
node.ipaddress,
node.zonegroupid
from
alerts, asset, alerttype, node, alertrules
where
ack=0 and
alerts.nodeid = node.id and
alerts.alerttypeid = alerttype.id and
alertrules.alerttypeid = alerts.alerttypeid and
alerts.assetid = asset.id and
alerts.alerttypeid = 1 and
asset.id=1157 and
alertrules.userid = 1
order by alerts.ts desc
but, when run in the jsp it returns "Column alerts.ts is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause".
I don't want alerts.ts aggregated or grouped by, hence the 'correct' select statement.
If I remove TOP 1 or alerts.ts desc the query returns the wrong row (earliest rather than latest record)
Converting what should be straightforward basic SQL commands so they run properly with SQL Server is proving a nightmare.
Any thoughts appreciated.
Regards
Ralph
(I wrote this as an answer because as a comment would be a mess)
You are using old style joins, and have redundant checks. Maybe this could make a difference (not sure, as it seems to be a problem related to JSP):
SELECT TOP(1)
alerts.id, alerts.ts,
asset_firstname,
asset_lastname,
assetid,
alerttype.name,
node.zonename,
node.ipaddress,
node.zonegroupid
from alerts
inner join asset on alerts.assetid = asset.id
inner join alerttype on alerts.alerttypeid = alerttype.id
inner join node on alerts.nodeid = node.id
inner join alertrules on alertrules.alerttypeid = alerts.alerttypeid
where ack=0 and
alerts.alerttypeid = 1 and
asset.id=1157 and
alertrules.userid = 1
order by alerts.ts desc;

group_concat to sql server in a view

i'm trying to migrate a mysql view from mysql to sql server 2008, but there is a funcion called "group_concat" and i cant emulate it in sql server.
Here is the code
select
permiso.ID AS ID,replace(
group_concat(cuerpo_legal.descripcion, _utf8' ',
documento_legal.NUM_CUERPO_LEGAL, _utf8' / ',
documento_legal.ANNO_CUERPO_LEGAL separator ',')
,_utf8',', _utf8' ; ') AS cuerpo_legal
from
(((permiso
join permiso_doc_legal ON ((permiso_doc_legal.ID_PERMISO = permiso.ID)))
join documento_legal ON ((documento_legal.ID = permiso_doc_legal.ID_DOCUMENTO_LEGAL)))
join cuerpo_legal ON ((cuerpo_legal.id_cuerpo_legal = documento_legal.ID_CUERPO_LEGAL)))
group by permiso.ID
I've tried diferent ways and searched here in stackoverflow but none one works
Thanks!

MySQL left join subquery fail

Following query runs well in MySQL 5.x
SELECT
m_area.id, m_area.cn_areaName, m_area.de_areaName,
m_area.en_areaName,m_area.jp_areaName,t_shop.count
FROM
m_area left join
(
select t_shop.areaID, count(areaID) AS count
from t_shop
group by t_shop.areaID
) t_shop
on m_area.id = t_shop.areaID
However, when I have to run it in a 4.0.23 MySQL DB with same DB structure and data it just return following message:
1064 - 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 '[
select t_shop.areaID, count(areaID) AS count
from t_s
I tried many times but still failed. Is left join to subquery not allowed in MySQL 4.x ? Then that mean I have to do it with a temp table?
Thanks in advance!
Subqueries were quite not well supported with MySQL 4.0 : it became possible to use them (at least, in some real, useful way) with MySQL 4.1 -- and MySQL 4.0 is really old, now...
See for instance this page of the MySQL manual : 12.2.8. Subquery Syntax (quoting, emphasis mine) :
Starting with MySQL 4.1, all subquery forms and operations that the
SQL standard requires are supported,
as well as a few features that are
MySQL-specific.
With MySQL versions prior to 4.1, it
was necessary to work around or
avoid the use of subqueries. In
many cases, subqueries can
successfully be rewritten using joins
and other methods. See Section
12.2.8.11, “Rewriting Subqueries as Joins for Earlier MySQL Versions”.
take out ", count(areaID) AS count"
The multiple columns in the subquery is messing up the join.
A temp table should work fine ....
Have fun!
Only thing I could think of is adding the tablename to your areaID in the subquery or renaming the reserved word count to cnt.
SELECT m_area.id
, m_area.cn_areaName
, m_area.de_areaName
, m_area.en_areaName
,m_area.jp_areaName
,t_shop.cnt
FROM m_area
left join (
select t_shop.areaID
, count(t_shop.areaID) AS cnt
from t_shop
group by t_shop.areaID
) t_shop on m_area.id = t_shop.areaID