SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 UDF - Can i pass a table as a parameter?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

landau66
Yak Posting Veteran

Austria
61 Posts

Posted - 01/20/2014 :  14:52:27  Show Profile  Reply with Quote
Hi everyone,

I have several tables which are all identical in columns and datatypes. i want to create a function which does some calculations on this tables.
Since I just want to create one function I Need to specify the table on which the function should work on. That means i want to pass the table Name as a Parameter to the function.

Is that possible? Or how would you solve this problem.


Thank you very much in advance and greetings from Austria
landau

tkizer
Almighty SQL Goddess

USA
37446 Posts

Posted - 01/20/2014 :  14:57:00  Show Profile  Visit tkizer's Homepage  Reply with Quote
You'd have to use a stored procedure as you can't use dynamic SQL in a function.

But I would solve this problem by normalizing the database. You should not have tables like this.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

landau66
Yak Posting Veteran

Austria
61 Posts

Posted - 01/20/2014 :  15:04:52  Show Profile  Reply with Quote
hi tkizer!

i have the Price data from several stocks. Ofcourse the structure of this tables is the same. Because of the facility of inspection i like to Keep the data of a stock in its own table.

Or waht do you mean exactly?

thank you very much
landau
Go to Top of Page

landau66
Yak Posting Veteran

Austria
61 Posts

Posted - 01/20/2014 :  15:07:14  Show Profile  Reply with Quote
By the way:
How would I enter the Name of the table into the stored procedure?

thanks a lot
landau
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
37446 Posts

Posted - 01/20/2014 :  15:10:16  Show Profile  Visit tkizer's Homepage  Reply with Quote
quote:
Originally posted by landau66

By the way:
How would I enter the Name of the table into the stored procedure?

thanks a lot
landau



You have to use dynamic SQL: http://www.sommarskog.se/dynamic_sql.html

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
37446 Posts

Posted - 01/20/2014 :  15:11:39  Show Profile  Visit tkizer's Homepage  Reply with Quote
quote:
Originally posted by landau66

hi tkizer!

i have the Price data from several stocks. Ofcourse the structure of this tables is the same. Because of the facility of inspection i like to Keep the data of a stock in its own table.

Or waht do you mean exactly?




I would put the data into one table. Add another column if you need it to differentiate them, then use that column to get only the data you need.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 01/20/2014 :  19:16:03  Show Profile  Reply with Quote
As Tara mentioned, part of the process of creating a proper schema is called Normalization. If you try to follow the rules of normalization you won't have to worry about trying to implements the hacks you are trying to do now.

Here is a link that has a little information about the subject:
http://en.wikipedia.org/wiki/Database_normalization
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000