October 16, 2020 at 8:47 pm
Good day, I need some advice about cursors:
SP1 SP2 SP3
Temp tables (3) Temp tables (3) Temp tables (3)
Batch_cursor Batch_cursor Batch_cursor
Line_cursor Line_cursor Line_cursor
Export_cursor Export_cursor Export_cursor
Export data to a SP1_FINAL Export data to a SP2_FINAL Export data to a SP3_FINAL
Is it possible
· All the cursors in different SP’s to have the same name.
· Can the temp tables have same names as well?
· Can they all run at the same time without interfering with each other
What is advisable in these situations.
October 16, 2020 at 11:47 pm
With the idea of teaching a person to fish, a temp table is still a table and you're trying to create one in each of the 3 procs. That's the hint you need to find the MS documentation on the subject. So, do a search for the following...
CREATE TABLE (Transact-SQL)
The first item returned in the search will probably be the MS documentation on the subject. If it's not, it probably won't be far from the top.
Once found, click on the link to get to the MS article (documentation) on the subject. It's a pretty long article so press [Ctrl][F] to do a find in the browser and search for "Temporary Table" (without the quotes) and click whatever your browser recognizes for the "next" instance of what it found until you get to the section about temporary tables. The information you seek, along with a whole lot more about temporary tables, is there.
For your cursor question, do similar.
Make sure that you read about the differences between "local" and "global" versions of the objects in each of the two articles because the answer to both of your questions actually is "It Depends". 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
October 18, 2020 at 6:35 am
Yes, yes and yes, as long as the proc not being EXEC'd by each other.
That is, this should work fine:
--ok
EXEC sp1
EXEC sp2
EXEC sp3
because SQL will clear temp tables created within a proc when that proc ends.
But you would have problems if you tried to do this:
--will fail
EXEC sp1
--and inside sp1 it tries to exec sp2:
CREATE sp1
AS
EXEC sp2
because the temp table that sp1 created is still there when sp2 is called, so trying to create the same table again will fail.
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
October 18, 2020 at 11:53 am
@jeff Moden -- Thank you for the guidance.
@scottpletcher -- Thank you. I am planning to use the first method.
October 18, 2020 at 12:35 pm
and look at not using cursors at all - in majority of cases where developers think they need cursors that is not the case
October 18, 2020 at 4:58 pm
To Frederico's good point, please see the following article for an introduction to "Tally Tables" (also known as "Numbers" tables). Keep in mind that it's only an introduction and do a search for both types of tables for many extraordinary examples of what they can be used to solve for without Cursors, While loop, and Incremental rCTEs (Recursive CTEs), which are all forms of "RBAR" (an acronym for Row By Agonizing Row and is pronounce as "ree-bar"... like the steel rods buried in cement that aren't going anywhere quickly).
--Jeff Moden
Change is inevitable... Change for the better is not.
October 18, 2020 at 6:16 pm
Thank you. I will do some research and see how I can achieve the same results without cursors. Because of tight deadlines I have used cursors in achieving what is required and I was a bit worried since I have used same names across the SP's.
October 19, 2020 at 1:27 pm
Thank you. I will do some research and see how I can achieve the same results without cursors. Because of tight deadlines I have used cursors in achieving what is required and I was a bit worried since I have used same names across the SP's.
When there is a tight deadline, and you take a shortcut to meet that deadline, do you realize the potential for creating far far more work than you saved?
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply