In a query on a DB2 v10 for z/os, I need to pull data from live production lines, so I can't have any lock put on them. Accuracy in this situation is second to locking up data, as locking data would be very bad for production. My goal is to do something like this:
INSERT INTO newTable
SELECT a, b, c
FROM (SELECT d, e, f
FROM tableB b
FOR FETCH ONLY WITH UR)
I've tried all that I can think of doing, but it seems like whenever i put code inside of the FROM statement, it gives me an error on the "FOR FETCH ONLY WITH UR" even though it hasn't before when the same code is in other places. The error I get when I put "FOR FETCH ONLY WITH UR' or even just "WITH UR" is usually SQLCODE -199 "ILLEGAL USE OF FOR/WITH....."
I've also tried doing something like this:
INSERT INTO newTable
WITH tableA AS (
SELECT d, e, f
FROM tableB b
FOR FETCH ONLY WITH UR
)
SELECT a, b, c
FROM tableA
but I so far haven't found a way to specify WITH UR or to not lock data in tableB, while at the same time being able to use it for processing and putting it into newTable. What am I doing wrong and is it even possible to do what I want to do?
Thanks in Advance
Related
I can write simple queries in R using DBI like:
SELECT id, class, var3
FROM tableA
These queries work without a problem, but I'd like to write more complex queries that pull from multiple tables using dot notation such as:
SELECT a.id, a.class, b.id, b.rank
FROM tableA AS a, tableB AS b
WHERE a.id=b.id
When I do the second one in R I get an error saying that the SQL command was not properly ended. Does anyone know a way to accomplish this?
Exactly as the title says.
I have an MS Access database. I didn't create it, I was handed it with a request to update a query. My task was to add a column with a calculated result, and we added a new column... essentially lets say we have query:
SELECT DISTINCT a, b, c
FROM tbl1
Modified with a UDF to be:
SELECT UDF(d) as e, *
INTO tb2
FROM
(SELECT DISTINCT a, b, c, d FROM tbl1)
We did this, and got different answers, 3 more rows out of hundreds of thousands. I assumed simply that column d must contain more information and after removing duplicates by the DISTINCT clause we ended up with more columns.
In attempting to verify this, we discovered that the order of the columns seemed to matter. If we did this:
SELECT UDF(d) as e, *
INTO tb2
FROM (SELECT DISTINCT a, c, b, d FROM tbl1)
The extra columns went away.
I thought at first that I was using distinct wrong, but checking different syntax combinations resulted in errors, and looking up the SQL documentation for access said that this was correct. A colleague suggested that we needed a GROUP BY clause but since we're not doing any aggregation and we want the globally unique records that doesn't seem to fit.
What am I missing?
I have two tables: A, B. I need to select all data from B.
I could do this like
SELECT id, b1, b2, ... b20 FROM A,B WHERE A.id = B.id;
it's not cool solution. I will need to update this statement if I modify B's database. Is exist something like ?
SELECT *(B)
It selected all data from B and didn't selected any data from A.
My databases
A
id
a1
a2
...
a20
B
id
b1
b2
...
b20
So if you want create database seriously you shouldn't see on complexity of way but on efficiency and speed. So my advice to you is use JOIN that is the best solution for selecting data from two and more tables, because this way is fast as possible, at least for my more cleaner like for example inserted select.
You wrote: I need to select all data from B this means SELECT * FROM B not that you wrote.
My advice to your is using this:
SELECT * FROM A <INNER / LEFT / RIGHT / NATURAL> JOIN B ON A.id = B.id;
or to select specific columns
SELECT A.column1, A.column2, ... FROM A <INNER / LEFT / RIGHT / NATURAL> JOIN B ON A.id = B.id;
Note:
NATURAL JOIN will work in above example since the primary key
and the foreign key in the two tables have the same name. So you must be very
careful in using NATURAL JOIN queries in the absence of properly
matched columns.
So you really should think about how you will create database and how you will working with database, how you will pulling data for View from database, how you will insert new potential data to database etc.
Regards man!
Use following query:
SELECT * FROM B;
or
SELECT * FROM B INNER JOIN A ON A.id = B.id;
if you want to join tables A and B.
I suspect that the others have sufficiently answered your question about selecting all fields from table B. That's great, as you really should understand the SQL basics. If they haven't, I'd also advise that you check out SQLite.org site for a clarification on SQL syntax understood by SQLite.
But, assuming you've answered your question, I just want to voice two words of caution about using the asterisk syntax.
First, what if, at some later date, you add a column to B that is a big hairy blob (e.g. a multimegabyte image). If you use the * (or B.*) syntax to retrieve all of the columns from B, you may be retrieving a ton of information you might not need for your particular function. Don't retrieve data from SQLite if you don't need it.
Second, is your Objective C retrieving the data from your select statement on the basis of the column names of the result, or based upon the index number of the column in question. If you're doing the latter, then using the * syntax can be dangerous, because you can break your code if the physical order of columns in your table ever changes.
Using named columns can solve the problem of memory/performance issues in terms of retrieving too much data, as well as isolating the Objective C from the physical implementation of the table in SQLite. Generally, I would not advise developers to use the * syntax when retrieving data from a SQL database. Perhaps this isn't an issue for a trivial project, but as projects become more complicated, you may want to think carefully about the implications of the * syntax.
I don't know if the following query would work in sqlite, I know it works in Oracle, but you can give it a try...
SELECT B.* FROM A,B WHERE A.id = B.id;
I'm working with three tables, and for simplicity's sake let's call them table A, B, and C. Both tables A and B have a column called id, as well as one other column, Aattribute and Battribute, respectively. Column c also has an id column, and two other columns which hold values for A.id and B.id. Now, in my code, I have easy access to values for both Aattribute and Battribute, and want to delete the row at C, so effectively I want to do something like this:
DELETE FROM C WHERE aid=(SELECT id FROM A WHERE Aattribute='myvalue') AND bid=(SELECT id FROM B WHERE Battribute='myothervalue')
But this obviously doesn't work. Is there any way to make a single complex query, or do I have to run three queries, where I first get the value of A.id using a SELECT with 'myvalue', then the same for B.id, then use those in the final query?
[Edit: it's not letting me comment, so in response to the first comment on this: I tried the above query and it did not work, I figured it just wasn't syntactically correct. Using MS Access, for what it's worth. ]
You must use IN instead of =.
DELETE
FROM C
WHERE aid IN
(SELECT id
FROM A
WHERE Aattribute='myvalue'
)
AND bid IN
(SELECT id
FROM B
WHERE Battribute='myothervalue'
)
For example I have the following tables resulting from:
CREATE TABLE A (Id int, BId int, Number int)
CREATE TABLE B (Id int, Number decimal(10,2))
GO
INSERT INTO A VALUES(1, 3, 10)
INSERT INTO B VALUES(3, 50)
INSERT INTO A VALUES(2, 5, 20)
INSERT INTO B VALUES(5, 671.35)
GO
And I run the following query multiple times:
SELECT * FROM A INNER JOIN B ON A.BId = B.Id
I should get something like:
ID BId Number ID Number
1 3 10 3 50.00
2 5 20 5 671.35
But is it possible for A.Number and column B.Number be in different position (also ID in that respect) so I'll get something like:
ID Number ID BId Number
3 50.00 1 3 10
5 671.35 2 5 20
We are currently experiencing some weird problem that might be resulting from something like this. We have an ASP.NET application, executing a custom reflection based code generated data mapper that is connecting to SQL Server 2008 cluster.
We found sometimes that we get an error like so:
Object of type 'System.Decimal' cannot be converted to type 'System.Int32'
Trying to figure out if this is a behaviour in SQL Server or it's something in the reflection based data mapper engine.
As you can see the field names are the same in the two tables. Thinking perhaps when we tried to do DataReader.GetValue(DataReader.GetOrdinal("Number")), it will return B.Number which is a decimal instead of A.Number which is an int.
To complicate the matter further, this only happen intermittently (but consistently after it happened once on a particular IIS web server in a web farm only).
Say Web Server A is doing okay up until 2:00pm and suddenly, we got this error and we'll keep getting that error on Web Server A until we reset IIS on that server and then it will be okay again.
Something to do w/ connection pooling and how SQL query plan cache perhaps?
The second scenario is possible only if you interchange table orders.
Something like SELECT * FROM B INNER JOIN A ON A.BId = B.Id.
Otherwise its not possible.
SQL is a relational algebra - the standard does not specify what order columns will be returned in if you don't explicitly state the order yourself.
I tend to avoid "select *" as much as possible since it can clog up the network with unnecessary traffic and makes it harder to catch things like column renames and ordering until it's too late.
Just select the columns you actually need.
For your specific case, I would also just return the shared ID once since it has to be equal due to your join (I tend to prefer the "old" style as the DBMS should be smart enough to optimize this to an inner join anyway):
select
a.Id as Id,
a.BId as BId,
a.Number as Number,
b.Number as BNumber
from
a, b
where
a.BId = b.Id
The second scenario is possible only if you interchange table orders.
Order of joins has nothing to do with position of columns, except if you use simple SELECT * which is not recommended.
Even in that case you can use
SELECT B., A. without changing order of joins
The best and recommended solution is to put column names instead of * (as posted in 1st two answers)
-- added later
I forgot one more thing I wanted to point on: Use column aliases for the columns with the same names e.g.
Select A.Number as NumberA, B.Number as NumberB
I don't think I've ever seen it swap the order, but personally I don't use "SELECT *" in production code, either:
SELECT A.[Id], A.[Bid], A.[Number], B.[Id], b.[Number]
FROM A INNER JOIN B ON A.[BId] = B.[Id]
Or at worst:
SELECT A.*, B.* FROM A INNER JOIN B ON A.[BId] = B.[Id]