Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 defing table names dynamically using functions

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-06-20 : 09:43:53
Hariharan writes "I would like to create a function that return the value from a table. The name of the table will have to be picked up dynamically from another table. The table structure and the sequence of steps are listed below. Can you kindly help me in creating a function.

Table [Material] {M_Code,M_Name,M_TableName}
Table [Ledger1] {L_Date,L_ClQty,L_ClRate,L_ClValue}

Each Record in the [Material] table will have a corresponding Ledger Table and the name of the Ledger Table is mentioned in the [Material] table.

The function should be like ClValue(Material_Code,Date)

The sequence of steps.
1. Select @TableName=M_TableName from Material where M_Code=@Material_Code.
2. Select @ClValue = L_ClValue from @TableName where L_Date=@Date.
3. Return @ClValue"

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-06-20 : 09:50:23
One of the bad table designs
You need to make use of Dynamic SQL
Refer these
http://www.sommarskog.se/dynamic_sql.html
http://www.nigelrivett.net/SQLTsql/sp_executesql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -