SQL Server 2008 Merge Soft Delete Error - sql-server-2008

I'm trying to perform a soft delete on a row in my target table using the SQL server 2008 MERGE command.
I think this should fall under the "when not matched by source" section, since the source is missing the row and the target still has it. All I want to do is set the IsActive bit to false, but I'm getting an error.
"Attempting to set a non-NULL-able column's value to NULL."
What am I missing?
The Users table is:
[ID] [nvarchar](50) NOT NULL,
[FirstName] [nvarchar](200) NULL,
[LastName] [nvarchar](200) NULL,
[EmailAddress] [nvarchar](200) NULL,
[IsActive] [bit] NOT NULL
The Merge statement is:
merge into Users
using TempUserTable lu
on Users.ID = TempUserTable.ID
when matched then
update set
ID = lu.ID,
FirstName = lu.FirstName,
LastName = lu.LastName,
EMailAddress = lu.EmailAddress,
IsActive = lu.Status
when not matched then
insert (ID, FirstName, LastName, EmailAddress, IsActive)
values (lu.ID, lu.FirstName, lu.LastName, lu.EmailAddress, lu.Status)
when not matched by source then
update set IsActive = 0;

You can get this to work exactly as you want but for me I needed to add a condition in the NOT MATCHED line.
So try something like...
WHEN NOT MATCHED BY SOURCE
AND TARGET.[IsActive] = 1
AND TARGET.[DeletedOn] IS NULL
THEN UPDATE
SET
TARGET.[IsActive] = 0,
TARGET.[DeletedOn] = SYSDATETIMEOFFSET()

It appears that your temp table TempUserTable either has a NULL in the IsActive column or the ID column.

Related

update only null value columns in table if ID exits in table

I am trying to check for the ID. If it does not exist I want my data to be inserted as a new record in table and if ID exists I want to compare both incoming new record and existing record and only update the values which are null in existing record. I have written the query but I don't know how to check for null in each column:
Here is the query:
insert into ui_table(MDMID,FirstName,Lastname,Mail,Address,City,State,Zip,ProfileStatus,TAID)
select im.MDMID,im.FirstName,im.Lastname,im.Mail,im.Address,im.City,im.State,im.Zip,im.ProfileStatus,im.TAID
from intermediate im on duplicate key update
MDMID = im.MDMID,
FirstName = im.FirstName,
Lastname = im.Lastname,
Mail = im.Mail,
Address = im.Address,
City = im.City,
State = im.State,
Zip = im.Zip,
ProfileStatus = im.ProfileStatus,
TAID = im.TAID;

Report the line manager of your line manager where they have a Manager flag (loops)

CREATE TABLE `empTest` (
`keysearch` INT(8) NOT NULL,
`flg_manager` INT(1) NOT NULL,
`fk_manager` INT(8) NOT NULL,
PRIMARY KEY (`keysearch`)
)
INSERT INTO `empTest` (`keysearch`, `fk_manager`, `flg_manager`) VALUES ('5407', '5866', 0);
INSERT INTO `empTest` (`keysearch`, `fk_manager`, `flg_manager`) VALUES ('5866', '0679', 0);
INSERT INTO `empTest` (`keysearch`, `fk_manager`, `flg_manager`) VALUES ('0679', '9177', 1);
INSERT INTO `empTest` (`keysearch`, `fk_manager`, `flg_manager`) VALUES ('9177', '0011', 1);
In the example data above there are 3 users (4th being the last line manager). I would like to be able to select the "1st keysearch where they have the flg_manager set as 1" as AuthManager (indicating they are an Authorising manager)
The query result I am looking to achieve is:
Keysearch,AuthManager
5407,0679
5866,0679
0679,9177
I am thinking it needs to be a loop but I really don't know where to start. I understand I need to use the middle employee as a Join to be able to see from employee 5407 to employee 0679. Annoyingly in this example there is only one employee to 'jump' but I need to be able to account for up to 8 jumps.
Completely Wrong - but I'm at a complete loss...
SELECT e.keysearch, #manager AS manager
FROM emptest e, emptest m1, emptest m2
WHERE #manager = CASE WHEN (
SELECT e.fk_manager
FROM e
WHERE e.fk_manager = m1.keysearch AND m1.flg_manager = 1)
Else When...
Will I need to do lots of Case loops?
Any Suggestions? Running MariaDB 10.3.

How to combine two SQL queries in one query

