473,466 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Function Data from (Multiple?) Tables

6 New Member
Hi Forum,
I need a funtion to retrieve data from more then one Table. The tables are not related.Field names are the same in all tables. The table list I get from a view and i need to go through this list and pull out any records from the different tables that have a true value in a certain field. I am new to SQL and would appreciate any help. I am using SQL 2000

Thanks
Sedanda
Sep 9 '10 #1
8 1893
Sedanta
6 New Member
My Sp retrieves all the names of the tables that I need
I would like to use my SP dynamically in a loop with a union select command
Expand|Select|Wrap|Line Numbers
  1. GO
  2. -- Declare the variables to store the values returned by FETCH.
  3. DECLARE @Table varchar(50);
  4.  
  5. DECLARE Table_cursor CURSOR FOR
  6. SELECT Projekt FROM C_TFLS_Status_Matrix
  7. WHERE FLSExportAktiv = -1
  8. ORDER BY Projekt;
  9.  
  10. OPEN Table_cursor;
  11.  
  12. -- Perform the first fetch and store the values in variables.
  13. -- Note: The variables are in the same order as the columns
  14. -- in the SELECT statement. 
  15.  
  16. FETCH NEXT FROM Table_cursor
  17. INTO @Table;
  18.  
  19. -- Check @@FETCH_STATUS to see if there are any more rows to fetch.
  20. WHILE @@FETCH_STATUS = 0
  21. BEGIN
  22.  
  23.    -- Concatenate and display the current values in the variables.
  24.    PRINT @Table
  25.  
  26.    -- This is executed as long as the previous fetch succeeds.
  27.    FETCH NEXT FROM Table_cursor
  28.    INTO @Table;
  29. END
  30.  
  31. CLOSE Table_cursor;
  32. DEALLOCATE Table_cursor;
  33. GO
  34.  
But the problem is that I dont know where to start.

Has anyone an idea how to do this? and please explait it for beginners.
Sep 9 '10 #2
NeoPa
32,556 Recognized Expert Moderator MVP
I struggled to find anything that could work for this, but my T-SQL experience is admittedly limited. Someone may well come up with a simpler solution.

My approach would be to create a Table-Valued User-Defined Function. It would need to take the list of table names as a parameter, and would return the data for the first table as well as checking if any further tables are left in the list and, if so, calling itself recursively to append the rest.

I'm sorry I'm not in a position to make this easy enough for a beginner, but that's a big ask considering the time it would require. Have a play and see what you come up with. We may be able to help further down the line if you have a specific problem.
Sep 9 '10 #3
ck9663
2,878 Recognized Expert Specialist
How many tables are there in total? Do you know the names of these tables? How many tables are you expecting that the view give you as list of table names that you need to read?

~~ CK
Sep 9 '10 #4
Greg Stowell
5 New Member
You mentioned all of the tables contain the same column names....

Do all of the tables have the same data types, same number of columns (with the columns ordinal positions all the same), and you're just looking to create something to do dynamic SQL to union them all together?
Sep 10 '10 #5
Sedanta
6 New Member
Hi Greg
That is correct I need a function to where i Give the list of table names as a Parameter.
Sep 13 '10 #6
ck9663
2,878 Recognized Expert Specialist
There could be other ways of doing it than a function. Like a conditional where, perhaps. It all depends on how many the tables are, the condition that you need to test, the expected size of the result set, etc...

You can even return a result set (table-like) parameter from a stored procedure.

Good Luck!!!

~~ CK
Sep 13 '10 #7
Greg Stowell
5 New Member
This might be what you're looking for - its in a stored proc format - but you could switch it around to be a function without much trouble...

