I want to add multiple values under a specific row for a user. It would look similar to this:
| User | Departments |
|---|---|
| John | Deli |
| John | Frozen Goods |
| John | Bakery |
So far, I am having to add in these values individually and manually, is there a way to add all departments under on user in a single SQL statement?
Update Here is a snippet of code I have so far. With this, it creates a comma separated list for departments, but I would rather just have the user listed multiple times and a different department assigned to each of those instances.
department = "," + department + ",";
department = department.Replace(",,", ",");
department = department.Replace(",,", ",");
department = department.Replace(",,", ",");
department = department.Replace(",,", ",");
strsql = "UPDATE [TV_App].[dbo].[users] " +
"SET " +
"[u_FirstName] = '" + firstname + "' " +
",[u_LastName] = '" + lastname + "' " +
",[u_Admin] = " + admrights +
",[u_Departments] = '" + department + "'" +
",[u_LastEditedOn] = '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'" +
",[u_LastEditedBy] = '" + username + "' " +
"WHERE [u_ADUN] = '" + username + "' " +
"IF @@ROWCOUNT = 0 " +
"INSERT INTO [TV_App].[dbo].[users] " +
"( " +
"[u_ADUN] " + ",[u_FirstName] " + ",[u_LastName] " +
",[u_Admin] " + ",[u_Departments] " +
",[u_CreatedOn] " + ",[u_CreatedBy] " +
",[u_LastEditedOn] " + ",[u_LastEditedBy] " +
",[u_LastLoginOn] " + ",[u_LastLoginFrom] " +
",[u_FirstLoginOn] " + ",[u_FirstLoginFrom] " +
",[u_UsageCount] " +
") VALUES( " +
"'" + username + "', " + "'" + firstname + "', " + "'" + lastname + "', " +
admrights + ", '" + department + "', " +
"'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "', " + "'" + Environment.UserName.ToLower() + "', " +
"'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "', " + "'" + Environment.UserName.ToLower() + "', " +
"'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "', 'newuser', " +
"'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "', 'newuser', " +
"0" +
") ";
SqlCommand cmd = new SqlCommand(strsql, conn);
cmd.Connection.Open();
cmd.ExecuteReader();
cmd.Connection.Close();
return;
Additional information: I am adding these items from a checked listbox.