How can be expressed this Access Query to SQL Server query - sql

I have a database in access but recently moved to SQL server, and I have modified almost all queries but this one :
UPDATE Articulos_Auditoria
INNER JOIN Auditoria ON Articulos_Auditoria.CUD = Auditoria.CUD
SET Articulos_Auditoria.Cortado = 'True'
WHERE
(((CAST([Fecha] AS DATE)) = CAST(#Fecha AS DATE))
AND ((Auditoria.Terminal) = #term))
I am trying to convert it to SQL Server (since I changed DateValue to a CAST) but intellisense gives me a syntax error near 'INNER'
Can anyone give me some insight?

You have two errors:
You need to define the SET after the UPDATE TableName
You need to define a FROM clause
UPDATE Articulos_Auditoria
SET Cortado = 'True'
FROM Articulos_Auditoria
INNER JOIN Auditoria
ON Articulos_Auditoria.CUD = Auditoria.CUD
WHERE CAST([Fecha] as date)=CAST(#Fecha as date)
AND Auditoria.Terminal=#term

Use the from clause and table aliases:
UPDATE aa
SET Cortado = 'True'
FROM Articulos_Auditoria aa INNER JOIN
Auditoria a
ON aa.CUD = a.CUD
WHERE CAST([Fecha] as date) = CAST(#Fecha as date) AND (a.Terminal = #term)

Related

I am trying to convert Oracle view into SQL, but i am having issue with query performance

Oracle
(TO_CHAR(P.EstimatedInServiceDate, 'YYYY') =
(SELECT SUBSTR(ConfigurationValue, -4)
FROM PETE.vw_Configuration
WHERE configurationkey = 'CurrentPATYear'))
SQL - I tried to convert above code it is returning a data fine but my query is taking too long to run because of this particular chunk. If any better way to do this please let me know.
(Cast(DatePart(Year,P.EstimatedInServiceDate)as varchar) =
(SELECT right(ConfigurationValue, 4)
FROM Configuration
WHERE configurationkey = 'CurrentPATYear'))
You can use datepart() only :
where datepart(year, P.EstimatedInServiceDate) = (SELECT right(ConfigurationValue, 4)
FROM Configuration
WHERE configurationkey = 'CurrentPATYear')
)
It's a poor practice in SQL Server (or Oracle) to apply expressions to table columns in a WHERE-clause, as it makes the expression non-Sargable.
Instead write this something like:
P.EstimatedInServiceDate >=
(SELECT ConfigurationValue
FROM Configuration
WHERE configurationkey = 'CurrentPATYear')
and
P.EstimatedInServiceDate < dateadd(year,1,
(SELECT ConfigurationValue
FROM Configuration
WHERE configurationkey = 'CurrentPATYear'))

getting error The multi-part identifier "e.EpVisitCount" could not be bound. in sql server

I am working on converting mysql query to sql server, but when i run the query it gives me error The multi-part identifier "e.EpVisitCount" could not be bound. , here i have posted my both query can you please look into it, why it gives me error in sql server ?
Mysql Query(Working Fine) :
UPDATE
tb_EpVisitRange as v left JOIN tb_Episode as e ON (v.company_id = e.CustID) AND (v.CMW = e.CMW)
SET
e.EpVisitCount = If(PayerType='NonEp',0,If(LUPA=1,0,v.High)),
e.VisitAlert = If( e.TotVisits > v.High,1,NULL)
where UploadID = '23'
SQl Query(Getting Error) :
UPDATE v
SET
e.EpVisitCount = IIF(PayerType='NonEp',0,IIF(LUPA=1,0,v.High)),
e.VisitAlert = IIF( e.TotVisits > v.High,1,NULL)
FROM tb_EpVisitRange v
JOIN tb_Episode as e ON (v.company_id = e.CustID) AND (v.CMW = e.CMW)
where UploadID = '613'
You seem to want to update e rather than v. So, you might try:
UPDATE e
SET EpVisitCount = (CASE WHEN PayerType = 'NonEp' THEN 0
WHEN LUPA = 1 THEN 0
ELSE v.High
END),
VisitAlert = (CASE WHEN e.TotVisits > v.High THEN 1 END)
FROM tb_Episode e JOIN
tb_EpVisitRang v
ON v.company_id = e.CustID AND v.CMW = e.CMW
WHERE UploadID = '613';
Notes:
The problem appears to be the table alias used for the update.
SQL Server only allows updating one table in a statement. There is no need to qualify the column names for the SET.
Use CASE for conditions. It is the ANSI standard and supported by almost all database.
This is especially true for nested expressions. CASE supports multiple conditions.
You are updating e, so I made that the first table in the FROM clause. I find that logic easier to follow.
You should qualify UploadId.

SQL query works even with wrong syntax

I am running a SQL query in stored procedure which is like following
SELECT
t1.id,t2.Name
FROM
table1 t1 , table2 t2 ,table2 t3,table4 t4
WHERE
t1.id=t3.t4.id
this query gets executed on SQL server 2008 when its compatible with SQL server 2000 but if we turn OFF the compatibility with SQL server 2000 then this Query gives syntax error which is expected.
Can some one help me to understand why this is happeneing ? thanks in advance
Original query:
SELECT
ConfigID , LocationDesc + '-' + LOBTeamDesc LocLOBTeamSource
FROM Config CONFIG , Location_LOBTeam LOCLOB , Location LOC , LOBTeam LOB, System SRC
WHERE CONFIG.LocationLOBTeamID = LOC.LOB.LocationLOBTeamID
AND CONFIG.SourceSystemID = SRC.SystemID
AND LOCLOB.LocationID = LOC.LocationID
AND LOCLOB.LOBTeamID = LOB.LOBTeamID
AND (GETDATE() BETWEEN CONFIG.effectiveDate AND CONFIG.EndDate
OR CONFIG.EndDate IS NULL)
ORDER BY
LOC.LocationCode
I think that original query, with current standard join syntax applied would be this:
SELECT
ConfigID
, LocationDesc + '-' + LOBTeamDesc LocLOBTeamSource
FROM Config CONFIG
INNER JOIN Location_LOBTeam LOCLOB
ON CONFIG.LocationLOBTeamID = LOCLOB.LocationLOBTeamID
INNER JOIN Location LOC
ON LOCLOB.LocationID = LOC.LocationID
INNER JOIN LOBTeam LOB
ON LOCLOB.LOBTeamID = LOB.LOBTeamID
INNER JOIN [System] SRC
ON CONFIG.SourceSystemID = SRC.SystemID
WHERE (GETDATE() BETWEEN CONFIG.effectiveDate AND CONFIG.EndDate
OR CONFIG.EndDate IS NULL)
ORDER BY
LOC.LocationCode
Perhaps this will help.
+EDIT
"System" as a table name, could that be a problem? Suggest you try it as [System]
+EDIT2
The original is given with this: LOC.LOB.LocationLOBTeamID but that appears to be an error as there is an alias LOCLOB
I think below post from msdn answers this issue Compatibility Levels and Stored Procedures
in the above post the point number 3 under section "Differences Between Compatibility Level 80 and Level 90" states "WHEN binding the column references in the ORDER BY list to the columns defined in the SELECT list, column ambiguities are ignored and column prefixes are sometimes ignored. This can cause the result set to return in an unexpected order."
on my database I am using compatibility level 80 i.e 2000 thats why it runs smoothly with the given syntax but when I remove this compatibility and make it to 100 i.e. 2008/R2 script gives syntax error which is expected

How do I update multiple columns with a subquery in a single statement?

I am attempting to update a temp table from a source table:
UPDATE #DETAIL
SET EXCD_ID, CDOR_OR_AMT, CDOR_OR_VALUE
(SELECT
CDID_ADDL_DATA_1, CDID_ADDL_DATA, CDID_VALUE_STRING
FROM
CMC_CDID_DATA CDID
WHERE
CDID.CLCL_ID = DTL.CLCL_ID AND
CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO AND
CDID_TYPE = 'NDC'
)
FROM #DETAIL DTL
WHERE DTL.CDOR_OR_ID = 'XS'
Unfortunately it complains
Incorrect syntax near ',' (on the '(SELECT' line)
Incorrect syntax near 'FROM' (the second one)
After much trial and error I pooled some help at work and we came up with this:
UPDATE #DETAIL
SET DTL.EXCD_ID = CDID.CDID_ADDL_DATA_1,
DTL.CDOR_OR_AMT = CONVERT(MONEY,CDID.CDID_ADDL_DATA),
DTL.CDOR_OR_VALUE = CDID.CDID_VALUE_STRING
FROM #DETAIL DTL
INNER JOIN
CMC_CDID_DATA CDID ON
CDID.CLCL_ID = DTL.CLCL_ID AND
CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO
WHERE DTL.CDOR_OR_ID = 'XS'
AND CDID.CDID_TYPE = 'NDC'
Which sybase seems to accept.
You have to make the update like this:
UPDATE #DETAIL
SET DTL.EXCD_ID = CDID.CDID_ADDL_DATA_1,
DTL.CDOR_OR_AMT = CDID.CDID_ADDL_DATA
DTL.CDOR_OR_VALUE = CDID.CDID_VALUE_STRING
FROM #DETAIL DTL
INNER JOIN (SELECT
CDID_ADDL_DATA_1, CDID_ADDL_DATA, CDID_VALUE_STRING
FROM
CMC_CDID_DATA ) CDID ON CDID.CLCL_ID = DTL.CLCL_ID AND
CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO AND
CDID_TYPE = 'NDC'
WHERE DTL.CDOR_OR_ID = 'XS'
Check THIS ARTICLE for more info!
I just tried this out and it worked (on Oracle)
update dstTable T
set (T.field1, T.field2, T.field3) =
(select S.value1, S.value2, S.value3
from srcTable S
where S.key = T.Key);
This, unfortunately is Oracle specific syntax.
Caveat: Note that the update above has no where clause. It updates the entire table. If the subquery return no rows then the target fields are set to NULL. Also, it's an error if the subquery returns more than one row.

SQL server syntax error in update statement, but I can't see it

I'm getting a syntax error on this query, but I can't figure it out.
Incorrect syntax near the keyword
'group'.
I believe its on the last group by, but I don't see whats wrong. Can anyone suggest how to correct this?
UPDATE [NCLGS].[dbo].[CP_CustomerShipTo]
SET TimesUsed = TimesUsed + B.NewCount
from [NCLGS].[dbo].[CP_CustomerShipTo] CST
INNER JOIN (
Select
PKH.CompanyCode,
PKH.CompanyName,
PKH.Addr1,
PKH.Addr2,
PKH.City,
PKH.State,
PKH.Zip,
Count(recid) As NewCount
from avanti_packingslipheader PKH
where pksdate > dbo.ufn_StartOfDay(DATEADD(d, -1, GETDATE() ) )
group by
PKH.CompanyCode,
PKH.CompanyName,
PKH.Addr1,
PKH.Addr2,
PKH.City,
PKH.State,
PKH.Zip
) B
ON CST.CustomerCode = B.CompanyCode
AND CST.ShipToName = B.CompanyName
AND CST.ShipToAddress1 = B.Addr1
AND CST.City = B.City
AND CST.PostalCode = B.Zip
group by
PKH.CompanyCode,
PKH.CompanyName,
PKH.Addr1,
PKH.Addr2,
PKH.City,
PKH.State,
PKH.Zip
BACKGROUND - I'm trying to do an update statement with a Count(), but of course you can't use agg. functions in an update set statement, so I'm trying to use a subquery.
You have already got GROUP BY inside the subselect, so what does the outer GROUP BY stand for?
You can't reference an alias in a subselect from an outer GROUP BY. But in any event you can't use GROUP BY with an UPDATE statement, and that's what the error message is about.
Try removing the last Group By. What exactly are you hoping this last group by will do?
Change the code to this:
update mytable set
mycolumn = mycolumn + (select x from ...);