I want to insert records into a TempTable. Something like this:
insert into ##tempT
SELECT * FROM MyTable
MyTable contains a large amount of records so the "insert into" takes a long time.
If I try to run:
SELECT COUNT(*) FROM ##tempT
it returns always "0" until all records from "MyTable" are inserted by INSERT INTO command.
How can I get a progress count that advise me how many records are in ##tempT?
I need to update a progress bar value while the SQL command is running.
Thank you.
try
set transaction isolation level read uncommitted
SELECT COUNT(*) FROM ##tempT
You can split your query up.
x = number of records in MyTable / 100
i = 0
do until we're done
queryString = "insert into ##tempT "
queryString += "select top " + x " + " * FROM MyTable "
queryString += "where RecordNumber > " + i
Execute queryString
Update Progress Bar
i = i + x
loop
You'll notice that you'll need some sort of RecordNumber field to make this work though. There are various ways to accomplish that you can search for.
Use a stored procedure and DECLARE a variable COUNT and treat this as a looping variable and each time an insert is done, increment COUNT by 1 and keep printing it using another query whenever you want to know the count .Or, return this count from the procedure and read it into your program for updating the progress bar. :)
Related
I want to delete multiple records from table at the same time.
Sample input:
{"INPUT":{"ID":"2200038,2200039,2200073,2200019"}}
Input will be provided from the application i.e.,
ID can be random - it gets changed based on requirements.
delete from mytable
where id = ....?
I want to delete multiple ID's coming from the input at the same time.
To delete multiple rows at once with different IDs, one approach is to use IN:
DELETE FROM mytable
WHERE ID IN (2200038,2200039,2200073,2200019)
Here's some documentation and further examples: http://www.postgresqltutorial.com/postgresql-in/
You may extract the ids from your json string as an array and delete those using ANY operator
WITH t AS
(
SELECT '{"INPUT":{"ID":"2200038,2200039,2200073,2200019"}}' AS input
)
DELETE FROM mytable
WHERE id = ANY ( SELECT unnest(
String_to_array(input::json->'INPUT'->>'ID',',')::int[])
FROM t );
Demo
Here's a demo using a Bind variable for input in psql. Note that UNNEST was not needed here.
\set input '{"INPUT":{"ID":"2200038,2200039,2200073,2200019"}}'
knayak=# DELETE FROM mytable WHERE
id = ANY( String_to_array(:'input'::json->'INPUT'->>'ID',',')::int[] )
DELETE 2
Maybe some dynamic sql will help
EXEC SQL BEGIN DECLARE SECTION;
const char *stmt = "DELETE FROM tablename WHERE ID IN(?);";
EXEC SQL END DECLARE SECTION;
EXEC SQL PREPARE mystmt FROM :stmt;
inputdata varchar; -- remove unwanted parts of the string
EXEC SQL EXECUTE mystmt USING inputdata;
I have a check box on front end.
If the check box is checked: data with only checked chk1 should appear on front end.
If the check box is not checked: full data should appear on front end.
Please suggest how should I proceed with the same in SQL Using If else / Case statement.
I am using:
SELECT *
FROM table1
where (some conditions) AND
CASE #a.Id_Oem_Irm
WHEN 0 THEN (a.id_oem_irm in(0,1))
WHEN 1 THEN (a.id_oem_irm in (1))
END
PS: a.Id_Oem_Irm: a is the table name, Id_oem_irm is the column name for check box.
I would recommend writing this as:
where (some conditions) AND
((#a.Id_Oem_Irm = 0 and a.id_oem_irm in(0, 1)) OR
(#a.Id_Oem_Irm = 1 and a.id_oem_irm in (1) )
)
I am not sure what #a.Id_Oem_Irm is supposed to be. I suspect you want a variable there.
Or you could tune it a bit like this:
SELECT *
FROM table1
where (some conditions) AND
(a.id_oem_irm = 1 OR
#a.Id_Oem_Irm = 0)
This is only valid if #a.Id_Oem_Irm is always 0 or 1. Otherwise, you obviously should add a #a.Id_Oem_Irm IN (0,1) or <= 1 (if it can't be negative) condition.
What is a? The alias for another table you didn't include here?
The simpler way to do this is create a storedprocedure which will take the input "#a.Id_Oem_Irm"
Example:
CREATE PROCEDURE p_get_a_data(
IN Id INT(1)
)
BEGIN
IF (Id = 0) BEGIN
--YOur query to retrieve partiular row/ column
END;
IF(Id = 1) BEGIN
--YOur query to retrieve all data
SELECT * FROM table1
where (some conditions)
END;
END
The usage of stored procedure will give you selected data and the query is precompiled in the database so it faster.
Note: I didn't quite understand what your query is returning so replace mine with yours
I'm having to restructure how an existing program deals with the database.
Before, I was executing statements using odbc_php consecutively.
E.g
SELECT [Value1] FROM TABLE save to $value1
INSERT INTO TABLE2 (VALUE2) VALUES ('$value1')
UPDATE TABLE SET [Value1] = '" . $value1 + 1 . "'
You get the idea.
However I believe this way of running statements is causing conflict with 'other users' of the database.
My solution is to run the statements as an implicit transaction, however I require values being saved & reused during this transaction.
so, How do I save values from select statements in MSSQL?
(My skills are not ideal for MSSQL so any good tutorial or help document are apperciated)
You have some issues with dealing with concurrent attempts to hit this sequence of commands. Given that:
You want Table2 to have an entry for every incremented value1
and
You want Table1 to have value1 incremented by 1 every time this is executed.
In which case, I think you need to update Table1 first and then insert the original value prior to the update into table2, ensuring this remains consecutive:
DECLARE #UpdatedVal TABLE (InsertVal INT)
UPDATE Table1
SET Value1 = (Value1 + 1)
OUTPUT INSERTED.Value1
INTO #InsertedVal
INSERT INTO Table2 (VALUE2)
VALUES ((SELECT(InsertVal - 1) FROM #UpdatedVal)
GO
One example:
declare #value int <\or any datatype> --create a variable
select #value = value1
from table --set the value
insert into table2 (value2) values(#value) --insert it
How can I test an UPDATE statement for example to see if it would work, for example if it would actually update rows etc?
Is there a way to simulate it easily?
Use a transaction to wrap your update statement and a select query (to test the update) and then always roll it back.
Example:
BEGIN;
UPDATE accounts SET balance = balance - 100.00
WHERE name = 'Alice';
SELECT balance FROM accounts WHERE name = 'Alice';
ROLLBACK; -- << Important! Un-does your UPDATE statement above!
A transaction typically ends with a commit but since you're just testing and do not want the changes to be permanent you will just roll back.
Wrap it in a transaction, test the results with a SELECT and rollback at the end.
BEGIN;
UPDATE ...;
SELECT ...;
ROLLBACK;
Prepend your SQL UPDATE command with EXPLAIN [ref], and it will tell you how many lines will be affected by your command. And it will not perform the actual update.
This is much simpler than wrapping your command in a transaction. E.g.:
EXPLAIN UPDATE accounts SET balance = balance + 100.00 WHERE name = 'Alice';
You could always build up a sample database on SQL Fiddle and try out your update statements there.
Full disclosure: I am the author of sqlfiddle.com
With Postgres you can use the UPDATE clause RETURNING to show which rows have been modificated.
-- example data
CREATE TABLE data(id int, text text);
INSERT INTO DATA VALUES(1,'aaa'),(2,'bbb'),(3,'ccc'),(4,'ddd');
-- original data
SELECT * from data;
-- dry-run update
BEGIN;
UPDATE
data
SET
text = 'modified'
WHERE
id > 2
RETURNING
id, text;
ROLLBACK;
-- data after dry-run update
SELECT * from data;
Run the same check with a SELECT statement first: the rows returned by SELECT will be the rows modified by UPDATE
Given this simple update:
UPDATE Products
SET price_including_vat = price * 1.05
WHERE product_type = 'Food';
I would test it using something like this:
SELECT price_including_vat AS price_including_vat__before,
price * 1.05 AS price_including_vat__after,
*
FROM Products
WHERE product_type = 'Food';
Actually, I'd proably engage brain and do analysis more like this:
WITH updated AS
(
SELECT price_including_vat AS price_including_vat__before,
price * 1.05 AS price_including_vat__after,
*
FROM Products
WHERE product_type = 'Food'
)
SELECT *
FROM updated
WHERE price_including_vat__before = price_including_vat__after;
This is a bit of code in one of the pages I am reviewing and trying to update. What I am trying to do is swap the inline query for stored procedures. This is the bit of code:
numOfDelRec = 1
Do While (numOfDelRec > 0)
cm.CommandText = "DELETE Folder WHERE FolderID IN
(SELECT ch.FolderID FROM Folder ch
LEFT JOIN Folder p ON ch.ParentID=p.FolderID " & _
"WHERE p.FolderID IS NULL AND ch.ParentID > 0)
AND UserID=" & Session("UserID")
cm.Execute numOfDelRec
Loop
What I am curious about is that the value numOfDelRec comes back and is used to continue looping, how do I basically do the same thing with a stored procedure, given that the stored procedure is basically the same as the inline sql?
This is my call to the stored procedure:
AuthConn.Execute("sp_MyFolder_DeleteFolder " & Session("UserID"))
This is Microsoft SQL Server 2005.
Thanks, R.
First off: Do not name this stored procedure as
sp_
. "sp" stands for "special procedure", and--in SQL Server--all procedures that begin with "sp underscore" [Stack overflow really doesn't want me to type an underscore character where I need to] get special handling. There are times and places to make sp_ procedures [hah, that time it worked], but this doesn't look like one of them. Read SQL Books Online for more on this--it's an involved concept; for now, I'd call the procedure "MyFolderDeleteFolder", or "usp_MyFolder_DeleteFolder" [heck with it, please imagine the underscores are where they should be] if you have to have a semi-redundant naming convention.
Moving on from that mangled caveat, here's how I'd transfer this command into a procedure. (I can't test it just now, it may require some minor debugging, and I'd love to turn the LOJ into a NOT EXISTS):
CREATE PROCEDURE MyFolder_DeleteFolder
#UserId int
AS
SET NOCOUNT on
DECLARE #RowsDeleted int
SET #RowsDeleted = 1
WHILE #RowsDeleted > 0
BEGIN
-- Loop until a call to DELETE does not delete any rows
DELETE Folder
WHERE FolderID IN
(SELECT ch.FolderID
FROM Folder ch
LEFT JOIN Folder p
ON ch.ParentID = p.FolderID
WHERE p.FolderID IS NULL
AND ch.ParentID > 0)
AND UserID = #UserID
SET #RowsDeleted = ##rowcount
END
RETURN 0
You can pass the variable to the execute to get the # of rows affected.
AuthConn.Execute("sp_MyFolder_DeleteFolder " & Session("UserID"),numOfDelRec)
http://www.devguru.com/technologies/ado/quickref/connection_execute.html
I read your question as that you want to drop the whole of your query into a stored procedure for cleaner code.
If that is the case, then the ExecuteNonQuery method returns the number of rows affected.
Try:
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "ProcName"
numOfDelRec = 1
Do While (numOfDelRec > 0)
numOfDelRec = cm.ExecuteNonQuery()
Loop
The SQL looks like its deleting records from a self referencing table. More specifically it looks like its deleting parent records with no children. The numOfDelRec probably holds the number of records affected by the sql, i.e. number of rows deleted. So it looks like it continues to run, deleting parent records without children, until the number of deleted records is 0.
It probably loops like that so if a parent record has no children, its deleted. However if it itself was a child its parent record was not deleted on the first pass but will be caught on the second pass and so on.
You did not mention the flavor of database server you are using, but with sql server you can do a while loop which is described here at MSDN http://msdn.microsoft.com/en-us/library/ms178642.aspx That would probably do the trick.
I'm not sure if that is exactly what your asking, but I believe that is what the code is doing.