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
 General SQL Server Forums
 New to SQL Server Programming
 Parametrisation

Author  Topic 

Petar_T
Starting Member

8 Posts

Posted - 2010-12-24 : 07:53:20
Hi.
I have a question which I don't know how to state. It is connected with my previouse question (http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=154445) "How can I make table name a variable". At this moment I am reading an article about dynamic Sql which was kindly sugested to me by pk bohra.


Explaining my question I will start with this:

create table A(
A_id int primary key identity,
title nvarchar(max),
B_fk foreign key references B ( B_id )
)
go

create table B(
B_id int primary key identity,
tekst nvarchar(max)
)
go


Now... what I need is next... I need that.. in table A I can hold a foreign key to - any table. (But.. )

During the run-time I will be adding new tables similar like a table B, and I need this tables to be able to "link" them all to one single table A, in the run-time.

So, for example... I want my data to be organised in a "tree sructure".... maybe like this...

create table Tree(
tree_id int primary key identity,
title nvarchar(max),
notes_id foreign key references Notes ( notes_id )
parent_id foreign key references Tree ( tree_id )
)
go

create table Notes(
notes_id int primary key identity,
title nvarchar(max),
tekst nvarchar(max)
)



But, what I need ... is that... during the run-time of an application.... I would like to be able to add tables similar like Notes table, and yet, that the same column or colums may point to that table, so in pseudo-code, I would not have in a table Tree line:

notes_id foreign key references Notes ( notes_id )

,but instead of that line, column, I would like to have something similar like this:

table - which is "table name"
id_from the table

Actually, it would be like if in a foreign key, there wouldn't be only id integer which would be changing but also a 'references' table, and a column, like if I would be able to have a changing value in all three underlined things

notes_id foreign key references Notes ( notes_id )


Like a directory structure that can hold txt, pdf, gif, etc. files.

So I figured it out that it would be the best to do it with a procedure and a table name as a procedure parameter, and I got a very good answers.

But now I am wondering is it possible to do it on a better way.

And that is my question.


: ) I am gonna spend this few days in front of the screen typing, quite happily...

Marry Christmas to Everyone

Petar

Petar_T
Starting Member

8 Posts

Posted - 2010-12-24 : 08:56:14
I wanted to explain for what i need this for, and i managed not to tell it. I am making a home project application which should serve me for my learning, kind of lika a "notes organiser", where "notes" should be data organised in some kind of "tree structure", but on the "nodes" of a tree structure there should be something like a foreign key to things like: "noets", "forum topic", "href", "pdf-chapter", and things like this. What I wanted to say is that I found out how to make it with dynamic sql or procedures, but reading that article about dynamic sql i figured out that I really don't know anything, so I just wanted to know is there at all some other way how could I do it? But I started to think that this what I am writing is a little bit unpolite and a litle bit dumb. Sorry.
Go to Top of Page
   

- Advertisement -