I have two queries which are unrelated to each other, first query returns 4 column whereas the second one returns only 1 column.
so how to combine it?
query 1-
$sql = "select postlist.* from postlist order by postlist.id desc ";
query 2-
$sql1 = "select count (commentlist.id) as 'comments',commentlist.id,commentlist.name,commentlist.comment from postlist,commentlist where postlist.id=commentlist.post_id";
current query-
$sql = "select postlist.*, count (commentlist.id) as 'comments' from postlist LEFT JOIN commentlist ON postlist.id=commentlist.post_id order by postlist.id desc ";
Basically, I want to return all records from postlist, whether the commentlist table has any related comments or not.
Here is a database design
drop table if exists postlist;
create table postlist (
id integer not null primary key autoincrement,
post varchar(1000) not null,
name varchar(80) not null,
title varchar(80) not null
);
drop table if exists commentlist;
create table commentlist (
id integer not null primary key autoincrement,
post_id integer not null,
comment varchar(80) not null,
name varchar(80) not null
);
The get() will cast it to a Collection, that is a lot more powerful than an array. You can append it, iterate over it and more.
Have a bash at that. Hopefully it should be what you need: http://laravel.com/docs/4.2/eloquent#collections
$items = DB::select($sql)->get();
$items1 = DB::select($sql1)->get();
$items = items->toArray();
$items1 = items1->toArray();
$items = array_merge($items, $items1);

Storing Percentages in MySQL

I'm trying to store a percentage value in a MySQL database but when ever I try and set the value of the percentage column to 100%, I get an "Out of range value" error message.
I am currently using a DECIMAL(5,2) type and I need to be able to store values from 0% up to 100% (with 2dp when the value isn't an integer) ( the values are being calculated in a php script).
All values are fine apart from 100% which triggers the error.
Am I misunderstanding something or is there something else I am missing?
EDIT: The table was created using the following sql
CREATE TABLE overviewtemplate
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(32),
numberOfTests INT NOT NULL DEFAULT 0,
description VARCHAR(255) NOT NULL DEFAULT "Please add a Description",
percentageComplete DECIMAL(5,2),
numberPassed INT NOT NULL DEFAULT 0,
numberFailed INT NOT NULL DEFAULT 0
) ENGINE=MYISAM;
EDIT 2: This is the code of the SQL query
$numberOfPasses = 5;
$numberOfFails = 5;
$percentageComplete = 100.00;
$sqlquery = "UPDATE `overviewtemplate`
SET numberPassed = {$numberOfPasses},
numberFailed = {$numberOfFails},
percentageComplete = {$percentageComplete}
WHERE description = '{$description}'";
EDIT 3: FIXED - Had a syntax error in my table names which meant it was trying to update a wrong table.
With your declaration you should be able to save even 999.99 without trouble. Check if you have set any rule for it not be bigger than 100? If yes then set it to be less than or equal to 100.00
It could be in a trigger.

SQL: How can I update a value on a column only if that value is null?

I have an SQL question which may be basic to some but is confusing me.
Here is an example of column names for a table 'Person':
PersonalID, FirstName, LastName, Car, HairColour, FavDrink, FavFood
Let's say that I input the row:
121312, Rayna, Pieterson, BMW123d, Brown, NULL, NULL
Now I want to update the values for this person, but only if the new value is not null, Update:
121312, Rayna, Pieterson, NULL, Blonde, Fanta, NULL
The new row needs to be:
121312, Rayna, Pieterson, BMW123d, Blonde, Fanta, NULL
So I was thinking something along the lines of:
Update Person(PersonalID, FirstName, LastName, Car, HairColour,
FavDrink, FavFood) set Car = #Car (where #Car is not null), HairColour
= #HairColour (where #HairColour...)... etc.
My only concern is that I can't group all the conditions at the end of the query because it will require all the values to have the same condition. Can't i do something like Update HairColour if #HairColour is not Null
Id use coalesce for this:
http://msdn.microsoft.com/en-us/library/ms190349.aspx
update Person
set Car = coalesce(#Car, Car), HairColour = coalesce(#HairColour, HairColour)
The following should work:
UPDATE Person
SET Car = ISNULL(#Car, Car),
HairColour = ISNULL(#HairColour, HairColour),
...
It uses the SQL Server ISNULL function, which returns
the first value if it is non-null,
or, otherwise, the second value (which, in this case, is the current value of the row).
You can use the isnull function:
update Person
set
Car = isnull(#Car, Car),
HairColour = isnull(#HairColour, HairColour),
FavDrink = isnull(#FavDrink, FavDrink),
FavFood = isnull(#FavFood, FavFood)
where PersonalID = #PersonalID
Set the column equal to itself with an isnull round it setting it to your parameter.
UPDATE
YourTable
SET
YourColumn = ISNULL(YourColumn, #yourParameter)
WHERE
ID = #id