SQL Server - How to get record from table with table valued parameter

Asked By bhanupratap singh on 11-May-15 05:29 PM
Hello friends,
SubMenu                                 Designation datatype is nvarchar(max)
JOB REQUISITION REPORT CEO
FULL AND FINAL UPDATION CEO, CSO, TSO
QUALIFICATION MASTER NULL
CREATE DEPARTMENT NULL
TERMINATION REPORT CEO, CSO, 
CONSULTANT MASTER CEO, CSO, 

Problem is that my table column Designation single row contains multiple designation name
and in my parameter I pass only one designation. I have to retrive the those rows which designation column contains Parameter value. My procedure return all columns why ?

 sql = "usp_HRM_GetMenuToBindMenu";             
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Designation", SessionManager.DesignationName);  is ''TSO' I m sending
                SqlDataAdapter dap = new SqlDataAdapter(cmd);
                dap.Fill(ds);
                cmd.ExecuteNonQuery();
                dt = ds.Tables[0];
I want to fetch those menus whose designation column contain TSO

How to write procedure 
my procedure is below. I fetch record only one record. how to do it

ALTER PROC usp_HRM_GetMenuToBindMenuuuu  
(  
@Designation NVARCHAR(MAX)   
)        
AS        
BEGIN  

  SELECT sm.Designation, mm.MainMenuID,  
        mm.MainMenu,  
        mm.MContent,  
        mm.MenuOrder,  
        mm.IsActive[IsActiveMain],  
        sm.ParentID,  
        sm.SubMenu,  
        sm.MenuOrder[SubMenuOrder],  
        sm.SubMenuID,  
        sm.[Content][Sub Menu Content],  
        sm.SubURL,  
        sm.IsActive[IsActiveSub]  
 FROM   MainMenu mm(NOLOCK)  
        INNER JOIN SubMenu sm(NOLOCK)  
             ON  sm.ParentID = mm.MainMenuID  
 WHERE  --sm.IsActive = 1  
   
 @Designation IN(SELECT sm.Designation FROM SubMenu sm(NOLOCK) WHERE sm.Designation IS NOT NULL)
 --AND sm.Designation IN (@Designation)
 

 ORDER BY  
        mm.MenuOrder ASC,  
        sm.MenuOrder ASC  
END
Robbe Morris replied to bhanupratap singh on 11-May-15 08:18 PM
Go to a local gun store

purchase a gun of your choice

purchase applicable ammunition.  You'll need two shells

return to work and shoot the developer/dba who designed a table to store multiple values delimited with a comma much less put it in an nvarchar(max) data type.  Then, proceed to the next highest technical person that would have been responsible for reviewing this work.  Shoot him/her too.

Honestly, you need to put a stop on development of this, write some code to run once that breaks these values up into individual records in a related table with the destination and whatever the primary key to your submenu table is.  Then, adjust your queries to JOIN to it.

I've fired three developers on the spot for putting crap like this in an application/database.
bhanupratap singh replied to Robbe Morris on 12-May-15 11:44 AM
Thanks for reply
Robbe Morris replied to bhanupratap singh on 12-May-15 05:28 PM
Sorry about the minor rant and not providing a better alternative.  The reality is though that queries to parse out these comma delimited values are going to harm the performance of your app and the performance of your database.  Just wait until one of these values becomes corrupted or needs to be removed/altered from all of the records or worst yet, needs to be included in a wide variety of reports.  Typical reporting tools choke on design like this.

The best alternative is to fix this major design flaw as part of your next major release so that you don't have to suffer the consequences for years to come while supporting the app.
bhanupratap singh replied to Robbe Morris on 12-May-15 06:46 PM
no matter.
I have done this work using like clause instead of IN
Thanks one again
Robbe Morris replied to bhanupratap singh on 12-May-15 07:37 PM
Be aware that the data type of nvarchar(max) cannot be indexed.  So, every single query you run against that delimited value executes a full table scan and will not using any kind of index.  Depending on the size the table, this could have serious performance issues.

It is just one of the many consequences to this sort of architecture/tactic of multi-valued fields.

In any event, wish you the best with your app.

Take care, Robbe