I am using an Insert trigger on a table where I copy one record from one table to another.
Everything is ok. It works fine except on datetime columns. When I add a datetime column into the inserted values, then I get an error:
Invalid column name
My code is
ALTER TRIGGER [dbo].[Insert_MoinitoringBasic]
ON [dbo].[M0_BasicInfo]
FOR INSERT
AS
BEGIN
MERGE [MonitoringROSCII].[dbo].[MonitorBasicInfo] AS d
USING (SELECT DistrictID, upazilaID, LC_ID, AcademicYear, Trimester, RepID,
CASE VisitType
WHEN 'Initial validation' THEN 1
WHEN 'Full validation' THEN 2
WHEN 'Compliance monitoring' THEN 3
END AS VisitTp
FROM INSERTED) AS s ON s.DistrictID = d.DistrictID
AND s.upazilaID = d.upazilaID
AND s.LC_ID = d.LCID
AND s.AcademicYear = d.LCVisitYr
AND s.Trimester = d.Trimister
AND s.RepID = d.MOID
AND s.VisitTp = d.VisitType
WHEN MATCHED THEN
UPDATE
SET DistrictID = S.DistrictID
WHEN NOT MATCHED THEN
INSERT (DistrictID, UpazilaID, LCID, VisitType, LCVisitYr, Trimister, MOID,
LCStatus, IfCloseWhy, OthersSpecify,LC1stVstDt)
VALUES (DistrictID, UpazilaID, Lc_ID, VisitTp, AcademicYear, Trimester, RepId,
2, 'No', 'No', FirstVisitDate);
END
Here the last line FirstVisitDate which is a datetime column. Without this column, it worked nice but when I include this column, it shows the error mentioned above.
Can anybody help me with this?
Thanks
As the error text suggests, you are refering to a column that does not exist.
In the SELECT part of your MERGE statement, there is no output column named FirstVisitDate, which is why you're getting the error...
Related
I am trying to fetch customer id from ":ao.mos:pro:%" and update corresponding doc ":pd:pro:%", we have thousands of ":ao.mos:pro:%" docs and corresponding ":pd:pro:%" for each mo:pro doc.
I am using below query and it works but it updates only one doc it can find indexes.
MERGE INTO `bucket1` AS d
USING `bucket1` AS p
ON d.icc = p.icc AND META(d).id LIKE ':pd:pro:%' AND META(p).id LIKE ':ao.mos:pro:%'
WHEN MATCHED THEN
UPDATE SET d.customerId = p.customerId;
Any suggestions on how to get this working for all matching docs in the bucket.
The following works.
CREATE INDEX ix11 ON `default` (icc, customerId) WHERE META().id LIKE ":ao.mos:pro:%";
CREATE INDEX ix12 ON `default` (icc, customerId) WHERE META().id LIKE ":pd:pro:%";
INSERT INTO default VALUES(":ao.mos:pro:1", {"icc":1, "customerId":100});
INSERT INTO default VALUES(":ao.mos:pro:2", {"icc":2, "customerId":101});
INSERT INTO default VALUES(":pd:pro:1", {"icc":1});
INSERT INTO default VALUES(":pd:pro:2", {"icc":1});
INSERT INTO default VALUES(":pd:pro:3", {"icc":2});
MERGE INTO `default` AS m
USING (SELECT p1.icc, p1.customerId
FROM `default` AS p1
WHERE META(p1).id LIKE ':ao.mos:pro:%' AND p1.icc IS NOT NULL) AS p
ON m.icc = p.icc AND META(m).id LIKE ':pd:pro:%'
WHEN MATCHED THEN
UPDATE SET m.customerId = p.customerId;
MERGE INTO `default` AS m
USING default AS p
ON m.icc = p.icc AND META(m).id LIKE ':pd:pro:%' AND META(p).id LIKE ':ao.mos:pro:%'
WHEN MATCHED THEN
UPDATE SET m.customerId = p.customerId;
Are you having following issue:
First source row matches 2 target rows. Second source matches same target row (by changing icc value to 1 of document key ":ao.mos:pro:2") then returns error (5320 Multiple UPDATE/DELETE of the same document (document key 'xxx') in a MERGE statement) (as with partial updated rows, You need transactions don't want update anything) as you can't update same row again. To resolve that you need to change your ON clause.
If it is a one off operation you can use Eventing Service and create a function with a bucket alias of "src_bkt" to map to bucket1 in r+w mode and the deployment feed boundary set to "From Everything" can easily do 100's of millions of items quickly without indexes.
function OnUpdate(doc,meta) {
// Filter out all non-interesting items
if (!meta.id.startsWith(":pd:pro:")) return;
// No need to do anything already updated.
if (doc.customerId)return;
var myint = meta.id.substring(8);
var otherkey = ":ao.mos:pro:" + myint;
var otherdoc = src_bkt[otherkey];
if (otherdoc) {
if (doc.icc === otherdoc.icc ) {
// update the field as we have a match and it is missing.
doc.customerId = otherdoc.customerId;
src_bkt[meta.id] = doc;
}
}
}
So now you just insert some test documents using the QWB
INSERT INTO bucket1 VALUES(":ao.mos:pro:1", {"icc":1, "customerId":100});
INSERT INTO bucket1 VALUES(":ao.mos:pro:2", {"icc":2, "customerId":101});
INSERT INTO bucket1 VALUES(":pd:pro:1", {"icc":1});
INSERT INTO bucket1 VALUES(":pd:pro:2", {"icc":1});
INSERT INTO bucket1 VALUES(":pd:pro:3", {"icc":2});
Then inspect the documents you should have
:ao.mos:pro:1{"customerId":100,"icc":1}
:ao.mos:pro:2{"customerId":101,"icc":2}
:pd:pro:1{"icc":1}
:pd:pro:2{"icc":1}
:pd:pro:3{"icc":2}
Deploy the Eventing function and look at your documents again.
:ao.mos:pro:1{"customerId":100,"icc":1}
:ao.mos:pro:2{"customerId":101,"icc":2}
:pd:pro:1{"icc":1,"customerId":100}
:pd:pro:2{"icc":1}
:pd:pro:3{"icc":2}
As expected only one doc with matching keys and matching icc properties updated.
For more performance (doing millions) you can up the workers in the functions settings to 2x the physical cores.
so for this, the goal is to add a column to a table called ENROLLMENT called FullFeePaid and to populate the column (assuming its null) but the only two possible answers can be Yes or No comparing data from another table called Course.
So I type the following command using alter however I keep getting a message where there's a duplicate, even i've never made a column called FullFeePaid.
But before that I kept getting an error saying there's a syntax error where it says
"set e.FullPaid = if(c.fee - e.amount <= 0 'yes', 'no')".
SET SQL_SAFE_UPDATES = 0;
ALTER TABLE enrollment
add fullfeepaid varchar(45) NULL;
UPDATE ENROLLMENT as e
JOIN COURSE as c on e.CourseNumber = c.CourseNumber
set e.FullFeePaid = if(c.fee - e.amountpaid <= 0 'yes', 'no')
where e.CourseNumber = c.CourseNumber;
select *
from enrollment;
You're missing a comma between the condition and the result your want to return if it evaluates as true:
UPDATE ENROLLMENT as e
JOIN COURSE as c on e.CourseNumber = c.CourseNumber
SET e.FullFeePaid = if(c.fee - e.amountpaid <= 0, 'yes', 'no')
-- Missing comma in the OP ---------------------^
WHERE e.CourseNumber = c.CourseNumber;
I was trying to en- and decypt data with the corresponding MySQL-functions.
This is what I did:
INSERT INTO dbsec.tbl_credent (U_Password) VALUES (AES_ENCRYPT('secretText', SHA2('pwd123',512)));
the Primary-Key (id) was 6. So I used
SELECT dbsec.tbl_credent.U_Password FROM dbsec.tbl_credent WHERE dbsec.tbl_credent.id = '6';
I got something like this:
Ž4•ý/2Ÿ½üyÙ¤Ý'
So encryption seems to work so far.
When I start the following query:
SELECT AES_DECRYPT(dbsec.tbl_credent.U_Password, 'pwd123') FROM dbsec.tbl_credent WHERE dbsec.tbl_credent.id = '6';
Result is NULL
I used hashing for the password so I tried
SELECT AES_DECRYPT(dbsec.tbl_credent.U_Password, SHA2('pwd123',512)) FROM dbsec.tbl_credent WHERE dbsec.tbl_credent.id = '6';
Result is 73656372657454657874
As all this didn't work I tested this directly:
SELECT AES_DECRYPT(AES_ENCRYPT('secretText', SHA2('pwd123',512)), 'pwd123');
Again the Result was NULL and
SELECT AES_DECRYPT(AES_ENCRYPT('secretText', 'pwd123'), 'pwd123');
returned 73656372657454657874 again.
What do I have to do to get back the 'secretText' I have encrypted?
The Type of U_Password is text (latin1_swedish_ci), btw.
I am trying to update the column called NewNumber. What I want to do is if the column phone_code has 1 in it, I want to update the column NewNumber with the value of the phone_number column. But if it is anything else than 1 then I want to concatenate the phone_code and phone_number columns and update NewNumber column. Below is the code I tried but that doesnt work
update Arvada_N set NewNumber = IIf([phone_code = 1], phone_number, [phone_code&phone_number]);
Any help will be appreciated
Never mind, this works
update Arvada_N set NewNumber = IIf([phone_code] = '1', phone_number, [phone_code]&[phone_number]);
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