I want to perform many SQL UPDATE Statements like these:
UPDATE cityd SET time_zone='-7.000000' WHERE locId = 173567;
UPDATE cityd SET time_zone='-8.000000' WHERE locId = 173568;
UPDATE cityd SET time_zone='-6.000000' WHERE locId = 173569;
UPDATE cityd SET time_zone='-5.000000' WHERE locId = 173570;
UPDATE cityd SET time_zone='-6.000000' WHERE locId = 173571;
I want to optimize the transaction time, so I need to use BEGIN TRANSACTION/COMMIT pair. How to write this in SQL Syntax in SQLtite Manager?
Edit:
When I use the standard syntax in SQLite Manager I receive this error message:
"SQLiteManager: BEGIN TRANSACTION; [ cannot start a transaction within a transaction "
According on SQL Documention, there are two supported syntax of CASE
CASE x WHEN w1 THEN r1 WHEN w2 THEN r2 ELSE r3 END
CASE WHEN x = w1 THEN r1 WHEN x = w2 THEN r2 ELSE r3 END
So your multiple UPDATE statements can be further simplified into
UPDATE cityd
SET time_zone = CASE locId
WHEN 173567 THEN '-7.000000'
WHEN 173568 THEN '-8.000000'
WHEN 173569 THEN '-6.000000'
WHEN 173570 THEN '-5.000000'
WHEN 173571 THEN '-6.000000'
END
WHERE locId IN (173567, 173568, 173569, 173570, 173571)
BEGIN TRANSACTION;
.....YOUR SQL Statements here
COMMIT;
Related
I want to set hive variables based on condition. I am not sure if that's possible in HQL or not and if it is possible then how to achieve that.
I want to do something like this,
if ${hiveconf:work}==1 then do
set q1 = 4;
set yr2 = eval(${hiveconf:year}-1);
set q3= 3;
end
else if ${hiveconf:work}==2 then do
set q2 = 4;
set yr1 = eval(${hiveconf:year}-2);
set q4= 2;
end
Any help and knowledge will be appreciated.
I am running my query, but it does not work. The cause is the value of column is null and I'm trying to update it with a integer value.
This is my query.
The default value of sdoc is null
update expenese
set sdoc = sdoc + '200'
where expenese.date = '2016-03-26';
UPDATE EXPENESE
SET SDOC=ISNULL(SDOC,0)+200
WHERE DATE='2016-03-26'
I have this update statement:
UPDATE quotedetailextensionbase qd
SET qd.new_capvehicleid = (SELECT cv.new_capvehicleid
FROM vwCapidLookup cv
WHERE cv.new_capid = qd.new_capid
AND cv.new_captype = qd.new_captype)
I get an error back but I'm not sure why
Incorrect syntax near 'qd'
Seems like it would be easier to achieve this result with an update-join statement:
UPDATE qd
SET new_capvehicleid = cv.new_capvehicleid
FROM quotedetailextensionbase qd
JOIN (SELECT new_capvehicleid
FROM vwCapidLookup) cv ON cv.new_capid = qd.new_capid AND
cv.new_captype = qd.new_captype
SQL Server lets you do joined updates:
UPDATE
qd
SET
new_capvehicleid = cv.new_capvehicleid
FROM
quotedetailextensionbase qd
join vwCapidLookup cv on
cv.new_capid = qd.new_capid
AND cv.new_captype = qd.new_captype
;
Does that make sense?
Please try the below SQL:
UPDATE quotedetailextensionbase
SET new_capvehicleid =
(SELECT cv.new_capvehicleid
FROM vwCapidLookup cv
WHERE cv.new_capid = qd.new_capid AND
cv.new_captype = qd.new_captype)
Use MERGE: https://msdn.microsoft.com/en-us/library/bb510625.aspx. That is correct when you would update from another table.
I have a simple update statement that refuese to execute when passed from VB.NET (probably will after 10 minutes or so... haven't waited that long). But it executes in 1 sec from Oracle SQL Developer.
There are 67000 rows in table1
strQuery = "UPDATE table1 SET TYPE2 = TYPE2 || ', 5_badTag' WHERE LARGE = 'NO' AND ROUND = 'NO'"
SqlInsert = strQuery
cmdOleDbCommand = New Oracle.DataAccess.Client.OracleCommand(SqlInsert, dataAccessConn)
adpterODA.InsertCommand = cmdOleDbCommand
adpterODA.InsertCommand.ExecuteNonQuery()
Is there an open transaction on this table from another session (e.g. your SQL Developer)?
I have an SQL command I am attempting to execute.
The syntax is as follows
Public Shared Function CanRaiseWorkOrder(connection As IDbConnection, ProductID As Guid) As Boolean
Using sqlCmd As IDbCommand = connection.CreateCommand
With sqlCmd
.CommandTimeout = 30
.CommandType = CommandType.Text
.CommandText = "DECLARE #CanRaiseWorkOrder BIT, #WorkOrderQtyCount INT, #ProductAvailCount INT SET #WorkOrderQtyCount = (SELECT SUM(Qty) FROM WorkOrder WHERE ProductID = #ProductID AND WorkOrder.Status <> 4) --4 = Voided SET #ProductAvailCount = (SELECT Qty FROM Product WHERE ProductID = #ProductID) IF #WorkOrderQtyCount < #ProductAvailCount BEGIN SET #CanRaiseWorkOrder = 1 END ELSE BEGIN SET #CanRaiseWorkOrder = 0 END SELECT #CanRaiseWorkOrder AS CanRaiseWorkOrder"
Dim params As New List(Of IDbDataParameter)({
ProductDAL.CreateTSqlParameter("#ProductID", DbType.Guid, ProductID)
})
.Parameters.AddRange(params)
Return .ExecuteScaler(Of Boolean)()
End With
End Using
End Function
As you will probably notice there is some customization there in regards to how the parameters are created and the command executed but you can assume those aspects of the system work as required (We have a significant amount of code that functions correctly using those methods).
I will probably get some people asking why this is not a stored procedure and the answer is "because my boss said so".
I have run SQL profiler and here is the output that this query actually generates.
exec sp_executesql N'DECLARE #CanRaiseWorkOrder BIT, #WorkOrderQtyCount INT, #ProductAvailCount INT SET #WorkOrderQtyCount = (SELECT SUM(Qty) FROM WorkOrder WHERE ProductID = #ProductID AND WorkOrder.Status <> 4) --4 = Voided SET #ProductAvailCount = (SELECT Qty FROM Product WHERE ProductID = #ProductID) IF #WorkOrderQtyCount < #ProductAvailCount BEGIN SET #CanRaiseWorkOrder = 1 END ELSE BEGIN SET #CanRaiseWorkOrder = 0 END SELECT #CanRaiseWorkOrder',N'#ProductID uniqueidentifier',#ProductID='0908C780-763F-4CE6-B074-CEC01F4451B4'
Running the code in query analyser (when I originally created it) works fine but if I run the above query as outputted from the SQL command all I get is "Command(s) completed successfully."
Any ideas?
Figure I'll make it an answer, but also wanted to make sure I was accurate (considering how long I've been up now :grin:).
From what I can see (and this is probably due to condensing your statement for the sake of making it a one-liner in a code file) you have a comment declaration in the middle of your statement:
--4 = Voided [...]
What's happening is you're executing only your DECLARE and first SET command (which are done without error) but the rest of your statement is being ignored because it follows the comment declaration (--).
Make sure if you do condense your query that you remove commented lines. Once they're on one line, SQL doesn't care and WILL ignore anything past the --.