Update query on SQL Server with link server with oracle - sql

I want to update column in SQL Server 2008 from link server (oracle) table.
I have table columns are opr_code, m_code etc.
In SQL Server table which has value in opr_code.
I want to update m_code value in SQL Server from link server (oracle) where common value is opr_code which is conf_code in oracle. I tried with following query
update test_S set m_code=A.M_CODE from
(Select * FROM OPENQUERY(linkserver,'Select * From abcd.NAME_desk)) A
inner join test_S B on b.opr_code=a.conf_code

UPDATE t
SET m_code = l.m_code
FROM test_s t
JOIN OPENQUERY(linkserver,'SELECT conf_code, m_code FROM abcd.name_desk') l
ON t.opr_code = l.conf_code

Related

PostgreSQL - No records returned with INNER JOIN but INNER JOIN is working with MS SQL Server [duplicate]

This question already has answers here:
PostgreSQL "Column does not exist" but it actually does
(6 answers)
sql statement error: "column .. does not exist"
(1 answer)
SQL query column does not exist error
(1 answer)
Closed last year.
I have migrated some tables and table data from MS SQL Server to PostgreSQL. The Data on both the sides is identical. However, when I am trying to query two tables using INNER JOIN in MS SQL Server and PostgreSQL the results are different.
With MS SQL Server , I am getting the more than 18K records whereas in PostgreSQL no record is returned !
I know this is weird and it seems to be something related to either syntax or some configuration that I am missing on the PostgreSQL side. I am using pgAdmin4 for querying my PostgreSQL database and SSMS 2018 for querying MS SQL Server database respectively.
Here's my queries -
PostgreSQL
select *
from dbo.foo AS fo
INNER join dbo.woo AS wo
on 'fo.ID' = 'wo.ORDERID'
Also tried this way but its still giving me no records
select *
from dbo.foo INNER JOIN dbo.woo
on 'foo.ID' = 'woo.OrderID'
MS SQL Server
select *
from foo
INNER JOIN woo
on foo.ID = woo.OrderID
Not sure where the problem lies. Can anyone please suggest something ?
You are comparing strings and the result is false because 'fo.ID' is different from 'wo.ORDERID'.
You have to write :
select *
from dbo.foo AS fo
INNER join dbo.woo AS wo
on fo.ID = wo.ORDERID
or if the table has been created with double quotes
select *
from dbo.foo AS fo
INNER join dbo.woo AS wo
on fo."ID" = wo."ORDERID"
you should remove it from the quotes ' because 'foo.ID' does not equal 'woo.OrderID' string, and for column foo.id does not exist error, check if the column is exist by entering in your terminal psql dbname -U username --password replace the dbname and username to the real credintainls, then \d table_name if you don't find the id column DROP the table and recreate it.

Translating MS Access Query for SQL Server

I am trying to recreate a query that was done in MS Access, and now is being handled in a SQL Server environment. I understand that some of the SQL syntax is different in Access than it is in SQL Server. Is there somewhere online that points out the main differences, or will help translate one to the other?
This is a query that I need to update to use in SQL Server:
UPDATE
dbo.TPR00100
INNER JOIN (
dbo.UPR10302
LEFT JOIN dbo.B3980280 ON dbo.TPR10302.COMPTRNM = dbo.B3980280.COMPTRNM
) ON dbo.TPR00100.STAFFID = dbo.TPR10302.STAFFID
SET
dbo.B3980280.COMPTRNM = dbo.TPR10302.comptrnm,
dbo.B3980280.BI_LOC_ID = dbo.TPR00100.locatnid
WHERE
(((dbo.B3980280.COMPTRNM) Is Null))
What are they key aspects that need to be handled differently in a SQL Server transaction for this query?
If find it handy to use an updateable CTE for this:
with cte as (
select
b39.comptrnm b39_comptrnm
b39.bssi_loc_id b39_bssi_loc_id,
tpr.comptrnm tpr_comptrnm,
tpr.locatnid tpr_locatnid
from dbo.tpr00100 tpr
inner join dbo.upr10302 upr on tpr.staffid = upr.staffid
inner join dbo.b3980280 b39 on tpr.comptrnm = b39.comptrnm
where b39_comptrnm is null
)
update cte
set b39_comptrnm = tpr_comptrnm, b39_bssi_loc_id = tpr_locatnid
Note: I am not really sure why the table to update is left joined in the original query, so I turned it to an `inner join .

How write a query in SQL Server 2000?

I have 2 databases with the same tables and views, one in SQL Server 2008 and another in SQL Server 2000.
I wrote this query and it works in SQL Server 2008, but it didn't work in SQL Server 2000.
How can I change my code to work in SQL Server 2000 ?
SELECT
SUM(NA_DA) OVER (PARTITION BY vd.SI_VoucherH) AS a,
SUM(NA_CA) OVER (PARTITION BY vd.SI_VoucherH) AS b
FROM
acc.ACC_VOUCHERH vh
INNER JOIN
acc.Acc_VoucherD vd ON vh.SI_VoucherH = vd.SI_VoucherH
It's hard to say w/o knowing which table NA_DA & NA_CA are coming from or knowing which table has SI_VoucherH as it PK and which has it a FK.
Hopefully the following will get you close...
SELECT
vda.NA_DA,
vda.NA_CA
FROM
acc.ACC_VOUCHERH vh
JOIN (
select
vd.SI_VoucherH,
NA_DA = Sum(vd.NA_DA),
NA_CA = SUM(NA_CA)
FROM
acc.Acc_VoucherD vd
GROUP BY
vd.SI_VoucherH
) vda
on vh.SI_VoucherH=vda.SI_VoucherH;

SQL Server 2008: update date from row in same column

I've seen this question a lot, but I can't seem to get my SQL to work so I'm hoping someone can help.
I've confirmed that this code will work in Access, but it isn't working with SQL Server 2008.
UPDATE shop_orders AS t1, shop_orders AS t2
SET t1.shipstreet1 = t2.shipstreet1,
t1.shipstreet2 = t2.shipstreet2,
t1.shipcity = t2.shipcity,
t1.shipregionstate = t2.shipregionstate
WHERE t1.orderid=3292
AND t2.orderid=3641;
Advice?
In SQL Server's T-SQL, you cannot have multiple tables in the UPDATE clause, nor can you give that table a table alias.
You need to use:
UPDATE shop_orders
FROM shop_orders AS t2
SET shipstreet1 = t2.shipstreet1,
shipstreet2 = t2.shipstreet2,
shipcity = t2.shipcity,
shipregionstate = t2.shipregionstate
WHERE shop_orders.orderid = 3292
AND t2.orderid = 3641;
The syntax for Update From is different for SQL server, the same query will not work in both.
See:
SQL update from one Table to another based on a ID match
For more information on Update From syntax.

Updating Rows Based on Multiple Tables in SQL Server Compact Edition [duplicate]

This question already has an answer here:
How to do Sql Server CE table update from another table
(1 answer)
Closed 7 years ago.
How to Updating Rows Based on Multiple Tables in SQL Server Compact Edition ?
I have two tables in a database. ActivatedProducts and DocumentSettings.I added new column (UID) in DocumentSettings table,i want to put that UID data from ActivatedProducts (ID) Table with respect to ProductID from ActivatedProducts table
following query's also not working please help me
UPDATE DocumentSettings
SET UID =
(
SELECT ActivatedProducts.ID
FROM ActivatedProducts
WHERE DocumentSettings.TitleID = ActivatedProducts.ProductID
)
UPDATE A
SET A.UID = B.ID
FROM DocumentSettings A, ActivatedProducts B
WHERE A.TitleID = B.ProductID
UPDATE DocumentSettings
SET [UID]=AP.[ID]
FROM DocumentSettings DS
INNER JOIN ActivatedProducts AP ON DS.[Titleid]=AP.[ProductID]
The last time I checked, SQL Server CE still does not support UPDATE-FROM-JOIN syntax. All 3 you have shown will work in Sql Server proper, but in CE, you will need to programmatic-ally retrieve each value for updating in a loop.
Reference: UPDATE (SQL Server Compact - 2008)
You can do it as per below
UPDATE [table one]
SET [table one].result = [table two] .results
FROM [table one] T1
INNER JOIN [table three] t3
on t1.reg_id = t3.reg_id
INNER JOIN [table two] T2
on t2.venue = t3.venue