How to insert the result in the desired row? SQL Server - sql

I need the result that I got to be inserted in the right row.
DECLARE #DiscPrice float;
SET #DiscPrice = (SELECT Prod.priceProd - Prod.priceProd / 100 * Prod.disc
FROM Prod
WHERE id_prod = 1);
UPDATE Prod
SET priceDisc = #DiscPrice
WHERE id_prod = 1;
SELECT * FROM Prod;
That is, instead of WHERE id_prod = 1, there was something that inserted the desired result in all rows.
I'm not sure I made myself clear, but I hope that you will understand.

I think you want
UPDATE Prod
SET priceDisc = ((priceProd - priceProd) / 100) * disc
WHERE id_prod = 1;
There is no need to use a variable or a query to assign the value to it.

You can directly update the query as below
UPDATE Prod SET priceDisc =((priceProd - priceProd) / 100 * disc)
This will update the data in all rows

Related

calculated field using if and or in ms-sql

IF(
[dbo.tblx.Category] = 'WS' OR [dbo.tblx.Category] = 'SEM',
0,
[dbo.tblx.Tonnes] * [dbo.tblx.Grade] / 1000
)
this returns the entire text and not the "calculation"
can someone help me...what am I doing wrong?
i tried
CASE WHEN dbo.tblx.Category = 'WS' OR dbo.tblx.Category = 'SEM' THEN 0
ELSE dbo.tblx.Tonnes * dbo.tblx.Grade / 1000
END AS Metal
Do perhaps insert something before "CASE WHEN"? it is not running
As I understood, you are trying to create a computed column, using value from other table.
I think in this case you have to use a User Defined function in the formula for computed column. If you give more information I can try to write some code for you.
Instead, if you just want to add a column in a query you can use:
CASE WHEN dbo.tblx.Category = 'WS' OR dbo.tblPTx.Category = 'SEM' THEN 0
ELSE dbo.tblx.Tonnes * dbo.tblx.Grade / 1000 END AS NAME_OF_YOUR_NEW_COLUMN
Update
DECLARE #myvar INT;
SET #myvar = CASE WHEN ([dbo.tblx.Category] = WS OR [dbo.tblPTx.Category] = SEM) THEN 0
ELSE [dbo.tblx.Tonnes] * [dbo.tblx.Grade] / 1000
END

SQL update only values whose value is not already updated

I have a Query it updates the whole tickets in table.
I want it to update only the tickets whose values needs to be updated not update all rows.
e.g.
If slabreachdays is already 10 then new value is also 10 it should not update.
This is my update query.
update ticket
set TICKET.slabreachdays =
FLOOR(((DAYS(TICKET.creationdate) - DAYS(current timestamp)+10)
* 86400 + (MIDNIGHT_SECONDS(TICKET.creationdate) -
MIDNIGHT_SECONDS(current timestamp)))/86400.0)
where TICKET.VENDOR like 'ABC'
and TICKET.STATUS NOT IN('CANCELLED','CLOSED')
This is my select query which selects only the tickets that needs to be updated. This is the query I need to convert to an update query
select * from (
select ticketid,slabreachdays,
FLOOR(((DAYS(TICKET.creationdate) - DAYS(current timestamp)+10) * 86400 + (MIDNIGHT_SECONDS(TICKET.creationdate) - MIDNIGHT_SECONDS(current timestamp)))/86400.0)
as newValue
from ticket
where TICKET.MLOUTSOURCEVENDOR like 'ABC' and TICKET.STATUS NOT IN('CANCELLED','CLOSED'))
where SLABREACHDAYS != newValue
Try it
Where .... And slabeachdays<> aller calculated expression
Try this
update ticket
set TICKET.slabreachdays =
FLOOR(((DAYS(TICKET.creationdate) - DAYS(current timestamp)+10)
* 86400 + (MIDNIGHT_SECONDS(TICKET.creationdate) -
MIDNIGHT_SECONDS(current timestamp)))/86400.0)
where TICKET.VENDOR like 'ABC'
and TICKET.STATUS NOT IN('CANCELLED','CLOSED') and
TICKET.slabreachdays <>
(FLOOR(((DAYS(TICKET.creationdate) - DAYS(current timestamp)+10)
* 86400 + (MIDNIGHT_SECONDS(TICKET.creationdate) -
MIDNIGHT_SECONDS(current timestamp)))/86400.0))

