I want to update all values of 'did not see' in a single table to NULL.
I cannot figure out how to write one set statement
This is the code that I would like to write more elegantly
UPDATE MovieSurvey
SET field7 =NULL
where field7 ='Did not see'
UPDATE MovieSurvey
SET field8 =NULL
where field8 ='Did not see'
UPDATE MovieSurvey
SET field9 =NULL
where field9 ='Did not see'
UPDATE MovieSurvey
SET field10 =NULL
where field10 ='Did not see'
UPDATE MovieSurvey
SET field11 =NULL
where field11 ='Did not see'
UPDATE MovieSurvey
SET field12 =NULL
where field12 ='Did not see'
Thanks for the help!
You have to reference each column you want to update, but you could use a case expression in a single update.
update MovieSurvey set
field7 = case when field7 = 'Did not see' then null else field7 end,
field8 = case when field8 = 'Did not see' then null else field8 end,
field9 = case when field9 = 'Did not see' then null else field9 end
... etc
Use the function NULLIF() which will return either NULL if the column's value is 'Did not see' or else the actual value of the column:
UPDATE MovieSurvey
SET field7 = NULLIF(field7, 'Did not see'),
field8 = NULLIF(field8, 'Did not see'),
field9 = NULLIF(field9, 'Did not see'),
field10 = NULLIF(field10, 'Did not see'),
field11 = NULLIF(field11, 'Did not see'),
field12 = NULLIF(field12, 'Did not see')
WHERE 'Did not see' IN (field7, field8, field9, field10, field11, field12);
Related
Is there a way to shorten this update query? I'm new to this and not sure how can I further shorten it
UPDATE [Tbl_SS_Utilization(F03)_Copy1]
SET [WW0] = 0
WHERE [WW0] IS NULL
UPDATE [Tbl_SS_Utilization(F03)_Copy1]
SET [WW0+1] = 0
WHERE [WW0+1] IS NULL
UPDATE [Tbl_SS_Utilization(F03)_Copy1]
SET [WW0+2] = 0
WHERE [WW0+2] IS NULL
UPDATE [Tbl_SS_Utilization(F03)_Copy1]
SET [WW0+3] = 0
WHERE [WW0+3] IS NULL
UPDATE [Tbl_SS_Utilization(F03)_Copy1]
SET [WW0+4] = 0
WHERE [WW0+4] IS NULL
UPDATE [Tbl_SS_Utilization(F03)_Copy1]
SET [WW0+5] = 0
WHERE [WW0+5] IS NULL
I don't know what RDMBS you're using, but your posted SQL looks like T-SQL (for Microsoft SQL Server or Sybase), in that case then this single UPDATE statement should do the trick:
UPDATE
[Tbl_SS_Utilization(F03)_Copy1]
SET
[WW0] = CASE WHEN [WW0] IS NULL THEN 0 ELSE [WW0] END,
[WW0+1] = CASE WHEN [WW0+1] IS NULL THEN 0 ELSE [WW0+1] END,
[WW0+2] = CASE WHEN [WW0+2] IS NULL THEN 0 ELSE [WW0+2] END,
[WW0+3] = CASE WHEN [WW0+3] IS NULL THEN 0 ELSE [WW0+3] END,
[WW0+4] = CASE WHEN [WW0+4] IS NULL THEN 0 ELSE [WW0+4] END,
[WW0+5] = CASE WHEN [WW0+5] IS NULL THEN 0 ELSE [WW0+5] END
WHERE
[WW0] IS NULL OR
[WW0+1] IS NULL OR
[WW0+2] IS NULL OR
[WW0+3] IS NULL OR
[WW0+4] IS NULL OR
[WW0+5] IS NULL
Or with COALESCE (or ISNULL if you're feeling brave):
UPDATE
[Tbl_SS_Utilization(F03)_Copy1]
SET
[WW0] = COALESCE( [WW0] , 0 ),
[WW0+1] = COALESCE( [WW0+1], 0 ),
[WW0+2] = COALESCE( [WW0+2], 0 ),
[WW0+3] = COALESCE( [WW0+3], 0 ),
[WW0+4] = COALESCE( [WW0+4], 0 ),
[WW0+5] = COALESCE( [WW0+5], 0 )
WHERE
[WW0] IS NULL OR
[WW0+1] IS NULL OR
[WW0+2] IS NULL OR
[WW0+3] IS NULL OR
[WW0+4] IS NULL OR
[WW0+5] IS NULL
One update for all.
UPDATE [Tbl_SS_Utilization(F03)_Copy1]
SET
[WW0] = COALESCE([WW0], 0)
, [WW0+1] = COALESCE([WW0+1], 0)
, [WW0+2] = COALESCE([WW0+2], 0)
, [WW0+3] = COALESCE([WW0+3], 0)
, [WW0+4] = COALESCE([WW0+4], 0)
, [WW0+5] = COALESCE([WW0+5], 0)
WHERE ([WW0] & [WW0+1] & [WW0+2] & [WW0+3] & [WW0+4] & [WW0+5]) IS NULL;
test
First of all hi everyone, I am in stuck about my store procedure. Let me tell you what I want and show my code. I have a Configurator table which has IdType, TransactionType and Value. And I have seven different transaction type which are C,G,T,A,X,S,M. My value and IdType column come from UI in procedure. Only I want that to add my table these seven types for each IdType. If there is, then update it.
Here is my created store procedure:
ALTER PROCEDURE [dbo].[spConfTypeTransaction]
(
#IdType INT,
#Value INT,
#TransactionType CHAR(1)
)
AS
BEGIN
DECLARE #CType CHAR
SET #CType = 'C'
DECLARE #GType CHAR
SET #GType = 'G'
DECLARE #TType CHAR
SET #TType = 'T'
DECLARE #AType CHAR
SET #AType = 'A'
DECLARE #XType CHAR
SET #XType = 'X'
DECLARE #SType CHAR
SET #SType = 'S'
DECLARE #MType CHAR
SET #MType = 'M'
IF(SELECT COUNT(*) FROM StockConfigurator where IdType = #IdType and Value = #Value and TransactionType = #CType) = 0
INSERT INTO StockConfigurator
(IdType, Value,TransactionType)
VALUES
(#IdType, #Value,#CType)
ELSE
UPDATE StockConfigurator
SET IdType = #IdType,
Value = #Value,
TransactionType = #TransactionType
IF(SELECT COUNT(*) FROM StockConfigurator where IdType = #IdType and Value = #Value and TransactionType = #GType) = 0
INSERT INTO StockConfigurator
(IdType, Value,TransactionType)
VALUES
(#IdType, #Value,#CType)
ELSE
UPDATE StockConfigurator
SET IdType = #IdType,
Value = #Value,
TransactionType = #TransactionType
IF(SELECT COUNT(*) FROM StockConfigurator where IdType = #IdType and Value = #Value and TransactionType = #TType) = 0
INSERT INTO StockConfigurator
(IdType, Value,TransactionType)
VALUES
(#IdType, #Value,#CType)
ELSE
UPDATE StockConfigurator
SET IdType = #IdType,
Value = #Value,
TransactionType = #TransactionType
IF(SELECT COUNT(*) FROM StockConfigurator where IdType = #IdType and Value = #Value and TransactionType = #AType) = 0
INSERT INTO StockConfigurator
(IdType, Value,TransactionType)
VALUES
(#IdType, #Value,#CType)
ELSE
UPDATE StockConfigurator
SET IdType = #IdType,
Value = #Value,
TransactionType = #TransactionType
IF(SELECT COUNT(*) FROM StockConfigurator where IdType = #IdType and Value = #Value and TransactionType = #XType) = 0
INSERT INTO StockConfigurator
(IdType, Value,TransactionType)
VALUES
(#IdType, #Value,#CType)
ELSE
UPDATE StockConfigurator
SET IdType = #IdType,
Value = #Value,
TransactionType = #TransactionType
IF(SELECT COUNT(*) FROM StockConfigurator where IdType = #IdType and Value = #Value and TransactionType = #SType) = 0
INSERT INTO StockConfigurator
(IdType, Value,TransactionType)
VALUES
(#IdType, #Value,#CType)
ELSE
UPDATE StockConfigurator
SET IdType = #IdType,
Value = #Value,
TransactionType = #TransactionType
IF(SELECT COUNT(*) FROM StockConfigurator where IdType = #IdType and Value = #Value and TransactionType = #MType) = 0
INSERT INTO StockConfigurator
(IdType, Value,TransactionType)
VALUES
(#IdType, #Value,#CType)
ELSE
UPDATE StockConfigurator
SET IdType = #IdType,
Value = #Value,
TransactionType = #TransactionType
END
I don't know how this procedure can be write more simply. Can you please help me?
If I follow you correctly, this can be the solution for you:
ALTER PROCEDURE [dbo].[spConfTypeTransaction]
(
#IdType INT,
#Value INT,
#TransactionType CHAR(1)
)
AS
BEGIN
IF(SELECT COUNT(*) FROM StockConfigurator where IdType = #IdType and Value = #Value and TransactionType IN ('C', 'G', 'T', 'A', 'X', 'S', 'M')) = 0
INSERT INTO StockConfigurator
(IdType, Value,TransactionType)
VALUES
(#IdType, #Value,#CType)
ELSE
UPDATE StockConfigurator
SET IdType = #IdType,
Value = #Value,
TransactionType = #TransactionType
if #TransactionType is always equal to #*Type and since all of your insert/update statements seem to do the exact same thing then all you need is this:
ALTER PROCEDURE [dbo].[spConfTypeTransaction]
(
#IdType INT,
#Value INT,
#TransactionType CHAR(1)
)
AS
BEGIN
IF(SELECT COUNT(*) FROM StockConfigurator where IdType = #IdType and Value = #Value and TransactionType = #TransactionType) = 0
INSERT INTO StockConfigurator
(IdType, Value,TransactionType)
VALUES
(#IdType, #Value,#TransactionType)
ELSE
UPDATE StockConfigurator
SET IdType = #IdType,
Value = #Value,
TransactionType = #TransactionType
END
My Update Trigger :
declare #NewValue NVARCHAR(4000) = '';
declare #OldValue NVARCHAR(4000) = '';
select #OldValue =''
+ CASE WHEN i.Name <> d.Name THEN '"Name": "'+ d.Name +'", ' ELSE '' END+
+ CASE WHEN i.Address1 <> d.Address1 THEN '"Address1": "'+ d.Address1 +'", ' ELSE '' END+
'' FROM
inserted i
JOIN deleted d on (i.AccountId = d.AccountId) ;
select #NewValue = ''
+ CASE WHEN d.Name = NULL OR i.Name <> d.Name THEN '"Name": "'+ i.Name +'", ' ELSE '' END+
+ CASE WHEN d.Address1 = NULL OR i.Address1 <> d.Address1 THEN '"Address1": "'+ i.Address1 +'", ' ELSE '' END+
'' FROM
inserted i
JOIN deleted d on (i.AccountId = d.AccountId) ;
if #NewValue <> '' AND #OldValue <> '' AND #NewValue <> #OldValue
INSERT INTO Auditlog (OtherId, TableName, ActionDate, ActionBy, Operation, ActionType)
SELECT
i.AccountId, 'Accounts', getdate(), '', 'Update', ''
FROM
inserted i
INSERT INTO AuditlogDetails (AuditlogId, OldValue , NewValue)
values (IDENT_CURRENT('dbo.Auditlog'), N'[{'+ #OldValue +'}]', N'[{'+ #NewValue +'}]')
But this trigger just insert Table AuditlogDetails and #NewValue is NULL: for example i have a record name = "abc", address1 = NULL when i update that record Address1 = "adasf" then #NewValue is NULL i expected that #NewValue is [{"Address1": "adasf"}] some one help me pls
Change your NULL comparison
select #NewValue = ''
+ CASE WHEN d.Name = NULL OR i.Name <> d.Name THEN '"Name": "'+ i.Name +'", ' ELSE '' END+
+ CASE WHEN d.Address1 = NULL OR i.Address1 <> d.Address1 THEN '"Address1": "'+ i.Address1 +'", ' ELSE '' END+
'' FROM
inserted i
JOIN deleted d on (i.AccountId = d.AccountId) ;
In the above lines you compared d.Name = NULL and d.Address1 = NULL. Null is something that we don't know. So you can't compare NULL with equality symbol.
Ex:
Declare #var varchar(20) = NULL;
IF (#var = NULL)
BEGIN
PRINT 'Equals work'
END
IF (#var IS NULL)
BEGIN
PRINT 'IS NULL WORK'
END
But the above behavior will change, when we use the property SET ANSI_NULLS (Transact-SQL)
Change it to d.Name is NULL instead. d.Address1 is NULL
you can modify your condition like this
(old.name != new.name) OR (old.name is NULL) OR (new.name is NULL)
I have problem that I have to make my code shorter .
code:
IF #result_var = #expected_value
BEGIN
INSERT INTO reports.consistencycheckhistory VALUES
(
Getdate(),
#rule_guid,
'Ok',
#result_var
)
IF CONVERT(DATE,#check_time) <> CONVERT(DATE, Sysdatetime())
BEGIN
UPDATE reports.consistencycheckrules
SET ok_days_count =#ok_days_count + 1 ,
last_check_time=#check_time
where rule_guid=#rule_guid
END
END
ELSE
BEGIN
INSERT INTO reports.consistencycheckhistory VALUES
(
Getdate(),
#rule_guid,
'Error',
#result_var
)
UPDATE reports.consistencycheckrules
SET ok_days_count=0,
last_check_time=#check_time
WHERE rule_guid=#rule_guid
END
There have to be only 1 insert and 1 update that is what my boss is saying but I don't know if it is possible.
IF #result_var = #expected_value
BEGIN
SET #Status = 'Ok'
END
ELSE
BEGIN
SET #Status = 'Error'
END
IF CONVERT(DATE,#check_time) <> CONVERT(DATE, Sysdatetime())
BEGIN
SET #ok_days_count = #ok_days_count + 1;
END
ELSE
BEGIN
SET #ok_days_count = 0;
END
INSERT INTO reports.consistencycheckhistory VALUES
(
Getdate(),
#rule_guid,
#Status,
#result_var
)
UPDATE reports.consistencycheckrules
SET ok_days_count = #ok_days_count ,
last_check_time=#check_time
where rule_guid=#rule_guid
You could use case statements within insert and within update. Not that it would make it much easier to understand, but would fulfill your boss' wish.
Try this (might need a bit of tweaking):
INSERT INTO reports.consistencycheckhistory
VALUES
(
Getdate(),
#rule_guid,
CASE
WHEN #result_var = #expected_value
then 'Ok'
ELSE 'Error"
END,
#result_var
)
UPDATE reports.consistencycheckrules
SET
ok_days_count =#ok_days_count +
CASE
WHEN CONVERT(DATE,#check_time) <> CONVERT(DATE,Sysdatetime())
then 1
ELSE
0
END,
last_check_time=#check_time
where rule_guid=#rule_guid
In the query there is case where you do not have to update columns, i.e. #result_var = #expected_value and CONVERT(DATE,#check_time) = CONVERT(DATE, Sysdatetime())
DECLARE #Should_Update bit
SET #Should_Update=0
IF #result_var = #expected_value
BEGIN
SET #Status = 'Ok'
END
ELSE
BEGIN
SET #Status = 'Error'
END
IF(#result_var = #expected_value)
BEGIN
IF CONVERT(DATE,#check_time) <> CONVERT(DATE, Sysdatetime())
BEGIN
SET #Should_Update=1
SET #ok_days_count = #ok_days_count + 1;
END
END
ELSE
BEGIN
SET #Should_Update=1
SET #ok_days_count = 0;
END
INSERT INTO reports.consistencycheckhistory VALUES
(
Getdate(),
#rule_guid,
#Status,
#result_var
)
UPDATE reports.consistencycheckrules
SET ok_days_count = #ok_days_count ,
last_check_time=#check_time
where rule_guid=#rule_guid AND #Should_Update=1
IF #result_var = #expected_value
BEGIN
SET #Status = 'Ok'
END
ELSE
BEGIN
SET #Status = 'Error'
END
IF #Status = 'Ok'
BEGIN
IF CONVERT(DATE, #check_time) <> CONVERT(DATE, Sysdatetime())
BEGIN
SET #ok_days_count = #ok_days_count + 1;
END
ELSE
BEGIN
#ok_days_count=#ok_days_count
END
end
ELSE
BEGIN
SET #ok_days_count = 0;
end
INSERT INTO reports.consistencycheckhistory VALUES
(
Getdate(),
#rule_guid,
#Status,
#result_var
)
UPDATE reports.consistencycheckrules
SET ok_days_count = #ok_days_count,
last_check_time = #check_time
WHERE rule_guid = #rule_guid
This seems like the right answer
When I run this it doesn't throw an error however it doesn't insert/delete an entry either. I don't know why it's not working.
DECLARE #trAction VARCHAR(5)
SET #trAction = 'addTR'
IF EXISTS (SELECT TransgeneID
FROM tbl_MT
WHERE MouseID = '3' AND TransgeneID = '3')
IF (#trAction = 'rmvTR')
DELETE
FROM tbl_MT
WHERE MouseID = '3' AND TransgeneID = '3'
ELSE
IF (#trAction = 'addTR')
INSERT INTO tbl_MT
VALUES ('3', '3')
First, put BEGIN and END on the IF's:
DECLARE #trAction VARCHAR(5)
SET #trAction = 'addTR'
IF EXISTS (SELECT TransgeneID
FROM tbl_MT
WHERE MouseID = '3' AND TransgeneID = '3')
BEGIN
IF (#trAction = 'rmvTR')
BEGIN
DELETE
FROM tbl_MT
WHERE MouseID = '3' AND TransgeneID = '3'
END
END
ELSE
BEGIN
IF (#trAction = 'addTR')
BEGIN
INSERT INTO tbl_MT
VALUES ('3', '3')
END
END
Second and final, if the first IF is true, the inner IF (#trAction = 'rmvTR') will fail and nothing will be done.