Expand|Select|Wrap|Line Numbers
  1. SET ANSI_NULLS ON
  2. GO
  3. SET QUOTED_IDENTIFIER ON
  4. GO
  5. CREATE PROCEDURE dbo.[sp_UnionEm]
  6.  
  7. AS
  8.  
  9. BEGIN
  10.  
  11. DECLARE @tablenames varchar(8000)
  12. DECLARE @query varchar(8000)
  13.  
  14. DECLARE aCursor Cursor for
  15.  
  16. SELECT 
  17.     tablename
  18. FROM 
  19.     Tablelist
  20.  
  21. OPEN aCursor
  22. Fetch aCursor into @tablenames
  23.  
  24. SET @query = 'SELECT * FROM [' + @tablenames + '] '
  25.  
  26. Fetch aCursor into @tablenames
  27.  
  28. WHILE @@Fetch_Status = 0
  29. BEGIN
  30.     SET @query = @query + 'union all SELECT * FROM [' + @tablenames + '] '
  31.     Fetch aCursor into @tablenames
  32. END
  33.  
  34. exec (@query)
  35.  
  36.  
  37. CLOSE aCursor
  38. DEALLOCATE aCursor
  39.  
  40. END
Sep 16 '10 #8
Greg Stowell
5 New Member
I forgot - Notice the varchar(8000) on the @query?
Be careful how many tables you pull in.. go over that 8000 chars, and it gets truncated.

Good luck, and let me know if the concept helps, or if you get any errors, I can help debug (kinda busy at work - so my replies may take some time).
Sep 16 '10 #9

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

Similar topics

1
by: bettina | last post by:
Ich habe die Tabellen einer Access DB zum MySql exportiert. Aber die Beziehungen zwischen die Tabellen wurden nicht exportiert. Folgende ist die Meldung ich kriegein PhpMyAdmin: Die zusätzlichen...
1
by: Torsten Mohr | last post by:
Hallo, ich möchte eine Funktion schreiben, der ich eine Referenz auf einen String übergeben kann und die dann einige Änderungen am String vornimmt. In Perl würde ich also ein \$string...
1
by: QB | last post by:
Hallo dies ist mein erster Post. ICh möchte eine kleine Applikation schreiben, welche es mir ermöglicht auf eine Access DB die Tabellen zu aktualisieren. Die ^Schwierigkeit für mich dabei...
2
by: amir | last post by:
Hallo liebe Gemeinde, ich möchte aus einem Formular aus, eine bereits vorhandene Laufendenummer aus einer Tabelle.spalte auslese und dazu noch eine 1 addiere und dann das Ergebnis in ein...
1
by: marcel.stallmach | last post by:
Hallo, ich bin ehrlich schon am verzweifeln. Ich habe eine lokale Anwendung (Access) von wo aus eine HttpSendRequest an eine Website gesendet wird. Im HttpSendRequest werden die Daten per...
8
by: Maximilian Hofer | last post by:
Hallo NG, zum erstellen einer Anfrag an eine Website benutze ich folgenden Code: Dim encoding = New System.Text.UTF8Encoding 'Daten zum Posten zusammenbauen Dim postData As String
6
by: Bastian | last post by:
Hi, ich benutze PHP und PostgreSQL. Folgendes Problem: Eine Seite zeigt die Daten, die in einer Tabelle der DB abgespeichert sind. Der Benutzer wählt dann einen Datensatz aus, den er gerne...
1
by: TO | last post by:
Hallo, kann man links neben den Daten einen freien Textoder Grafik platzieren? Unser Briefpapier hat am linken Rand Text. Ich finde keine Möglichkeit, den Text unabhängig von den Daten zu...
1
by: Hugo Balazs | last post by:
Hallo NG, ich möchte mit einem install-script mehrere Tabellen erstellen und habe es zum Testen mit dem Befehl "mysql_query" versucht. Eine Tabelle erstellen funktioniert einwandfrei. Ab zwei...
0
by: Spooks | last post by:
Hi Leute, hier mein kleines Problem: Ich möchte die Tabellen eines SQL Servers auslesen und anschließend die Daten der Tabellen in die Tabellen der Access Datenbank schreiben! (Die Access...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.