Knowing the Value or Result on each variable SQL stored procedure

This is my stored procedure:
CREATE PROCEDURE _spCalc
(#Num01 decimal(18,0), #Num02 decimal(18,0), #Num03 decimal(18,0))
AS
BEGIN
DECLARE #Var01 float, DECLARE #Var02 float, DECLARE #Var03 float
SET #Var01 = #Num01 * 1000
SET #Var02 = #Num02 - ((POWER(6.53 * #Var01, 0.5)) / (POWER(#Num03, 0.5)))
SET #Var03 = (1 - 3.4680733 * LOG(#Var02) + 1.8779192 * POWER(LOG(#Var02), 2))
INSERT INTO _myTable(Num01, Num02, Num03, Num04)
VALUES (#Num01, #Num02, #Num03, ((#Var02 + #Var03 + #Num01) * 1000))
END
My question is, how can I test and know the result of each variable that I declared? I want to compare the result with the calculation on excel format. Just want to make sure, the result is the same.
Cheers,
we can use "print #var01". repeat for different variable names before or after calculation.this shows up in messages section of results.
or
we can use " select #var01 as var01,#var02 as var02,#var03 as var03" before your insert statement.this gives you a table like output during execution
you can extend the print or select with other variables as needed.
I could not add further comments for some reasons.
"try the calculations as separate "select" before assigning to variable"
select #Num01 * 1000 as #Var01
select #Num02 - ((POWER(6.53 * #Var01, 0.5)) / (POWER(#Num03, 0.5))) as #Var02
select (1 - 3.4680733 * LOG(#Var02) + 1.8779192 * POWER(LOG(#Var02), 2))
substitute the values that u pass to the procedure accordingly

sql loop update with multiple rows returned

I've got to update a column value by decreasing the value in the column by a variable.
There are two conditions:
1. where the row count = 1
2. where the row count is more than 1
I've got it set to do the single row count but need help when the query returns multiple rows.
set #rowsCounted = (select COUNT(QuantityA) from Offers where WID = #wId and ND = #nd)
if(#rowsCounted = 1)
begin
set #QuantityAvailable = (select QuantityA from Offers where WID = #wId and ND = #nd)
set #QuantityAvailable = (select #QuantityAvailable - #QuantityAdjusted)
update Offers
set QuantityA = #QuantityAvailable
where WID = #wId and ND = #nd
end
else
begin
select #rowsCounted as rowsCounted -- example of 4 rows with values of = 287,280,288,288
--begin loop as the QuantityA may contain different values
end
If #QuantityAdjusted is constant for the procedure, then you only need one update statement. Use set-based thought constructs rather than procedural-based ones:
update Offers
set QuantityA = QuantityA - #QuantityAdjusted
where WID = #wId and ND = #nd
This will update in a set-based operation, and there is no need to construct your own loop. This is part of what SQL engines are meant to do.

Add a random number between 30 and 300 to an existing field

I have looked on the net as well as here but can't find an answer to the following MySQL question. I'm looking to replace the value of an existing field with a query that has a random number between 30 and 300.
Reason was because I've moved galleries and had 250,000,000 views on my images and there have been lost with the migration and a lot of my members are upset that they have lost views....
UPDATE the_table SET the_field = the_field + FLOOR(RAND() * (270 + 1)) + 30
Use RAND()
UPDATE table
SET field = FLOOR(30 + (RAND() * 270));
WHERE foo = 'bar'
I think this will do the trick:
UPDATE table SET field = ROUND(30 + (RAND() * 270)) WHERE id =1;