getting the updated value from an UPDATE query - sql-server-2008

I have an UPDATE Query :
UPDATE FKMS_GNST_Transaction_Details
SET Received_Quantity=Received_Quantity+(
CASE
WHEN (#int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity)
THEN GTD.Quantity-GTD.Received_Quantity
ELSE (#int_Updated_Qty)
END)
,#int_GNST_Reference_Id=GTD.Transaction_Detail_Id
FROM FKMS_GNST_Transaction_Details GTD
INNER JOIN #tbl_transactions tmp
ON tmp.Transaction_id=GTD.Transaction_id
AND GTD.Item_id=tmp.Item_id
I want to get the quantity which is added to the Received_Quantity field. That is, if (#int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity) then GTD.Quantity-GTD.Received_Quantity other wise #int_Updated_Qty.
How can we take this value (into a variable or any other way)? Please help.

Use the OUTPUT clause
UPDATE FKMS_GNST_Transaction_Details
SET Received_Quantity=Received_Quantity+(
CASE
WHEN (#int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity)
THEN GTD.Quantity-GTD.Received_Quantity
ELSE (#int_Updated_Qty)
END)
,#int_GNST_Reference_Id=GTD.Transaction_Detail_Id
--start gbn code
OUTPUT INSERTED.Received_Quantity
--end gbn code
FROM FKMS_GNST_Transaction_Details GTD
INNER JOIN #tbl_transactions tmp
ON tmp.Transaction_id=GTD.Transaction_id
AND GTD.Item_id=tmp.Item_id
The OUTPUT results can go
into a table (real, temp or variable)
to the client as a recordset
You can't assigned directly to a local variable

Related

SELECT statement inside a CASE statement in SNOWFLAKE

I have a query where i have "TEST"."TABLE" LEFT JOINED to PUBLIC."SchemaKey". Now in my final select statement i have a case statement where i check if c."Type" = 'FOREIGN' then i want to grab a value from another table but the table name value i am using in that select statement is coming from the left joined table column value. I've tried multiple ways to get to work but i keep getting an error, although if i hard code the table name it seems to work. i need the table name to come from c."FullParentTableName". Is what i am trying to achieve possible in snowflake and is there a way to make this work ? any help would be appreciated !
SELECT
c."ParentColumn",
c."FullParentTableName",
a."new_value",
a."column_name"
CASE WHEN c."Type" = 'FOREIGN' THEN (SELECT "Name" FROM TABLE(c."FullParentTableName") WHERE "Id" = 'SOME_ID') ELSE null END "TestColumn" -- Need assistance on this line...
FROM "TEST"."TABLE" a
LEFT JOIN (
select s."Type", s."ParentSchema", s."ParentTable", s."ParentColumn", concat(s."ParentSchema",'.','"',s."ParentTable",'"') "FullParentTableName",s."ChildSchema", s."ChildTable", trim(s."ChildColumn",'"') "ChildColumn"
from PUBLIC."SchemaKey" as s
where s."Type" = 'FOREIGN'
and s."ChildTable" = 'SOMETABLENAME'
and "ChildSchema" = 'SOMESCHEMANAME'
) c
on a."column_name" = c."ChildColumn"
Thanks !
In Snowflake you cannot dynamically use the partial results as tables.
You can use a single bound value via identifier to bind a value to table name
But you could write a Snowflake Scripting but it would need to explicitly join the N tables. Thus if you N is fixed, you should just join those.

Return text if tablix results are empty

I have a tablix that is linked to DataSet1.
DataSet1 uses the following TSQL code
select ir.SourceRef as Account_Ref,
rab.BalanceFromDate,
rab.ClosingBalance Current_Balance,
ra.Account_ID as rserial,
ra.Current_Balance as Current_Balance
from db1..RentAccountBalance rab
left join db1..ImportReference ir on ir.EntityID = rab.AccountId and ir.EntityType='XXXX.XXX.X.XX'
left join db2..RentAccounts ra on convert(varchar(50),ra.Account_ID) = ir.SourceRef
where ir.SourceRef = '12857'
order by rab.AccountBalanceId
As I know that there is no ir.SourceRef that is equal to 12857, the result set is blank. Therefore, my tablix comes back just blank.Is there a way that if no results are returned that a text of say "All Accounts are OK." be displayed by the report instead?
Hope that's clear?
Thanks
You can try expression like:
=IIF(IsNothing(Fields!FieldName.Value, "Accounts are OK", Fields!FieldName.Value))
Or if you want to check if there is no data at all, you can try to throw an error in TSQL in following:
--add this line to the end of query:
IF ##ROWCOUNT = 0 RAISERROR('Accounts are OK', 16, 1)
If you use a Stored Procedure you can then insert your Select statement data into a table variable before returning it. From this you can perform a check on its contents before it is returned to the report.
For example if you populate a table of data you wish to return as follows
INSERT INTO #ReturnTable (Account_Ref, ...)
SELECT ir.SourceRef, ...
You can then query it's contents by using a command such as
IF (SELECT COUNT(*) FROM #ReturnTable) = 0
BEGIN
INSERT INTO #ReturnTable (Account_Ref, ...)
SELECT 'All Accounts are OK', ...
END
You can then perform a check within the report to see if the Account_Ref is 'All Accounts are OK', and if so display the report appropriately. You can even set the entire report's contents inside a rectangle with the visibility set to the result of
=iif(First(Fields!Account_Ref.Value) = "All Accounts are OK", false, true)
You can layer another object (an information message perhaps) on top of this with the inverse of this visibility set.

MySQL Stored Procedure update using a Join and CASE..ELSE syntax

This is my first time creating a MySQL stored procedure and am stuck on getting the UPDATE piece to work correctly. The proc is performing an inner join, looking for matches on a domain name field. If there is a match, a column named inbound is getting updated with a value of 0. If there is not a match on the join, then I need inbound set to a value of 1.
When I run this, I am able to get the matches tagged with a 0, but the non-matches are not getting updated with a 1. I thought how I have the 'ELSE' part set up would take care of this- can anyone tell if I am missing something with the syntax?
CREATE PROCEDURE `sp_InboungTagging`()
BEGIN
update `tableA` a
inner join `TableD` d
on a.senderDomain = d.domainName
set inbound = CASE
when a.senderDomain = d.domainName then 0
ELSE 1
END
WHERE inbound is null;
END;|
DELIMITER ;
Thanks,
Ron
EDIT-
Thanks for your reply. I am looking for exact matches on a varchar field that has domain names in it- the master list of domains is in table D. If the record in TableA has a match in TableD, I want to tag that recored with a 0. If there is no match in TableD, then I would like to tag it with a 1. Let me know if that clears things up- thanks
Your JOIN condition is the same as your CASE condition. If you JOIN your two tables on:
a.senderDomain = d.domainName
Then there will be no values in the result set for which
a.senderDomain != d.domainName
so the ELSE clause of your CASE statement never fires.
Without knowing more about what you mean by "matches" and "non-matches," I can't really suggest a correction.

MySQL Get "id" from select

I have a select statement:
SELECT id, content, name
FROM records
WHERE type = '1'
AND name = 'test';
Here's the output:
id content name
99708 10.6.252.41 server01.example.org
What I'd like to do is be able to get the id that is returned from the previous statement and USE the id as input into another statement (an UPDATE statement) that will increment the value of a single column in the same table.
An example UPDATE statement that I am wanting is:
update records SET hits = hits + 1 WHERE id = ID_FROM_SELECT;
Thanks in advance.
You can use user defined session variables for this if the SELECT is returning just one result:
SELECT #id:=id AS id, content, name
FROM records
WHERE type = '1'
AND name = 'test';
Then, on the same database session (connection), do the following:
UPDATE records
SET hits = hits + 1
WHERE id = #id;
I'm assuming you're doing something with the selected records in your app, and you're trying to save on performance by avoiding having to search for the record again in the UPDATE. Though, in that case, why not set the 'id' value as a parameter in code?
Obviously, if the SELECT is returning multiple records, this would best be done in code as I mentioned above, otherwise you're left with running the SELECT query again as a subquery:
UPDATE records
SET hits = hits + 1
WHERE id IN
(SELECT id
FROM records
WHERE type = '1'
AND name = 'test');
So, then, it makes more sense just to apply the same filter to the UPDATE instead:
UPDATE records
SET hits = hits + 1
WHERE type = '1'
AND name = 'test'
Probably this is not what you want to do.
First of all...If the query only returns 1 line, the solution provided by Marcus Adams works fine. But, if the query only returns one line, you dont need to preset the id in order to update. Just update it:
update records
set hits = hits + 1
where type = '1'
and name = 'test'
Second...If the query will not return only one record and you want to update all records returned with same values or calculations, the same code above will do what you need.
Third, if the query does not return just one record and you need to update each record returned with different value then you need to have a different approach.
I think you are not designing your system very well. If the request for update come from outside, you should have the id to be updated as a parameter of your request. For example something like:
<html>
<body>
Test
</body>
</html>
And in your update.php you have something like:
<?php
$id = $_GET['id'];
$sql = "update records set hits = hits + 1 where type = '1' and name = 'test' and id = $id";
?>
Of course, the picture I have is to small. Probably you have a reason to do this way or this is just an example. If you fill us up with more info we might be more helpful.

update case query for multiple conditions

I am using an UPDATE query to update multiple rows which is something like the following
UPDATE TBLNAME SET NEWCOLUMN =
CASE
WHEN TYPE='ACCOUNT' THEN 'FINANCE'
WHEN CODE='DATA' AND CLASS='FIRST' THEN 'LEGAL'
END
The above query works fine for single conditions and for certain conditions something like
NEWCOLUMN = Audit when Type = file, fax, documents, I was using something like
UPDATE TBLNAME SET NEWCOLUMN =
CASE
WHEN TYPE='ACCOUNT' THEN 'FINANCE'
WHEN CODE='DATA' AND CLASS='FIRST' THEN 'LEGAL'
WHEN TYPE='FILE' AND 'FAX' AND 'DOCUMENTS' THEN 'AUDIT'
END
The above query works fine for the first two conditions but the third condition AUDIT is not updated.
Could somebody help?
Thanks
Use
WHEN TYPE IN ('FILE','FAX','DOCUMENTS') THEN 'AUDIT' END