Home > DeveloperSection > Forums > How to select a table from many table while inserting
ranjunath govind
ranjunath govind

Total Post:1

Points:7
Posted on    July-21-2014 10:22 PM

 MSSQL Server MSSQL Server 
Ratings:


 1 Reply(s)
 889  View(s)
Rate this:
I have 4 tables
 
StdntBtech,StdntPg,StdntUg,StdntDip
 
i have to insert values into StdntBtech if the course name is BE or Btech
similarly 
 
i have to insert values into StdntPG if the course name is MCA or MSc
similarly
i have to insert values into StdntUg if the course name is BSc
 
similarly 
i have to insert values into StdntDip if the course name is Diploma

which statement i have to use(IF or Case)
if so how the syntax will be?
how i can use a string instead of table name in insert statement?





Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    July-23-2014 11:11 PM

Here is my code for insert record in a table according to course name from many table while inserting...

string tblname = "";
        if (txtcourse.Text == "BE" || txtcourse.Text == "Btech")
        {
            tblname = "StdntBtech";
        }
        else if (txtcourse.Text == "MCA" || txtcourse.Text == "MSc")
        {
            tblname = "StdntPg";
        }
        else if (txtcourse.Text == "BSc" || txtcourse.Text == "BA")
        {
            tblname = "StdntUg";
        }
        else if (txtcourse.Text == "Diploma")
        {
            tblname = "StdntDip";
        }
        else
        {
            Response.Write("Please Insert Correct Course Name!!");
        }
        string st = "insert into " + tblname + " values('" + txtName.Text + "','" + txtCity.Text + "','" + txtcourse.Text + "')";
        if (tblname != "")
        {
            cn.Open();
            cmd = new SqlCommand(st, cn);
            int executerow = cmd.ExecuteNonQuery();
            if (executerow > 0)
            {
                Response.Write(executerow + " Row's affected");
            }
            cn.Close();
        }
        else
        {
            Response.Write("Please Insert Correct Record");
        }

Modified On Jul-23-2014 11:33:57 PM

Don't want to miss updates? Please click the below button!

Follow MindStick