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
 Development Tools
 ASP.NET
 form- var. as table name in SQL insert statement.

Author  Topic 

bottomsup
Starting Member

4 Posts

Posted - 2004-10-27 : 19:30:41
hi,
perhaps someone can help me. this seems like it should be easy if i have the right syntax for the sql command string, but i can't seem to find an example on the web:

using ado.net/sql server 2000/asp.net form/VB. doing a basic form which collects information and updates a database via the command object.

Here's the SQL command string-
Dim strSQL as String = "INSERT INTO events (title, description, code) VALUES (@title, @description, @code)"


the question is: how to do i get the table named in the insert string to be a variable passed from the form- the form submitter choses the correct table to update from a dropdown list of the table names from the form itself.

thanks to any that can help.

MichaelP
Jedi Yak

2489 Posts

Posted - 2004-10-27 : 19:56:34
Do a search on the main page for "Dynamic SQL."

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

bottomsup
Starting Member

4 Posts

Posted - 2004-10-27 : 21:27:41
Thanks for the right keyword to use Michael.
Go to Top of Page

bottomsup
Starting Member

4 Posts

Posted - 2004-10-27 : 21:39:39
Hmm.
I did a search for that and found articles, however mostly centered on stored procedures, which isn't my cup of tea... yet. that is why i posted this to the asp.net forum. i need client scripting to accomplish that.

the one client side sample i saw used a literal table name, like all examples i have seen on the web so far. (i am a beginner).

i think i'll leave this post up in case someone else can help me out some more.
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2004-10-28 : 02:42:21
Do all the tables have the same structure? If so, since you are building the string dynamically in VB, there is no reason why you cannot concatenate the table name into the query. Like this:

Dim strSQL as String = "INSERT INTO " & cbTables.SelectedItem.ToString() & " (title, description, code) VALUES (@title, @description, @code)"

Of course, this will not work if each table has a different structure or different column names. In that case you will need to work out a mechanism to supply this meta-data to this part of the code and dynamically build the whole query.

OS
Go to Top of Page

bottomsup
Starting Member

4 Posts

Posted - 2004-10-28 : 10:50:25
Hi mohdowais,

this is what i was looking for. thank you for the help, which i was having alot of troubles finding on the web (probably looking in the wrong places).

yes each of the tables is the same structure for the moment.
Go to Top of Page
   

- Advertisement -