I have a col1 in my table with initial value of 0 with one row, I want to get updated value of col1.
My update query is :
Update table set col1 = col1 + 1
I can get last update by put output like :
Update table set col1 = col1 + 1 OUTPUT inserted.col1
But I am not sure that output value is related same query or last updated by other query at same time.
The value returned is the value of this update statement. It will not reflect updates made by other users.
EDIT:
You can also store the value in a variable without an OUTPUT clause (https://msdn.microsoft.com/en-us/library/ms177523.aspx):
Update table set #col1 = col1 = col1 + 1;
Related
I have multiple null values in a table including different rows and columns, how I can replace them with one query like using 'all" or *, wherever the Null values in the whole table?
Check if column value is null,if yes update with blank values like below code
Update TableName
SET TableName.Column=''
WHERE TableName.Column IS NULL
For Multiple Columns
UPDATE TableName
SET col1 = COALESCE(col1,''),
col2 = COALESCE(col2,''),
col3 = ...
Coln = COALESCE(coln,'')
In Oracle: ='' and 'is null' return NULL Values
SELECT COL1 FROM TABLE WHERE COL1='' --> RETURN COL1 WITH NULL VALUES
SELECT COL1 FROM TABLE WHERE COL1 IS NULL --> RETURN COL1 WITH NULL VALUES
Both queries return the same set of rows.
In Microsoft SQL:
SELECT COL1 FROM TABLE WHERE COL1='' --> RETURN COL1 WITH <BLANK> VALUES
SELECT COL1 FROM TABLE WHERE COL1 IS NULL --> RETURN COL1 WITH <NULL> VALUES
Here the first and second queries return different sets of rows.
Why is there a difference between the Microsoft SQL result set and the Oracle result set?
How could I retrieve both set of rows in Microsoft SQL? (Values which are '' and values which are NULL).
In SQL Server '' is not null. In Oracle its a bit complicated x char := '' is not null in PL/SQL but as varchar2 empty string '' is exactly same as null as explained here And in case of queries this is varchar2 so condition COL = '' is never true.
If you need condition that will work in both DBs please use coalesce function.
select * from TAB
where coalesce(COL,'SOME_UNIQUE_STRING') = coalesce(FILTER_PARAM, 'SOME_UNIQUE_STRING')
Such condition should return rows when COLUMN is equal to FILTER as well as both are null or empty strings.
To retrieve both sets of data, you could use isnull.
SELECT COL1 FROM TABLE WHERE ISNULL(COL1, '') = ''
Oracle automatically converts empty strings to null values, and sql server does not, which is why the result sets are different.
You can find more information on this here:
Why does Oracle 9i treat an empty string as NULL?
To avoid defining a unique string, you could use a case statement.
This way we evaluate col when null or empty set to the same value for comparison. Since case to my knowledge is DBMS independent this should work in both.
SELECT *
FROM Table
WHERE case when col is NULL or col = '' then 1 else 0 end = 1
I want to insert #id value into Items table as auto increment.
Table Name= Items
Columns= Code, Name.
The column Code does not allow null value and is unique but it has no auto increment,
I tried to write query which will fill value (1,2,3,4,...) in column of Code as auto increment but it does not work
This is my query
DECLARE #id INT
SET #id = (select MAX(Code) from Items)
SET #id =#id+1
insert into Items (Code,Name) values(#id,'m')
This is the error
Msg 515, Level 16, State 2, Line 6
Cannot insert the value NULL into column 'Code', table 'Items'; column does not allow nulls. INSERT fails.
The statement has been terminated.
I want #id to be inserted in column of Code as auto increment.
Please anyone can help me.
Can I assume your table is empty?
In this case,
SET #id = (select MAX(Code) from Items)
is NULL,
so
SET #id =#id+1
is also null.
The correct code would be:
SET #id = (select ISNULL(MAX(Code),0) from Items)
which will return numeric 0 when there are no rows.
Typical beginner SQL error - be aware of the "poisonous" nature of NULL in SQL, which turns everything it touches into - NULL.
If you use #id you risk it changed between the two statements
Since below is one statement you don't need to wrap it in a transaction
insert into Items (Code, Name)
select MAX(Code) + 1, 'm'
from items
And I think this deals with empty items
insert into Items (Code, Name)
select isnull(MAX(Code), 0) + 1, 'm'
from items
I have created a view using 2 columns from my Employee table like this
Create VIEW [dbo].[vwTestEmp] AS
select
EMpID,
case when (Address1 IS null) then null
when (Address1 IS not null) then Address1
end as "EMpAddress",
'' as "Date_Verified"
FROM Employee;
Now Date_Verified is not in the Employee table, so is a derived column in the view and contains null. After manual verification of certain processes, I want to call
update [dbo].[vwTestEmp]
set Date_verified = GETDATE()
where EMpid = 60;
I am getting an error
Update or insert of view or function 'vwTestEMp' failed because it contains a derived or constant field.
How to handle this error?
THanks
MR
You cannot update the view through the constant value for Date_Verified. Change the query to:
CREATE VIEW [dbo].[vwTestEmp1] AS
SELECT EMpID, Address1 AS EMpAddress], [Date_Verified]
FROM EMployee;
This avoids the unnecessary CASE and replaces the constant value for the date column with the underlying column.
This updates as shown in the SqlFiddle.
You have an error. The correct syntax is:
UPDATE < view_name > SET<column1>=<value1>,<column2>=<value2>,... WHERE <condition>;
Use a table.
select
EMpID,
Address1 as "EMpAddress",
'' as "Date_Verified"
INTO [dbo].[tblTestEmp]
FROM Employee;
update [dbo].[tblTestEmp]
set Date_verified = GETDATE()
where EMpid = 60;
My question has to do with the order of updates in a single update statement. I have observed that when I set variables using a SELECT statement, that the variables are set in order. For example:
SELECT
#Test1 = 2,
#Test2 = #Test1 + 1
At this point #Test1 will be 2 and #Test2 will be 3 because the set operations are done in order. But what happens with UPDATE?
UPDATE TestTable SET
Test1 = 2,
Test2 = Test1 + 1
Will Test2 use the initial value of Test1 in its calculation or will it use the value we just set? Does it make any difference if it is an UPDATE statement inside of a MERGE?
MERGE INTO TestTable AS T
USING (
SELECT
Test1,
Test2
FROM SomeOtherTable
) AS S
ON S.Test1 = T.Test1
WHEN MATCHED THEN
UPDATE SET
T.Test1 = 2,
T.Test2 = T.Test1 + 1
;
The names on the right hand side of the assignment refer to the old values of the columns, regardless of the order they are in.
This (for example) allows you to swap two values without using a temporary variable.
UPDATE foo
SET a = b,
b = a
http://sqlfiddle.com/#!3/f6984/1
The SQL-92 specification (Section 13.10, General Rules, Item 6 on page 395) states:
The <value expression>s are effectively evaluated for each row of T before updating any row of T.