I'm a bit paranoid and novice when it comes to update scripts. Will this script work in SQL Server 2014?
I have entries in the country_code column in the address table I'd like to update, these entries are supplier addresses; and I only need to update where the terrority is Finland (You'll see that from the last line).
The script:
USE %dbname%
UPDATE [address_backup]
SET [address_backup].[country_code] = 'FI'
FROM [dbo].[address_backup]
LEFT JOIN [supplier_table] --Joins the Address table with the Supplier table
ON [address_backup].[addressid] = [supplier_table].[addressid]
LEFT JOIN [territory] --Join to the area table
ON [supplier_table].[territoryid] = [territory].[territoryid]
WHERE
[supplier_table].[deleted] <> '1' --Not Deleted
AND [territory].[territoryid] = '1000070' --Finland
I suspect it won't work?
Supposing frnachisees_territory to be territory yes, it works;
I would just check if all the JOINs are needed, since you use territoryid in the last one and then in the WHERE clause: could you avoid the last JOIN and use the supplier_table.territoryid for the WHERE?
EDIT: Possible script
USE %dbname%
UPDATE [address_backup]
SET [address_backup].[country_code] = 'FI'
FROM [dbo].[address_backup]
LEFT JOIN [supplier_table] --Joins the Address table with the Supplier table
ON [address_backup].[addressid] = [supplier_table].[addressid]
WHERE
[supplier_table].[deleted] <> '1' --Not Deleted
AND [supplier_table].[territoryid] = '1000070' --Finland
*I'd also swap LET JOIN with INNER JOIN as suggested by Juan Carlos Oropeza
Related
I am trying to write a query in oracle to only update a flag based on below scenario :
Scenario :
A mctn_id is linked with multiple PRPR_ID and each PRPR_ID can have different addresses, I need to update flag as N in a table if ALL PRPR_ID addresses don't belong to config table address. If any of it belongs to config table address then it shouldn't update the flag as N.
I am using not exists in this case which is not working.
update prcb_enroll_tbl
set prov_flg ='N',
sys_insert_dtm = systimestamp
where tin_number in (select mctn_id
from cc_pr_prov prpr
inner join cc_pr_addr prad
on prpr.prpr_id = prad.prad_id
and not exists (select 1
from fsg_prcb_config config
where prad.prad_addr1 = config.config_value)
The above query is updating a flag even if only one of the addresses belongs to config table which is not the expected outcome.
This shoulds like not exists. Does this do what you want?
update prcb_enroll_tbl pe
set prov_flg ='N', sys_insert_dtm = systimestamp
where not exists (
select 1
from cc_pr_prov pr
inner join cc_pr_addr pa on pr.prpr_id = pz.prad_id
inner join fsg_prcb_config pc on pc.config_value = pa.prad_addr1
where ??.mctn_id = pe.tin_number
)
It is unclear which table column mctn_id comes from, so I used ???: you should replace it with the correct table alias.
Trying to run an update on a column and i'm getting a syntax error on the FROM line connecting the tables
UPDATE inv_loc
SET inv_loc.product_group_id = 'TEMP'
WHERE inv_mast_ud.eh_spk LIKE '%T'
FROM
inv_mast_ud
left join inv_loc on inv_mast_ud.inv_mast_uid = inv_loc.inv_mast_uid
WHERE comes after FROM. I think you want:
UPDATE inv_loc
SET inv_loc.product_group_id = 'TEMP'
FROM inv_loc JOIN
inv_mast_ud
ON inv_mast_ud.inv_mast_uid = inv_loc.inv_mast_uid
WHERE inv_mast_ud.eh_spk LIKE '%T';
Note that I changed the LEFT JOIN to an INNER JOIN. You are updating inv_loc, so it makes no sense that that table is the second table in a LEFT JOIN.
I assume you actually want to filter the rows, so a LEFT JOIN is not needed. Otherwise, you would not need inv_mast_ud.
The WHERE clause belongs at the end of the update join statement:
UPDATE il
SET il.product_group_id = 'TEMP'
FROM inv_loc il
INNER JOIN inv_mast_ud imu
ON imu.inv_mast_uid = il.inv_mast_uid
WHERE
imu.eh_spk LIKE '%T';
See SQL update query using joins for a good canonical answer to your question.
Struggling with a create table statement which is based on this select into statement below:
#MaxAPDRefundAmount money = 13.00
...
select pkd.*, pd.ProviderReference, per.FirstName, per.Surname, #MaxAPDRefundAmount [MaxAPDRefundAmount],commission.Type [Commission] into #StagedData from CTE_PackageData pkd
inner join J2H.dbo.Package pk on pkd.Reference = pk.Reference
inner join J2H.dbo.Product pd on pk.PackageId = pd.PackageId
inner join J2H.dbo.FlightReservation fr on pd.ProductId = fr.ProductId
and fr.FlightBoundID = 1
inner join J2H.dbo.ProductPerson pp on pd.ProductId = pp.ProductID
and pp.StatusId < 7
inner join J2H.dbo.Flight f on fr.FlightId = f.FlightID
inner join J2H.dbo.Person per on pk.PackageId = per.PackageId
and per.PersonId = pp.PersonId
inner join J2H.dbo.PersonType pt on per.PersonTypeId = pt.PersonTypeID
We are changing a select into to just normal insert and select, so need a create table (we are going to create a temp (hash tag table) and not declaring a variable table. Also there is a pkd.* at the start as well so I am confused in knowing which fields to include in the create table. Do I include all the fields in the select statement into the create statement?
Update:
So virtually I know I need to include the data types below but I can just do:
create table #StagedData
(
pkd.*,
pd.ProviderReference,
per.FirstName,
per.Surname,
#MaxAPDRefundAmount [MaxAPDRefundAmount],
commission
)
"Do I include all the fields in the select statement into the create statement?" Well, that depends, if you need them, than yes, if not than no. It's impossible for us to say whether you need them... If you're running this exact query as insert, than yes.
As for the create statement, you can run the query you have, but replace into #StagedData with something like into TEMP_StagedData. In management studio you can let sql server build the create query for you: right-click the newly created TEMP_StagedData table in the object explorer (remember to refresh), script Table as, CREATE To and select New Query Editor Window.
The documentation of the CREATE TABLE statement is pretty straightforward.
No. Clearly, you cannot use pkd.* in a create table statement.
What you can do is run your old SELECT INTO statement as a straight SELECT (remove the INTO #stagedata) part, and look at the columns that get returned by the SELECT.
Then write a CREATE TABLE statement that includes those columns.
To create a table from a SELECT without inserting data, add a WHERE clause that never returns True.
Like this:
SELECT * INTO #TempTable FROM Table WHERE 1=0
Once the table with the columns for your SELECT, you can add additional columns with ALTER TABLE.
ALTER TABLE #TempTable ALL ExtraColumn INT
Then do your INSERT/SELECT.
I am trying to update a table from another database using joins and having a hard time. This is what I am trying to do in pseudo:
UPDATE [Database1].[dbo].[Sessions]
SET [SpeakerID] = ?STATEMENT1?
WHERE ?STATEMENT2?
For "Statement1", this would be coming from another database and table that has columns: SessionID and SpeakerID. How can this be achieved?
UPDATE a
SET a.SpeakerID = b.colName -- SET valoue here
FROM Database1.dbo.Sessions a
INNER JOIN Database2.dbo.Sessions b
ON a.SessionID = b.SessionID -- assumes that their
-- relationship column is SessionID,
-- change it in your original columnName
WHERE ....
a and b are called alias. They are useful when you have longer source name.
UPDATE L
SET SpeakerID = R.SpeakerID
FROM dbo.LocalTable AS L
INNER JOIN RemoteDatabase.dbo.RemoteTable AS R
ON L.SomeValue = R.SomeValue;
This really is no different from this problem except you have to add a database prefix to one of the tables in the join.
try
UPDATE [Database1].[dbo].[Sessions]
SET
Sessions.col1 = other_table.col1
FROM
[Database1].[dbo].[Sessions] Sessions
INNER JOIN
[Database2].[dbo].other_table AS other_table
ON
Sessions.id = other_table.id
WHERE Sessions.id = ??
Note if the database is on another server you will need to create a linked server first
http://msdn.microsoft.com/en-us/library/ff772782.aspx
I have created a report in Access and I have written a query for fetching records from
multiple tables as follows:
SELECT BuildingDetails.*, Contractors.Item, ActionDetails.ActionType
FROM Contractors
INNER JOIN (BuildingDetails
INNER JOIN (ActionDetails
INNER JOIN DormData ON ActionDetails.ActionID = DormData.ActionID)
ON BuildingDetails.BuildingID = DormData.BuildingID)
ON Contractors.ID = DormData.ItemID;
Now what I want is only actiontype=repair or actionid=1 get retrieved by the query. We have two actontype "repair" and "replace".
I have reformatted you query a little to neaten it up. You haven't specified what the data looks like for the filter but based on what you have said I would go with something like the following
SELECT BuildingDetails.*,
Contractors.Item,
ActionDetails.ActionType
FROM Contractors
INNER JOIN DormData ON Contractors.ID = DormData.ItemID
INNER JOIN ActionDetails ON DormData.ActionID = ActionDetails.ActionID
INNER JOIN BuildingDetails ON DormData.BuildingID = BuildingDetails.BuildingID
WHERE ActionDetails.ActionType = 'Repair' OR ActionID=1
If ActionID is a lookup column that relates ActionID(1) to ActionType ('Repair') then you don't need the or and can stick to one or other of the conditions in the WHERE Clause.
Hope this helps.
I suspect you only need to filter using actiontype = 'repair' (I further guess that ActionID is an autonumber and you have a row {ActionID = 1, actiontype = 'repair'} only by chance... but this is maybe extrapolating too far :)
I'm surprised #David Steele's answer works in Access (ACE, Jet, whatever) because he's removed the parentheses from the JOIN clauses (however if it does -- suggesting a linked table -- then you should "accept" that answer). But I too could resist 'neatening them up' so that the ON clauses are close to the table names:
SELECT BuildingDetails.*, Contractors.Item, ActionDetails.ActionType
FROM ((DormData
INNER JOIN Contractors
ON Contractors.ID = DormData.ItemID)
INNER JOIN BuildingDetails
ON BuildingDetails.BuildingID = DormData.BuildingID)
INNER JOIN ActionDetails
ON ActionDetails.ActionID = DormData.ActionID
WHERE ActionDetails.ActionType = 'repair';
Add this to the end of your select statement to fix the issue:
where actiondetails.actiontype = 'repair' or actiondetails.actionid = 1