Sequential update statements - sql

When using multiple SETs on a single update query like
update table set col1=value1,col2=col1
is there an order of execution that will decide the outcome, when the same column is left or right of an equals sign? As far as I've tested so far, it seems when a column is used to the right of an equals as a data source, then its value is used from BEFORE it gets a new value within the same update statement, by being to the left of an equals sign elsewhere.

I believe that SQL Server always uses the old values when performing an UPDATE. This would best be explained by showing some sample data for your table:
col1 | col2
1 | 3
2 | 8
3 | 10
update table set col1=value1,col2=col1
At the end of this UPDATE, the table should look like this:
col1 | col2
value1 | 1
value1 | 2
value1 | 3
This behavior for UPDATE is part of the ANSI-92 SQL standard, as this SO question discusses:
SQL UPDATE read column values before setting
Here is another link which discusses this problem with an example:
http://dba.fyicenter.com/faq/sql_server/Using_Old_Values_to_Define_New_Values_in_UPDATE_Statements.html
You can assume that in general SQL Server puts some sort of lock on the table during an UPDATE, and uses a snapshot of the old values throughout the entire UPDATE statement.

Related

db2 snapshot at an instance

I have a requirement where I would like to get the snapshot od data at an instance in database.
For Eg:
At a given time - T1, EMP table in DB has following values
Col 1 Col2
1 ABC
2 DEF
3 GHI
However that data has been modified by another resource/s.
So when I checked at time T2
Col 1 Col2
1 LMN
2 PQR
3 XYZ
Is there any command available in DB2/ORACLE or any database where if, I provide Time stamp,
I can retrieve the state of Data at that timestamp??
Thanks
Both DB2 and Oracle allow you to do that by issuing queries similar to
SELECT * FROM EMP AS OF <timestamp> WHERE ...
In DB2 the table must be set up as a system temporal table (as described here, for example) before you can query it like that.
In Oracle the entire database must be enabled for flashback.
In either case the corresponding feature must be enabled before the data change to allow you to query the original state of data. You cannot make the table EMP system temporal today and query its yesterday's state; you had to enable the feature yesterday.

SQL Command to copy data from 1 column in table and 1 column in another table into a new table?

I had to make a new table to get the Include statement working in Entity Framework since EF was looking for a table called be_PostTagbe_Posts. I was using EF Code First from DB. But now the question is about SQL. I added one row of data and now the include works. But what I am looking for is a SQL command that can copy data from 1 column in 1 table and 1 column in another into the new be_PostTagbe_Posts table. In the be_Posts table I need the data in PostRowID to go into be_Posts_PostRowID and PostTagId to go into be_PostTag_PostTagID. Both be_PostTag_PostTagID and be_Posts_PostRowID are in the new be_PostTagbe_Posts table. I am not very good with SQL so not sure how to do this.
Edit: Thanks for the answers. I tried 2 separate queries but only data was inserted into the the be_PostTag_PostTagID while be_PostTag_PostRowID remained null.
And I tried this query which returned The multi-part identifier "be_PostTag.PostID" could not be bound.
INSERT INTO be_PostTagbe_Posts(be_PostTag_PostTagID, be_Posts_PostRowID)
SELECT be_PostTag.PostTagID, be_Posts.PostRowID
WHERE be_PostTag.PostID = be_Posts.PostID
EDIT:
This only inserted half the data - even 2 inserts leave one column null
INSERT INTO be_PostTagbe_Posts (be_Posts_PostRowID)
SELECT PostRowID FROM be_Posts;
INSERT INTO be_PostTagbe_Posts (be_PostTag_PostTagID)
SELECT PostTagID FROM be_PostTag;
And yet management studio tells me the query executed successfully but one column is still null. Weird.
Here are screenshots of the tables:
SELECT PostTagID AS be_PostTag_PostTagID, PostRowID AS be_Posts_PostRowID
INTO be_PostTagbe_Posts
FROM be_PostTag
Inner JOIN be_Posts
ON be_PostTag.PostID=be_Posts.PostID
That command created the new table with the 2 columns populated.
If i understand you ,you want to Copy Table Z's Column A to Table X And Table Z's Column B to Table Y.
If it is so, According to your question it is not clear about Table Structure of TableX and TableY
Assuming TableX And TableY to single ColumnTable [Apart from IdentityColumn] our query will be
INSERT INTO TableX
SELECT ColumnA FROM TableZ
INSERT INTO TableY
SELECT ColumnB FROM TableZ
Rest put your Entire Structure of Table To Get More Help Because These query are on Assumptions
There's not enough information in your question to give you a working example, but this would be the general syntax for INSERTing into a different table using a query SELECTing from two other tables.
INSERT INTO destination_table(wanted_value_1, wanted_value_2)
SELECT table_1.source_field_1, table_2.source_field_1
WHERE table_1.matching_field = table_2.matching_field
There has to be some sort of relationship between the two tables for the WHERE clause to work in that statement. I'm guessing based the little information you provided that there is a PostRowID field somewhere in the table that contains the tags such that your data would look similar to this in the tag table:
PostRowID PostTagID
--------- ---------
1 1
1 2
1 3
1 4
2 1
2 2
3 3
4 4
It sounds like you should use two sql statements:
Insert into `be_PostTagbe_Posts` (`be_PostTag_PostTagID`)
select `PostTagID` from POSTTAGIDTABLE
and
Insert into `be_PostTagbe_Posts` (`be_Posts_PostRowID`)
select `PostRowID` from POSTTAGIDTABLE
unless the items have some sort of relationship, then if you have a select statement that will select the merged data in two columns you can just do
Insert into `be_PostTagbe_Posts` (`be_PostTag_PostTagID`,`be_Posts_PostRowID`)
(select statement that selects the two items)

SQLite table with some rows missing a column

I have a table in a SQLite database that looks something like this, but with more columns and rows:
| Field1 | Field2 |
|---------|---------|
| A | 1 |
| B | 2 |
| C | |
What I need to do is run a SQL query like this:
SELECT * FROM <tablename> WHERE <conditions> ORDER BY Field2
The problem is, I'm getting the error: no such column: Field2
So now I've been asked to set all the missing values to 99. But when I run
UPDATE <tablename> SET Field2='99' WHERE Field2 IS NULL;
I get the same error. How do I fix this and update all those missing cells?
EDIT: I should also add that the missing values don't seem to be null since if I add a new column in my database GUI browser, all the cells show as [NULL], though this column doesn't.
This turned out to be caused by a very subtle problem in the table:
Several of the column names (the ones that were causing me problems) ended in a newline (\n). Removing the newline solved all my problems!

Update one row only out of two identical rows in sybase

I have three rows in the database out of those two are identical. Out of those two same rows I have to make changes in one using the sybase.Ex.
Row1: ABC 456 ancient block
Row2: ABC 456 ancient block
Row3: DEF 678 class block
I have to make changes in one of the first two block by changing ABC to XYZ.If there are only two identical blocks then I am doing the below method.
begin transaction AA
set rowcount 1
update table
set col1 = XYZ
where col1 = ABC
commit transaction AA
set rowcount 0
It is easy if there are two identical rows but if two identical and one different then sybase picks the unique row and updates it. Can someone tell how to solve this three rows problem ?
I am using aseisql for the Sybase.
Have you tried:
update top 1 table
set col1 = XYZ
where col1 = ABC
This is the solution I figured.
begin transaction a
set rowcount 2
update table
set col1 = XYZ
where col4 = block
commit transaction a
set rowcount 0
It will update one of the duplicate rows and the unique row. Then I will update the unique row to its original value separately using the update statement. I know its sound crude but no one has answered satisfactorily.

Delete the column for the particular value

Using Sql Server 2005
Table1
ID Name Value
001 Rajesh 90
002 Suresh 100
003 Mahesh 200
004 Virat 400
...
I want to delete the value from the table1 for the particular id
Tried Query
Delete value from table1 where id = '001'
The above query is not working.
How to make a delete query for delete the particular column
Need Query Help
There are at least two errors with your statement:
The word table will give a syntax error because it is a reserved word. You need to specify the table name of the specific table you wish to delete from.
Also you cannot write DELETE value FROM. It's just DELETE FROM. And note that it deletes the entire row, not just a single value.
A correct delete statement would look like this:
DELETE FROM table1
WHERE id = '001'
However if you want to change a single value to NULL you should use an UPDATE statement.
UPDATE table1
SET value = NULL
WHERE id = '001'
Of course this assumes that the column is nullable. If not, you'll have to fix that first. See this question for details:
Altering a column to be nullable
I think you want to set the value to null
update Table1 set value=NULL where id='001'