473,434 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Would using a table variable be efficient?

We divided our tables from table zone_a, ..._b, ..._c to zone_z, these tables have the same attributes. We have divided this tables in this way so that any data that is under zone_a would only be on it's specific table, and the same with the other remaining table. Since per table holds thousands of data per day we have decided it to be designed this way and also hoping to reduce lock up of tables everytime a user accesses the table.

Now, we made a function that returns a table. This function will handle what table is to be selected. The body of this function contains a table variable, it holds the data from the selected table. This function will return the table variable

We have made this function hoping that it would be efficient to use for other future procedures.

Is it efficient?

Thank You in advance.
May 19 '10 #1

✓ answered by Jim Doherty

@clear1140
Table variables generally should be used only with relatively small amounts of data in my view why not just use a stored procedure with a zone parameter passed in to divide the data from the source then all you need to do is run the stored procedure. One tip for you.....if all you are doing is accessing table data jto read records basically and are not too worried or concerned about concurrent edits then use the optimiser Hint WITH (NO LOCK) This prevents locks and will thus be faster

Expand|Select|Wrap|Line Numbers
  1. SELECT Column1,Column2,Column3 FROM YOURTABLENAME (WITH NO LOCK)

4 2567
Jim Doherty
897 Expert 512MB
@clear1140
Table variables generally should be used only with relatively small amounts of data in my view why not just use a stored procedure with a zone parameter passed in to divide the data from the source then all you need to do is run the stored procedure. One tip for you.....if all you are doing is accessing table data jto read records basically and are not too worried or concerned about concurrent edits then use the optimiser Hint WITH (NO LOCK) This prevents locks and will thus be faster

Expand|Select|Wrap|Line Numbers
  1. SELECT Column1,Column2,Column3 FROM YOURTABLENAME (WITH NO LOCK)
May 19 '10 #2
@Jim Doherty
thank you so much. Am getting what u mean and somehow i knew this was gonna be the answer. Thanks for the WITH (NO LOCK) option, i have never used that before
May 19 '10 #3
Jim Doherty
897 Expert 512MB
@clear1140
You're welcome :) Yes the optimiser hint NO (LOCK) is very useful if used appropriately, ie fetching read only data to a web page for instance, where it doesnt really matter for that split second if we are retrieving all available updates and so on.
May 19 '10 #4
ck9663
2,878 Expert 2GB
Or you can still use a function that handles the returning of the value. Since they seems to have an identical structure, you can do something like...

Expand|Select|Wrap|Line Numbers
  1.  
  2. create function ReturnYourData (@fromtablename varchar(50))
  3. returns table
  4. as
  5. return
  6. (
  7. select 'TableA' as source, col1, col2, col3, col4 from TableA where @fromtablename = 'TableA'
  8. union all
  9. select 'TableB' as source, col1, col2, col3, col4 from TableB where @fromtablename = 'TableB'
  10. union all
  11. select 'TableC' as source, col1, col2, col3, col4 from TableC where @fromtablename = 'TableC''
  12. )
  13.  
  14.  
Since a variable can only contain a single value at any point in time, your query will only return the value from a specific table. This is also a handy technique rather than using an dynamic query.

Make sure your table have proper index.

Good Luck!!!

~~ CK
May 20 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Rick Razzano | last post by:
I am seeing some strange behavior in sql server. Running a procedure with a table variable, selecting from the table, and exiting the loop will cause a rollback statement to be issued (which you...
2
by: Shaun Evans | last post by:
Is is possible to create a composite primary key on a table variable? Neither of these two statements are successful: DECLARE @opmcjf TABLE ( jobdetailid INT NOT NULL, cjfid INT NOT NULL,...
2
by: LVande | last post by:
SQLLY challenged be gentle -- Trying to create code that will drop a table using a variable as the Table Name. DECLARE @testname as char(50) SELECT @testname = 'CO_Line_of_Business_' +...
1
by: Joe | last post by:
Hi, I've got 2 table variables inside of an SQL 2000 function: @tmpBigList(BItemID, BRank) @tmpSmallList (ItemID, Rank) The following UPDATE statement can run for a long time if @TmpTable1 has...
2
by: littlefitzer | last post by:
Hi, Say for example I want to use the following line in my XSL: <xsl:call-template name="buildText"/> I know I have to have a template defined named buildText. My question is: Is there a way...
9
by: Stefan Mueller | last post by:
I'd like to set a variable called 'FocusIsOn' if a button got the focus. Because my button is dynamically created I do it like xelement = document.createElement("input") xelement.type = "button"...
6
by: James White | last post by:
I wish to open a form using a variable derived from a table. The Application is based on the switchboard principle from Access. However in the absence of do.cmd openForm, I have to use this code....
2
by: Burbletrack | last post by:
Hi All, Hope someone can help me... Im trying to highlight the advantages of using table variables as apposed to temp tables within single scope. My manager seems to believe that table...
7
by: John Smith | last post by:
Hello, I have a simple question, I have a vb.net form with several buttons. If I store the name of a button in a variable.. Dim TheName as string TheName = ...
3
by: Andrea Raimondi | last post by:
Hello peers! I'm working on this application and I'm in need for some thoughtful advice :-p I have an SQLDataSource with params, select, etc. One of my params is the table name, which can be...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.