Mapping Query SQL Server 2008 R2 - sql

I have to map between to tables and update locations using is their anyway using SQL to do this in one query my structure is as follows. The table are as follows mapping table below
Then the detail table is
Is their a way in sql I can update location in the detail table where it equals animal store id and then replace it with geministore id My SQL is very basic so why not provided an example of what I tried as dont no. Is it possible to do this in one query?

I believe this should accomplish what you want, but as it will change data be sure to do a backup to ensure you can undo the changes if they're incorrect. Also, since you didn't specify the table names I assumed they're namedlocationsanddetailwhich is probably incorrect so you'll have to adjust the names.
UPDATE detail
SET location = locations.GeminiStoreID
FROM detail
INNER JOIN locations ON locations.AnimalStoreID = detail.location

Related

Creating a new table along with join statements in Run SQL in IBM dashDB

I'm new with SQL and I'm trying to create a new table after joining two tables with different fields except for the primary id. I saw online that it is possible, but IBM's dashDB's Run SQL doesn't seem to like it as it marks it as "'with_data_or_definitions_only' expected after this token" right after the parenthesis. I'm not entirely sure what exactly this error is and how I can fix it as after looking over it online, my code's almost similar in syntax. I'm thinking that dashDB might have a different syntax for creating a table along with a full outer join statement, but I'm not entirely sure what it is if that's the case.
CREATE TABLE SPSS as (SELECT SPSS_INPUT_TABLE.*, DIG_HISTORY.REPAIR_TYPE_DETAILS,
DIG_HISTORY.DIG_COMPLETION_YEAR
FROM SPSS_INPUT_TABLE FULL OUTER JOIN
DIG_HISTORY ON SPSS_INPUT_TABLE.DISTANCE__M_=DIG_HISTORY.ILI_CHAINAGE
ORDER by SPSS_INPUT_TABLE.DISTANCE__M_);
Just append "WITH DATA" after the parenthesis. This tells dashDB to create the table and fill it with the data from the result set of your join. Alternatively when you specify "DEFINITION ONLY" it will only take the schema of the result set to define the new table but will not copy the result set content in.

MS Access SQL update query based on another query

My DB has existing data. I'm trying to implement some sort of security rights system. It doesn't need to be truly secure...just limit what each level can effectively change. Technically...one exists...but it needs to be beefed up.
I need to adjust the existing Rights level for people that are Instructors. I have a query (qInstructors) that lists a DISTINCT query of anyone in the class table listed as an Instructor. There are a total of 38 Instructors.
Now I need to update the User table to adjust the rights of those 38 people...and this is where I'm stuck. A simple update query, no problem. But I must not be searching with the correct term because I can't find anything to help me hammer out the SQL.
UPDATE tblUserList
INNER JOIN tblUserList ON tblUserList.NTID = qInstructors.Instructor
SET tblUserList.Rights = 2
WHERE [NTID]=[Instructor];
When I try to run this, I get a syntax error in the JOIN. This is beyond my SQL knowledge...any leads?
I would suggest doing this using IN:
UPDATE tblUserList
SET tblUserList.Rights = 2
WHERE [NTID] IN (SELECT [Instructor] FROM qInstructors);
The use of JOIN in an UPDATE clause varies by database. The IN version is ANSI standard and should work both in MS Access and in whatever back-end database you might be using.
You were specifying the tblUserlist instead of qinstructors in the join clause.
UPDATE tblUserList
INNER JOIN qInstructors ON tblUserList.NTID = qInstructors.Instructor
SET tblUserList.Rights = 2
Create a query in Design View. Change the type to "Update Query".
Add your target table to the query, and add your existing "Give me instructors" query as well.
Drag a line from the ID in your target table to the corresponding ID returned by your Query.
Drag the field you want to update down to the grid at the bottom. In the "Update To" field, enter "Allowed" or "True" or whatever indicates that something is allowed for instructors.

Adding a Computed column with After clause

I am trying to create a computed column after a specified column.
But the SQL gives syntax error for the query below. Please help me with the correct syntax/way to do this.
ALTER TABLE service_ServiceClass
ADD LichenClassName AS ([dbo].[UfnGetServiceClassName]([Id])) AFTER Description
You get an error because "AFTER" is MySQL syntax.
There is no direct way to alter column order in SQL Server currently.
In MS SQL Server Management Studio, you'd have to use the SSMS Table Designer (rightclick yourTable->Design) to arrange your columns, which can then generate a script which drops and recreates the table.
There is no after clause in sql server's alter table statement.
The order of the columns in the table (as well is the order of the rows, btw) is completely insignificant from a user's point of view.
It's significant from the server point of view, as it may effect performance when using clustered indexes.
The only time the order of the columns matters is when you are using select * ..., but you shouldn't use that anyway. Always specify the column names directly.

MS Access SQL (Quickbooks) Update Query

I'm trying to create an Update Query in MS Access (2013) to a QuickBooks Database using QODBC.
I need to update the table PriceLevelPerItem. I am trying to update the field in said table called PriceLevelPerItemCustomprice with a value from another table, QueryThreeTable, and a column titled UpdatedPrice.
I need to update the table PriceLevelPerItem where the PriceLevelPerItemItemRefListID matches the value of ItemID from QueryThreeTable and ListID matches the QueryThreeTable.ItemListID (yes I know these are the wrong way around...)
So far this process has been a very annoying trial of many queries and any help would be greatly appreciated
This is what I've been working with:
UPDATE
PriceLevelPerItem
SET
(PriceLevelPerItemCustomPrice = QueryThreeTable.UpdatedPrice)
FROM
QueryThreeTable, PriceLevelPerItem
WHERE
QueryThreeTable.ItemID = PriceLevelPerItem.PriceLevelPerItemItemRefListID
AND
QueryThreeTable.ItemListID = PriceLevelPerItem.ListID;
I think the problem is that you're trying to use a DAO query inside a QODBC query. I think the two use different Data Access engines.
You're going to need to lookup your UpdatedPrice in your QueryThreeTable using DLookup. Or maybe you need to create a DAO loop using QueryThreeTable that then updates values in your QODBC table from there.
Make your QODBC query work without the use of QueryThreeTable and without any joins. Then come up with a way to dynamically create your query. You're resulting SQL should look something like this:
UPDATE
PriceLevelPerItem
SET
PriceLevelPerItemCustomPrice = 150.16
WHERE
PriceLevelPerItem.ListID = '310000-1146238368';

Create dynamic table from SQL Query

Using a hybrid Access 2010 / SQL Server 2012 platform - (a solution in either application will work)
I have a table created with a Select Into ... statement.
Is there any way to have this table dynamically update itself (using the query that created it) whenever its data is accessed?
Specifically, we want to keep a list of customers with only one order (non-repeat customers). I have created a table that contains the ID of those customers (WHERE COUNT(orderID) = 1) using Select Into, but if one of those customers makes a new order, or a new customer who makes one order is created, then I want that data removed/added to the table dynamically.
So, is this possible, or do I have to remember to update the table myself whenever I use it?
I have a table created with a Select Into ... statement. [...] Is there any way to have this table dynamically update itself (using the query that created it) whenever its data is accessed?
What you've described is a SQL VIEW, also called a "(saved) SELECT Query" in Access. A View is a virtual table that dynamically retrieves its information from other tables (or views) each time it is accessed. The view does not save its results between calls, so each time you reference it you get the most current data. See if you can use a VIEW (in SQL Server) or a saved SELECT Query (in Access) in place of the temporary table you are currently creating.